0

I can make some .png files using PIL and tkinter:

import tkinter
from PIL import ImageGrab

canvas = tkinter.Canvas(width=100, height=100)

SomeInterestingPhotoImage = PhotoImage(file='Path/To/My/file.png')
canvas.create_image(0, 0, image=SomeInterestingPhotoImage, anchor='nw')

x0 = canvas.winfo_rootx()
y0 = canvas.winfo_rooty()
x1 = x0 + canvas.winfo_width()
y1 = y0 + canvas.winfo_height()

image = ImageGrab.grab((x0, y0, x1, y1))

image.save(name.png)

# I need to make some pixels transparent.

Here I can make an .png file, but all pixels have some colour. I need to make white pixels of canvas (not of image) transparent.

acw1668
  • 40,144
  • 5
  • 22
  • 34

1 Answers1

0
import glob
from PIL import Image

def transparent(myimage):
    img = Image.open(myimage)
    img = img.convert("RGBA")

    pixdata = img.load()

    width, height = img.size
    for y in range(height):
        for x in range(width):
            if pixdata[x, y] == (0, 0, 0, 255):
                pixdata[x, y] = (0, 0, 0, 0)


    img.save(myimage, "PNG")

for image in glob.glob("*.png"):
    transparent('mypic.png')

This code will basically change black (0, 0, 0, 255) to transparent (0, 0, 0, 0) pixels