How to make background image transparent using python

January 14, 2019    /    Viewed: 22066    /    Comments: 0    /    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


Card image cap
profile-image
Daidalos

Je développe le présent site avec le framework python Django. Je m'intéresse aussi actuellement dans le cadre de mon travail au machine learning pour plusieurs projets (voir par exemple) et toutes suggestions ou commentaires sont les bienvenus !