我需要拍摄一张图像并将其放置到新生成的白色背景上,以便将其转换为可下载的桌面壁纸。所以这个过程是这样的:
- 生成尺寸为 1440x900 的新的全白图像
- 将现有图像放在顶部居中
- 另存为单张图像
在 PIL 中,我看到ImageDraw
对象,但没有任何迹象表明它可以将现有图像数据绘制到另一个图像上。有人可以推荐建议或链接吗?
这可以通过 Image 实例来完成paste
method:
from PIL import Image
img = Image.open('/path/to/file', 'r')
img_w, img_h = img.size
background = Image.new('RGBA', (1440, 900), (255, 255, 255, 255))
bg_w, bg_h = background.size
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
background.paste(img, offset)
background.save('out.png')
这个和许多其他 PIL 技巧可以在Nadia Alramli 的 PIL 教程 http://web.archive.org/web/20130115175340/http://nadiana.com/pil-tutorial-basic-advanced-drawing
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)