'Why are my composite PNGs black when using pillow?
For a final in one of my classes we basically had to make an NFT generator using pillow. I am a beginner with coding and so I googled some things and put together a code that would take random selections from PNGs I had made on photoshop and paste them on top of each other in random combinations. From what I can tell everything works fine, but the final image doesn't show any color, just black for filled areas and white for transparent ones. Its a very simple code so I will post most of it so you can see what I am talking about. Obviously y'all won't have all of my image files so I included only one of the dictionaries that reference them just so you know how I did it.
dict4 = {
"0": "C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/blue.png",
"1": "C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/green.png",
"2":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/orange.png",
"3":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/pink.png",
"4":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/purple.png",
"5":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/red.png",
"6":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/teal.png",
"7":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/yellow.png",
}
# List section where the random selection will come from
worm_eye_lst = [dict['0'],dict['1'],dict['2'],dict['3'],dict['4'],dict['5'],dict['6'],dict['7'],dict['8'],dict['9']]
worm_hat_lst = [dict2['0'],dict2['1'],dict2['2'],dict2['3'],dict2['4']]
worm_eyebrow_list = [dict3['0'],dict3['1'],dict3['2'],dict3['3'],dict3['4']]
worm_color_list = [dict4['0'],dict4['1'],dict4['2'],dict4['3'],dict4['4'],dict4['5'],dict4['6'],dict4['7']]
# Random selection function
def rand_attr(lst):
number_bank=len(lst)-1
choice = random.randint(0,number_bank)
return choice
# Function that puts all the pictures together
def composite_pic():
bg = Image.open("background.png")
cc=rand_attr(worm_color_list)
print(cc)
color_choice = worm_color_list[cc]
color = Image.open(color_choice)
bg.paste(color, (0, 0), mask = color)
base_worm = Image.open("base_worm.png")
bg.paste(base_worm, (0,0), mask = base_worm)
ec=rand_attr(worm_eye_lst)
print(ec)
eye_choice = worm_eye_lst[ec]
eye = Image.open(eye_choice)
bg.paste(eye, (0, 0), mask = eye)
hc = rand_attr(worm_hat_lst)
print(hc)
hat_choice= worm_hat_lst[hc]
hat = Image.open(hat_choice)
bg.paste(hat, (0, 0), mask = hat)
ebc = rand_attr(worm_eyebrow_list)
print(ebc)
eyebrow_choice = worm_eyebrow_list[ebc]
eyebrow = Image.open(eyebrow_choice)
bg.paste(eyebrow, (0,0), mask = eyebrow)
bg.show()
composite_pic()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
