[英]How do I upload a png image in pygame as a sprite, without the black background?
[英]How do i change the background color in python of this combined png image?
我有以下代碼很好用,但它會產生黑色背景圖像,我希望它是白色的。 我試過改變 bg_color 但我改變它的任何東西仍然保持黑色背景。 還有一種方法可以在我組合的每個圖像周圍制作一個框嗎?
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')
原來我原來的 png 背景不是白色的,它是透明的所以我不得不將透明背景更改為白色,然后我才能更改背景顏色。
這是我如何將原始 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.