How to make background image transparent using python

Active January 14, 2019    /    Viewed 34207    /    Comments 1    /    Edit


An example of how to make background image transparent using python (image used here open-science-logo.png).

<!DOCTYPE html>
<html lang="en">
<body style="background-color: #3c3d41;">

<img src="open_science_logo.png" style="width:500px;margin: 200px 500px;">

</body>
</html>

Rendre l'arrière-fond d'une image transparent avec python

Code python based on Pillow module

from PIL import Image

img = Image.open('open_science_logo.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:
        if item[0] > 150:
            newData.append((0, 0, 0, 255))
        else:
            newData.append(item)
            print(item)


img.putdata(newData)
img.save("open_science_logo_transparent.png", "PNG")

References

Links Site
How to merge a transparent png image with another image using PIL stackoverflow
Using PIL to make all white pixels transparent? stackoverflow
HTML PNG image transparent stackoverflow
How to convert the background to transparent? [closed] stackoverflow
Using the Right Logo Image – Transparent PNG stackoverflow
How to Make a Transparent PNG stackoverflow
How to paste a PNG image with transparency to another image in PIL without white pixels? stackoverflow
lunapic stackoverflow
Make the background of an image transparent in under two minutes on a Mac stackoverflow
How to Make Transparent Images with Preview in OS X stackoverflow
HTML background color w3schools

Comments


Hi, I have also written an article on the topic "Extract text from Image Using Python"

Link:- https://www.techroadmap.in/blog/extract-text-from-image-using-python/

If you like this article then can you add this article link to your website

ag7842 March 17, 2021, 10:38 a.m.
you need to be logged in to post a comment
you need to be logged in to post a comment


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!



Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount