我有一个透明背景的PNG图像,我想将其调整为另一张图像的大小,但是要使用白色背景而不是透明背景。如何使用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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句