简体   繁体   English

如何更改此组合 png 图像的 python 中的背景颜色?

[英]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.

相关问题 如何在pygame中将png图像作为精灵上传,没有黑色背景? - How do I upload a png image in pygame as a sprite, without the black background? 如何在Python的Tkinter中更改按钮的颜色 - How do I change the color of a button in Python's Tkinter 如何更改 WTForm 上文本的颜色? (蟒蛇,HTML) - How do I change the color of text on a WTForm? (python, html) 如何在 Python 3 中减少图像的调色板? - How do I reduce the color palette of an image in Python 3? 如何在Python Tkinter中将这些按钮覆盖在图像的背景上 - How do I overlay these buttons on top of an image for a background in Python Tkinter 如何将使用cv2.imread('img.png',cv2.IMREAD_UNCHANGED)读取的图像转换为cv2.imread('img.png',cv2.IMREAD_COLOR)的格式 - How do i convert an image read with cv2.imread('img.png',cv2.IMREAD_UNCHANGED) to the format of cv2.imread('img.png',cv2.IMREAD_COLOR) 如何在Python中由3个成员组成的组合数组中转换3个列表? - How do I convert 3 Lists in a combined array of 3 members in Python? 如何在 python 命令上打印一些带颜色的文本并更改背景? - How can I print some texts with color on python comand and change the background too? 我如何改变乌龟的颜色,而不是笔? - How do I change the color of the turtle, not the pen? 如何使用 Python 中的 Pillow 在 PNG 上叠加图案 - How do I overlay a pattern on a PNG using Pillow in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM