Monday, September 8, 2025
HomeLanguagesHow to make background image transparent using Python?

How to make background image transparent using Python?

In this article, the task is to create a background transparent of the image in Python

Library Required :

First Install pillow library on your Python Application before going ahead. Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many image file formats. Pillow library is necessary for this mentioned program. You can install pillow library in Python using the code

pip install pillow

Method :

  1. Read the image.
  2. Convert the image into RGBA format.
  3. Change the white pixels of the image into a transparent form
  4. Save the newly edited image

Example :

Python3




from PIL import Image
 
def convertImage():
    img = Image.open("./image.png")
    img = img.convert("RGBA")
 
    datas = img.getdata()
 
    newData = []
 
    for item in datas:
        if item[0] == 255 and item[1] == 255 and item[2] == 255:
            newData.append((255, 255, 255, 0))
        else:
            newData.append(item)
 
    img.putdata(newData)
    img.save("./New.png", "PNG")
    print("Successful")
 
convertImage()


Output:

RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6644 POSTS0 COMMENTS
Nicole Veronica
11808 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11871 POSTS0 COMMENTS
Shaida Kate Naidoo
6755 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS