pillow入门
import numpy
import PIL.Image as Image
import PIL.ImageFont as ImageFont
import PIL.ImageDraw as ImageDraw
import os
import sys
src = r'C:\Users\lovelyfrog\Pictures\wechat'
os.path.splitext(r'C:\Users\lovelyfrog\Pictures\wechat'+'0.jpg')
('C:\\Users\\lovelyfrog\\Pictures\\wechat0', '.jpg')
# 分别返回图片格式,大小,图片模式
img = Image.open( r'C:\Users\lovelyfrog\Pictures\wechat\0.jpg')
print(img.format, img.size, img.mode)
JPEG (640, 640) RGB
#显示图像
img.show()
#转换文件格式到png
file = r'C:\Users\lovelyfrog\Pictures\wechat\0.jpg'
f, e = os.path.splitext(file)
outfile = f + '.png'
try:
Image.open(file).save(outfile)
except IOError:
print("can't convert", file)
#创建缩略图
size = (100,100)
img = img.resize(size, Image.ANTIALIAS)
img.save(r'D:\python_project\some_thought\0.jpeg','jpeg')
#剪切,粘贴,合并图像
img = Image.open( r'C:\Users\lovelyfrog\Pictures\wechat\0.jpg')
box = (0,0,300,300)
region = img.crop(box)
region
img.paste(region,(100,100,400,400))
img
# 将图像平移
def roll(image, delta):
"Roll an image sideways"
xsize, ysize = image.size
delta = delta % xsize
if delta == 0: return image
part1 = image.crop((0, 0, delta, ysize))
part2 = image.crop((delta, 0, xsize, ysize))
image.paste(part2, (0, 0, xsize-delta, ysize))
image.paste(part1, (xsize-delta, 0, xsize, ysize))
return image
roll(img, 100)
img
#分离,合并颜色通道
img = Image.open( r'C:\Users\lovelyfrog\Pictures\wechat\0.jpg')
r,g,b = img.split()
img = Image.merge('RGB',(b,g,r))
img
#简单的几何变换
out = img.resize((100,100))
out = img.rotate(45)
out
#颜色变换
out = img.convert('L')
out
#应用点操作,对每个像素点乘1.2
out = img.point(lambda x : x*1.2)
out