I was looking at this question for generating a circular thumbnail and ran into something unexpected. Here's my question:
For img
img = Image.new('RGB', (1080, 1080), color = black)
For
from PIL import Image, ImageOps
mask = Image.open('mask.png').convert('L')
im = Image.open('image.png')
output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5))
output.putalpha(mask)
output.save('output.png')
Outputs as a circle (this is fine, bellow is the problem):
output.png
But when I run this code to copy the image onto another image:
icon = Image.open('output.png', 'r')
offset=(80,80)
img.paste(icon, offset)
I end up with this:
However if I save the image as a gif and use the following code then the I can get a circle:
from PIL import Image, ImageOps, ImageDraw
size = (128, 128)
mask = Image.new('L', size, 255)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + size, fill=0)
im = Image.open('android.png')
output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5))
output.paste(0, mask=mask)
output.convert('P', palette=Image.ADAPTIVE)
output.save('output.gif', transparency=0)
output.gif:
Running the same code (displayed again for convenience) using output.gif results in the correct result:
icon = Image.open('output.gif', 'r')
offset=(80,80)
img.paste(icon, offset)