Username: 
Password: 
Restrict session to IP 

Python3 - libpng - PNG manipulations

Global Rank: 183
Totalscore: 110766
Posts: 88
Thanks: 78
UpVotes: 91
Registered: 9y 197d
occasus`s Avatar



Last Seen: 11d 6h
The User is Offline
Python3 - libpng - PNG manipulations
Google/translate2Thank You!2Good Post!0Bad Post! link
Hello all Dear Challengers,
after spending too many weeks on the libpng.org website and many other resources like StackOverflow, Python programming challenges and in all corners of the web I managed to put hands on, I thought: why not also post here? Talented people are everywhere.

Aim: my goal is becoming able to manipulate raw PNG data. I attached here a 3x3 png. And I would like with PIL to "recycle", "modify" and "recreate" the 3x3 png. As far as I understand, in an RGB there are 3 bytes + 1 byte (the filter) every row.

Avoiding this post becoming too long or annoying, I would like to show my thoughts:

I download the image locally. After inspection with png-tools like pngcheck and further we will see something like:

GeSHi`ed text code
1
2
3
4
56
7
8
9
10
asdfasdf$ pngcheck -7cfpqstvvw test.png
Scanning: test.png
File: test-1
  chunk IHDR at offset 0x0000c, length 13
    3 x 3 image, 24-bit RGB, non-interlaced  chunk IDAT at offset 0x00025, length 17
    zlib: deflated, 256-byte window, maximum compression
    row filters (0 none, 1 sub, 2 up, 3 avg, 4 paeth):
      1 0 0 (3 out of 3)
  chunk IEND at offset 0x00042, length 0


With tweakpng we can error-free export the IDAT chunk. From this we remove the first 4 bytes ('IDAT'). Then with cyberchef we inflate and download the output bytes as file. So we have the raw-png-data containing pixel triplets values and single filter bytes.

Here the steps and the bytes:
GeSHi`ed text code
1
2
3
4
56
74 test.png
21 test.chunk
17 test.zlib   # 21-4=17 (removed the IDAT tag)
30 test.dat    # 3 * 3 = 9 pixels
               # 9 * 3 = 27 (R,G,B) bytes               # 27 + 3 = 30 filter byte


We see the bytes in the following order. Let's say I would like to put the bottom-right corner BLUE pixel, and move it on top-left corner instead of the white one. Then after modifying tuples / lists etc. I would like to feed PIL as in all my scripts from the past in order to view the new image. Which is the best way to go on

Most important, usually in scripts I give PIL only pixel values, after long research I were not able to find an article which explains how to tell PIL that the data I'm giving to him contains every nth a "special" filter byte byte.

GeSHi`ed text code
1
2
3
4
5
01 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff 00 00 00 ff 00 00 00 ff
 
{01} (ff, ff, ff) (00, 00, 00) (00, 00, 00)
{00} (00, 00, 00) (00, 00, 00) (00, 00, 00)
{00} (ff, 00, 00) (00, ff, 00) (00, 00, ff)


As you see, now my problems begin. I hope someone find this thread interesting because it could help also others who doesn't find the right articles after weeks... Happy I really desire improve my little PIL knowledge. By the way, as already stated I really put much efforts and many times the nights were just passed by... so the days become 72hours long during intense reads Smile

Sincerely Yours
Size: 328B
Hits: 217
Global Rank: 1
Totalscore: 760035
Posts: 431
Thanks: 491
UpVotes: 456
Registered: 14y 246d












The User is Offline
RE: Python3 - libpng - PNG manipulations
Google/translate2Thank You!3Good Post!0Bad Post! link
Why would you expect that you can tell PIL such a thing? It is something specific to the PNG file format; you can't just strip an arbitrary amount from the file and expect libraries to still read it.

Also, if you could and you would give it the unmodified data, you cannot expect it to produce the original PNG file from it. It can choose to use different filters, compression options, PNG chunks etc.

If you want to manipulate the raw PNG, then you need something that supports that. Perhaps you can find a library for it (not sure), but I would suggest to write some code of your own to do it. The PNG format is relatively simple and well documented. Extracting the data you got with the various tools requires only a few lines of Python code, really. Putting it back together after making some modifications is not that much harder.

If you want to use the data in PIL, you'll probably just have to do that one extra step of applying the filters to get the actual RGB data.
Global Rank: 183
Totalscore: 110766
Posts: 88
Thanks: 78
UpVotes: 91
Registered: 9y 197d
occasus`s Avatar



Last Seen: 11d 6h
The User is Offline
RE: Python3 - libpng - PNG manipulations
Google/translate1Thank You!1Good Post!0Bad Post! link
Hi dear dloser,
thank you for your reply. You're always present Happy ... yep, I shouldn't expect too much from PIL. Since it is a very large maze, I thought it would be possible with a specific method. Indeed your thought is totally correct.

Concerning libraries, I see there exist PyPNG. Your suggestion is well accepted, in fact I'm decisively eager to learn, understand and solve.

Quote from dloser
Extracting the data you got with the various tools requires only a few lines of Python code, really. Putting it back together after making some modifications is not that much harder.

If you or anyone would like to show some examples, which already exist online (didn't found them, yet) or own examples I would be very greatful. In any case I don't need this for real life therefore not that important. But I fell in love playing these challenges, instead of console games... that's it, that's all.

Kinedest Regards
Global Rank: 1
Totalscore: 760035
Posts: 431
Thanks: 491
UpVotes: 456
Registered: 14y 246d












The User is Offline
RE: Python3 - libpng - PNG manipulations
Google/translate1Thank You!2Good Post!0Bad Post! link
Try it yourself first. See how far you can get on your own. ;)
tunelko, quangntenemy, TheHiveMind, Z, balicocat, Ge0, samuraiblanco, arraez, jcquinterov, hophuocthinh, alfamen2, burhanudinn123, Ben_Dover, stephanduran89, braddie0, SwolloW, dangarbri have subscribed to this thread and receive emails on new posts.
1 people are watching the thread at the moment.
This thread has been viewed 5260 times.