图像处理 https://github.com/python-pillow/Pillow
天问 3aa7ec69cc Update 'README.md' | 9 months ago | |
---|---|---|
README.md | 9 months ago |
图像处理
创建图片
from PIL import Image
image = Image.open('python-logo.png')
print(image.format, image.size, image.mode) # 查看图像实例的属性
image.show() # 显示图像
f, _ = os.path.splitext(image_path)
outfile = f + ".jpg"
try:
Image.open(image_path).save(outfile) # 修改文件格式
except IOError:
print("无法转换", image_path)
处理图片
def roll(image, delta):
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(part1, (xsize - delta, 0, xsize, ysize))
image.paste(part2, (0, 0, xsize - delta, ysize))
return image
image_path = 'test.jpg'
im = Image.open(image_path)
roll(im, 300).show() # 向侧面滚动300像素