您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何将具有透明性的PNG图像粘贴到PIL中的另一幅没有白色像素的图像?

如何将具有透明性的PNG图像粘贴到PIL中的另一幅没有白色像素的图像?

您需要在粘贴功能中将图像指定为遮罩,如下所示:

import os
from PIL import Image

filename = 'pikachu.png'
ironman = Image.open(filename, 'r')
filename1 = 'bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0), mask=ironman)
text_img.save("ball.png", format="png")

给你:

透明粘贴

要将背景图像和透明图像居中放置在new上text_img,您需要根据图像尺寸计算正确的偏移量:

text_img.paste(bg, ((text_img.width - bg.width) // 2, (text_img.height - bg.height) // 2))
text_img.paste(ironman, ((text_img.width - ironman.width) // 2, (text_img.height - ironman.height) // 2), mask=ironman)
其他 2022/1/1 18:31:18 有544人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶