VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-make-background-image-transparent-using-python/

⇱ How to make background image transparent using Python? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to make background image transparent using Python?

Last Updated : 3 Feb, 2026

In this article, we will learn how to make an image background transparent by processing and modifying its pixel values using Python's Pillow library (Python Imaging Library).

Pillow supports RGBA images, where the extra Alpha channel controls pixel transparency. You can install Pillow using the following pip command:

pip install pillow

Approach

  1. Read the image using Pillow.
  2. Convert the image to RGBA format (Red, Green, Blue, Alpha).
  3. Read pixel data.
  4. Detect background-colored pixels (for example, white).
  5. Set their alpha value to 0.
  6. Save the result as a PNG file (PNG supports transparency).

Python Implementation

Input:

👁 Image

Output:

👁 Image

Explanation:

  • convert("RGBA"): adds an alpha channel so transparency can be stored.
  • getdata(): returns all pixel values.
  • Loop reads each pixel as (r, g, b, a).
  • White pixels are detected using RGB values (255,255,255).
  • (255,255,255,0): sets alpha to 0 → fully transparent.
  • Other pixels are kept unchanged.
  • putdata(): writes modified pixels back.

Related Articles:

Comment
Article Tags: