To create a transparent png with PIL you can use this code, as example. In this particular example I joined 2 transparent png and pasting them side by side on a new transparent image of the same size of the two together.
The process in the code is this:
- we create a new image (Image.new) with “RGBA” as first argument and (0,0,0,0) as third argument
- then we open two png (transparent)
- we past the 2 element one next to the other
First Image
second image
# combine 2 png images horizzontally
from PIL import Image
def combine(i1,i2):
i1 = Image.open(i1)
i2 = Image.open(i2)
x1, y1 = i1.size
x2, y2 = i2.size
x3 = x1 + x2
y3 = y1 if y1 > y2 else y2
i3 = Image.new("RGBA",(x3,y3), (0,0,0,0))
if y1 > y2:
y2 = (y1 - y2) // 2
else:
y2 = y1
i3.paste(i1, (0,0), i1)
i3.paste(i2, (x1,y2), i2)
return i3
i = combine("folder.png","py.png")
i.save("folder.png", "PNG")
i
The output (images 1 and 2 side by side) on a transparent background


