Pillow
目录
本篇博客简单介绍pillow,作为学习的记录。更多信息请前往PIL documentation
pillow是python的一个标准图像处理库,相比于opencv,它像是一个玩具,但是已经能够满足一些基本的使用。
与此相关的:opencv, PhotoShop
Basics
Read image
from PIL import Image
im = Image.open('image/animate2.png')
im.show()
print(im.size)
show information
print(im.info)
print(im.mode)
print(im.format)
RGBA是一种颜色格式,alpha通道表示不透明度。例如:logo图标通常是png图片,如果转换为jpg图片会有黑边。
convert format
img_jpg = im.convert('RGB')
img_jpg.save('image/animate2_jpg.jpg')
im.show()
img_jpg.show()
RGB to RGBA 同理
resize
white = Image.open('image/inwhite1.jpg')
white_resize = white.resize((500,500))
white_resize.show()
关于resize这里有一些讨论知乎, GitHub。总结就是PIL和opencv的像素点选择有差异,所以会导致图像有细微差别。如果你只是简单的使用不需要关心这一点,如果你在做cv就需要格外注意。
thumbnail
white.thumbnail((150,150))
white.show()
print(white.size)
white.save('image/thumbnail.jpg')
使用resize可能会变形,有时候我们不希望这么做,那就可以选择thumbnail
,它会进行等比例缩放。
blend
white1 = Image.open('image/inwhite1.jpg')
white2 = Image.open('image/inwhite2.jpg')
white1 = white1.resize(white2.size)
blended = Image.blend(white1, white2, 0.35)
blended.show()
对于在上例中blend
的操作可以理解为blended = white1*0.65+white*0.35
rotate
nico = Image.open('image/nike.png')
nico_out = nico.rotate(-45, translate=(0,-25), fillcolor='blue')
nico_out.show()
nico_out.save('image/nike_rotate.png')
modify image
这里仅给出一个例子:为图片添加水印(water mask)
选择字体(可选)
from PIL import ImageFont
font1 = ImageFont.truetype('C:/Windows/Fonts/Inkfree.ttf',size=100)
font2 = ImageFont.truetype('C:/Windows/Fonts/fengxingti.ttf',size=100) #风行体
font3 = ImageFont.truetype('C:/Windows/Fonts/shiguangshaonv.ttf',size=100) #时光少女
font = font2
from PIL import ImageDraw
def creating_watermark(im,text,font=font):
im_rgba = im.convert('RGBA')
im_text_canvas = Image.new("RGBA",im_rgba.size, (255,255,255,0))
draw = ImageDraw.Draw(im_text_canvas)
text_x_width, text_y_height = draw.textsize(text, font=font)
text_xy = (im_rgba.size[0] - 2 * text_x_width, im_rgba.size[1] - 2 * text_y_height)
draw.text(text_xy, text, font=font, fill=(255,0,0,128))
im_text = Image.alpha_composite(im_rgba, im_text_canvas)
return im_text
blended_watermark = creating_watermark(blended, 'love',font=font3)
blended_watermark.show()
blended_watermark.save('image/blended3.png')
Gif
opencv不支持处理gif格式的文件,但是pillow和imageio可以。
gif from images
import os
def jpg_to_gif(jpg_path, gif_name):
frames = []
for i in range(11):
path = os.path.join(jpg_path, 'girl{}.jpg'.format(i))
frame = Image.open(path)
frame.thumbnail((1000,1000))
frames.append(frame)
frames[0].save(gif_name, save_all=True, append_images=frames[1:], format='GIF', duration=1000, loop=0, disposal=2)
jpg_to_gif('./image/', './video/scaled_girls.gif')
效果展示
图片来源于https://www.vmgirls.com/,侵删。
imageio也可以处理gif
import imageio
def create_gif(image_list, gif_name, duration=0.35):
frames = []
for image_name in image_list:
frames.append(imageio.imread(image_name))
imageio.mimsave(gif_name, frames, 'GIF', duration=duration)
return
image_list = ['image/girl{}.jpg'.format(i) for i in range(11)]
create_gif(image_list, './video/girls_by_imageio.gif')
operations on gif
reference:PIL fork
girls = Image.open("video/scaled_girls.gif")
type(girls)
# PIL.GifImagePlugin.GifImageFile
GifImageFile继承自ImageFile,有如下几种属性
- n_frames
- is_animated
- seek()
- tell()
example:
cropped_images = []
crop_box = (250, 250, 500, 700)
for index in range(girls.n_frames):
girls.seek(index)
cropped = girls.crop(crop_box)
cropped_images.append(cropped)
cropped_images[0].save(
'video/sample_cropped.gif',
save_all=True,
append_images=cropped_images[1:],
duration=500,
loop=0,
)