简体   繁体   中英

Transform a list into a readable PNG file -Python

I have to convert an image spelled like this:

[[[R,G,B],[R,G,B],[R,G,B]],[[R,G,B],[R,G,B],[R,G,B]]]

In one of those 3 PNG format)

Boxed row flat pixel :

 list([R,G,B, R,G,B, R,G,B],
   [R,G,B, R,G,B, R,G,B])

Flat row flat pixel:

'I didn't undersand this one'

 [R,G,B, R,G,B, R,G,B,
  R,G,B, R,G,B, R,G,B]

Boxed row boxed pixel:

 list([ (R,G,B), (R,G,B), (R,G,B) ],
      [ (R,G,B), (R,G,B), (R,G,B) ])

I choose the 'Boxed row pixel' format, cause I think the conversion to this format can be faster (Im not sure, let me know)

So i define a function to convert the list of lists in a tuple of lists:

def convert(list):
return (*list, )

And from [[[R,G,B],[R,G,B],[R,G,B]],[[R,G,B],[R,G,B],[R,G,B]]] I now have ([[R,G,B],[R,G,B],[R,G,B]],[[R,G,B],[R,G,B],[R,G,B]])

Now what i need is to whrite a function that transform this: ([[R,G,B],[R,G,B],[R,G,B]],[[R,G,B],[R,G,B],[R,G,B]]) into ([(R,G,B),(R,G,B),(R,G,B)],[(R,G,B),(R,G,B),(R,G,B)])

I tryed several think but i ended up with a lot of error.

We can simply use the built-in tuple() function for this, for each rgb list in the tuple:

rgb_tuple = ([[R,G,B], [R,G,B], [R,G,B]], [[R,G,B], [R,G,B], [R,G,B]])
for item_idx, item in enumerate(rgb_tuple):
    for rgb_idx, rgb in enumerate(item):
        rgb_tuple[item_idx][rgb_idx] = tuple(rgb_tuple[item_idx][rgb_idx])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM