如何使用python枕头创建圆形缩略图并在背景图像上重叠 [英] How to create a circular thumbnail using python pillow and overlap on background image

查看:106
本文介绍了如何使用python枕头创建圆形缩略图并在背景图像上重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

avatar.jpg

avatar.jpg

back.jpg

如何合成两个图像,如下所示?

How to synthesize two images as follows?

我的作用:

推荐答案

以下是使用图片的示例.尺寸在示例中进行了硬编码,但是您可以轻松地用计算替换尺寸. avatar.jpg和background.jpg是帖子中的图像按原样保存.

Here's an example using your images. Dimensions are hardcoded in the example, but you can easily replace them with calculations. avatar.jpg and background.jpg are images in your post saved as is.

以下是此示例的github存储库链接: python_pillow_circular_thumbnail

Here's a link to github repo for this example : python_pillow_circular_thumbnail

from PIL import Image, ImageOps, ImageDraw

im = Image.open('avatar.jpg')
im = im.resize((120, 120));
bigsize = (im.size[0] * 3, im.size[1] * 3)
mask = Image.new('L', bigsize, 0)
draw = ImageDraw.Draw(mask) 
draw.ellipse((0, 0) + bigsize, fill=255)
mask = mask.resize(im.size, Image.ANTIALIAS)
im.putalpha(mask)

output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5))
output.putalpha(mask)
output.save('output.png')

background = Image.open('back.jpg')
background.paste(im, (150, 10), im)
background.save('overlap.png')

output.png:

overlap.png:

此代码的一部分是从 answer 借来的.

Crop part of this code is borrowed form this answer.

希望有帮助!

这篇关于如何使用python枕头创建圆形缩略图并在背景图像上重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆