Python PIL.Image Manipulations

python day 29

Recently i like many cute emoji stickers and memes. So let’s try to learn how to manipulate the images in python and maybe try to make a unique sticker by yourself!

I will use 大理寺日志 The White Cat Legend as an example, it is a short cute cartoon 😻

  • PIL Image, ImageFilter Module: image info, corp, pixel changes, thumbnail, filter, combine images, rotate, mirror images .
from PIL import Image, ImageFilterimage=Image.open(r"\Desktop\cat.jpg")
image.show()
# Image info
image.format,image.size,image.mode
# Corp Image
rect= 80,20,180,100
image.crop(rect).show()
# Thumbnail
thumb_image=Image.open(r"\Desktop\cat.jpg")
size=128,128
thumb_image.thumbnail(size)
thumb_image.show()
#pixel changes
for x in range(80,180):
for y in range (20,100):
image.putpixel((x,y), (128,128,128))
image.show()
# rotate
image.rotate(180).show()
# mirror image
image.transpose(Image.FLIP_LEFT_RIGHT).show()
# filter
image.filter(ImageFilter.CONTOUR).show()
#sticker
from PIL import Image, ImageFilter
image1=Image.open(r"\Desktop\cat.jpg")
image2=Image.open(r"\Desktop\dali.jpg")
rect= 80,20,180,100
fun_sticker=image1.crop(rect)
width,height=fun_sticker.size
image2.paste(fun_sticker.resize((int(width/1.5), int(height/1.5))), (190,40))
image2.show()
from IPython.display import Image
from IPython.display import HTML, display
p2=Image(filename="/Desktop/cat_var.png" )
display(p2)
the legend of the white cat

note: the image.show() has the format .BMP, i don’t want to convert every image one by one, so i put all the image variation together at the end.

Happy Studying!

Reference:

https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/15.%E5%9B%BE%E5%83%8F%E5%92%8C%E5%8A%9E%E5%85%AC%E6%96%87%E6%A1%A3%E5%A4%84%E7%90%86.md