0

I was trying to resize an image to a circular shape, but if i run the code, the transparency of the image is gone.


from PIL import Image,ImageOps

pb = Image.open('pb_image')

bigsize = (pb.size[0] * 3, pb.size[1] * 3)

mask = Image.new('L', bigsize, 0)

mask = mask.resize(pb.size, Image.ANTIALIAS)
pb.putalpha(mask)

circled = ImageOps.fit(pb, mask.size, centering=(0.5, 0.5))
circled.putalpha(mask)

circled.show()

This is my code (pb is a loaded image), but either the image is not resized at all, the image is resized but the transparent places are black or the image is completely transparent. I dont know how to fix this im new to pillow. Thanks in advance.

pippo1980
  • 2,181
  • 3
  • 14
  • 30
Philskillz
  • 11
  • 2
  • Please edit your post to make it actually runnable - that means putting back the `import` statements you have removed for some reason, and including your input and actual output image (or reasonable representation of them) and expected output image (or a reasonable mock-up). – Mark Setchell Aug 03 '21 at 14:08

1 Answers1

0

from How do I generate circular thumbnails with PIL?

using mask.png :

enter image description here

code:

from PIL import Image,ImageOps

image = 'pb.png'


pb = Image.open(image)

mask = Image.open('mask.png').convert('L') 

mask = mask.resize(pb.size, Image.ANTIALIAS)

circled = ImageOps.fit(pb, mask.size, centering=(0.5, 0.5))

circled.putalpha(mask)

circled.show()

circled.save('out.png')

pippo1980
  • 2,181
  • 3
  • 14
  • 30