Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate circular thumbnails with PIL?

How do I generate circular image thumbnails using PIL? The space outside the circle should be transparent.

Snippets would be highly appreciated, thank you in advance.

like image 629
ohnoes Avatar asked May 20 '09 20:05

ohnoes


People also ask

How do I PIL an image?

Image. open() Opens and identifies the given image file. This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load() method).

How do I cut a picture in PIL?

Solution: img. To crop an image to a certain area, use the PIL function Image. crop(left, upper, right, lower) that defines the area to be cropped using two points in the coordinate system: (left, upper) and (right, lower) pixel values. Those two points unambiguously define the rectangle to be cropped.

How do I import a picture into PIL?

To load the image, we simply import the image module from the pillow and call the Image. open(), passing the image filename. Instead of calling the Pillow module, we will call the PIL module as to make it backward compatible with an older module called Python Imaging Library (PIL).


1 Answers

The easiest way to do it is by using masks. Create a black and white mask with any shape you want. And use putalpha to put that shape as an alpha layer:

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') 

Here is the mask I used:

alt text


If you want the thumbnail size to be variable you can use ImageDraw and draw the mask:

from PIL import Image, ImageOps, ImageDraw  size = (128, 128) mask = Image.new('L', size, 0) draw = ImageDraw.Draw(mask)  draw.ellipse((0, 0) + size, fill=255)  im = Image.open('image.jpg')  output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5)) output.putalpha(mask)  output.save('output.png') 

If you want the output in GIF then you need to use the paste function instead of putalpha:

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('image.jpg')  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) 

Note that I did the following changes:

  • The mask is now inverted. The white was replaced with black and vice versa.
  • I'm converting into 'P' with an 'adaptive' palette. Otherwise, PIL will only use web-safe colors and the result will look bad.
  • I'm adding transparency info to the image.

Please note: There is a big issue with this approach. If the GIF image contained black parts, all of them will become transparent as well. You can work around this by choosing another color for the transparency. I would strongly advise you to use PNG format for this. But if you can't then that is the best you could do.

like image 194
Nadia Alramli Avatar answered Sep 27 '22 17:09

Nadia Alramli