Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting RGBA values from .png in Python

For a project I need to get the RGBA values from an image and use them in later calculations. To do this, I have been using im.getdata() and because this gives me the values in the format of lists within a list I wrote some code to put it into one long list instead.

for group in pix_val:
    for item in group:
        pix_val_flat.append(item)

This worked perfectly well for me until I tested the code with an entirely white image. Instead of producing sets of 4 values within a list, it just gave me one long list - which means I get an error when my program tries to flatten the list. Anyone understand why this happens and how I could fix it?

This is my code:

im=Image.open("/filelocation")
pix_val=list(im.getdata())
pix_val_flat=[]
for group in pix_val:
    for item in group:
        pix_val_flat.append(item)

This is the error message:

Traceback (most recent call last):
    File "/program", line 286 in <module>
        for item in group:
    TypeError: 'int' object is not iterable
like image 369
Natasha Blakemore Avatar asked Oct 22 '25 10:10

Natasha Blakemore


1 Answers

Edited this line of my code and it now works:

im=Image.open("/filelocation").convert('RGBA')
like image 139
Natasha Blakemore Avatar answered Oct 24 '25 01:10

Natasha Blakemore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!