简体   繁体   中英

Python 3 putpixel (PIL) object to string and reverse

I use PIL to read the pixel map of an image and I then store the map on a Postgre database VARCHAR column as a string. I would like to read that database VARCHAR string again from my Python script and transform it to the same pixel map object I get when I use pix=im.load() , so that I can then parse like this:

result I want after reading the database string eg

     mypixmap[0][0] = 255  mypixmap[1][0] = 255
     mypixmap[0][1] = 154  mypixmap[1][1] = 0
     mypixmap[0][2] = 120  mypixmap[1][1] = 30
     mypixmap[0][3] = 100  mypixmap[1][1] = 255

and so on...

The tuple list if printed completely with print(mypixmap) would result in somthing like this:

[(65, 65, 65, 255)(49, 49, 49, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(254, 254, 254, 255)(254, 254, 254, 255)(254, 254, 254, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(255, 255, 255, 255)(168, 168, 168, 255)(133, 133, 133, 255)(255, 255, 255, 255)]

but print(mypixmap[0][0]) in this case would give 65 without any brackets since it will retrieve the first integer.

I know the Postgre driver and SQL so I just need a way to store the object into the database (maybe VARCHAR is no good?) and get it back as it was before.

I hope to have been clear, I'm sorry if I did not use the proper terms but I'm still learning my first steps on Python.

I worked out my code by converting the mapping into String and .remove() all chars that are not the byte octets. When I store this string to the VARCHAR column I can then get it back, strip again everything but the octets and then use the string with the index[] to parse the octets.

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