本文介绍了如何使用PIL将透明图像的背景设置为白色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个透明背景的PNG图像,我想将其调整为另一幅图像,但背景是白色而不是透明背景.我该如何使用PIL?
I have a PNG image with transparent background and I want to resize it to another image, but with a white background instead of a transparent one.How can I do that with PIL?
这是我的代码:
basewidth = 200
img = Image.open("old.png")
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)hsize = int((float(img.size[1]) * float(wpercent)))
img.save("new.png")
推荐答案
import Image
from resizeimage import resizeimage
f = Image.open('old.png')
alpha1 = 0 # Original value
r2, g2, b2, alpha2 = 255, 255, 255,255 # Value that we want to replace it with
red, green, blue,alpha = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3]
mask = (alpha==alpha1)
data[:,:,:3][mask] = [r2, g2, b2, alpha2]
data = np.array(f)
f = Image.fromarray(data)
f = f.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
f.save('modified.png', image.format)
这篇关于如何使用PIL将透明图像的背景设置为白色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!