TOC

Pillow 旋转

抱歉,又水一篇

准备

先用 https://uutool.cn/text2img/ 生成一张兰亭集序的图片,保存为 兰亭集序_orig.png

稍加处理,避免旋转之后没有一个好的参照系,看不明显:

cp ~/Pictures/兰亭集序_orig.png ~/Pictures/兰亭集序.png
convert -bordercolor "#000" -border 2 ~/Pictures/兰亭集序.png ~/Pictures/兰亭集序.png
convert -bordercolor "#fff" -border 10 ~/Pictures/兰亭集序.png ~/Pictures/兰亭集序.png

代码

import os

from PIL import Image

img_path = os.path.expanduser('~/Pictures/兰亭集序.png')
save_path = '/tmp/123.png'

if os.path.exists(save_path):
    os.unlink(save_path)
assert not os.path.exists(save_path)

img = Image.open(img_path)
img.rotate(
    0.91,
    resample=2,
    # expand=True,
    fillcolor='#fff',
).save(save_path)

print(os.path.exists(save_path))

resample:

  • 0: Image.NEAREST
  • 2: Image.BILINEAR
  • 3: Image.BICUBIC

具体的含义我也不清楚,最近,双线性,双三次,反正下面两个值加上效果会看起来更加顺滑一些。