[英]How do i change the background color in python of this combined png image?
I have the following code that works great except, however it produces a black background image, and i would like it to be white.我有以下代码很好用,但它会产生黑色背景图像,我希望它是白色的。 I have tried changing bg_color but anything I change it to still keeps the black background.
我试过改变 bg_color 但我改变它的任何东西仍然保持黑色背景。 Is there also a way that i can make a box around each image that i am combining?
还有一种方法可以在我组合的每个图像周围制作一个框吗?
direction = 'vertical'
bg_color=(1,1,1,1)
aligment='center'
images = [Image.open(x) for x in ['LVISF2_ABoVE2019_0801_R2003_084168.TXT_ZG.png', 'LVISF2_ABoVE2019_0801_R2003_084168.TXT_RH95.png']]
widths, heights = zip(*(i.size for i in images))
if direction=='horizontal':
new_width = sum(widths)
new_height = max(heights)
else:
new_width = max(widths)
new_height = sum(heights)
new_im = Image.new('RGB', (new_width, new_height), color=bg_color)
offset = 0
for im in images:
if direction=='horizontal':
y = 0
if aligment == 'center':
y = int((new_height - im.size[1])/2)
elif aligment == 'bottom':
y = new_height - im.size[1]
new_im.paste(im, (offset, y))
offset += im.size[0]
else:
x = 0
if aligment == 'center':
x = int((new_width - im.size[0])/2)
elif aligment == 'right':
x = new_width - im.size[0]
new_im.paste(im, (x, offset))
offset += im.size[1]
new_im.save('test4.jpg')
turns out my original png background was not white, it was transparent So i had to change the transparent background to white, and then I was able to change the background color.原来我原来的 png 背景不是白色的,它是透明的所以我不得不将透明背景更改为白色,然后我才能更改背景颜色。
Here is how i changed the original png backgrounds to white这是我如何将原始 png 背景更改为白色
images1 = ['LVISF2_ABoVE2019_0801_R2003_084168.TXT_ZG.png', 'LVISF2_ABoVE2019_0801_R2003_084168.TXT_RH95.png']
for i in images1:
im = Image.open(r'{0}'.format(i))
fill_color = (255,255,255)
im = im.convert("RGBA")
if im.mode in ('RGBA', 'LA'):
background = Image.new(im.mode[:-1], im.size, fill_color)
background.paste(im, im.split()[-1]) # omit transparency
im = background
im.convert("RGB").save(r'newimage_{0}.jpg'.format(i))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.