Tuesday, 15 July 2014

How do I write png hex values stored in a string to a png file in Ruby? -


I have a head value stored in the string but I want to write it in a new PNG file. I am currently using ruby ​​code below (with a small PGG example), but the hex values ​​are slightly off when I see the output in a new PNG file. ? Where am I going wrong

  png_hex = "89504e470d0a1a0a0000000d494844520000000d0000000d08030000004535144e0000000774494d4507d7041517171c5943ea76000000097048597300000b1100000b11017f645f910000000467414d410000b18f0bfc610500000015504c544500000086c1e01544706ba4da4a85c6386c9f427ab6583bfce90000000174524e530040e6d866000000434944415478da558d41120030040395c8ff9f5cd421dd0b6b02b3e198c24f1d22c72996bed1683c89ae3d2f4856ed6570795b317dec0da45aba04ad2e761cfb7a4228bb5cb30147915c409a0000000049454e44ae426082" File.open ( "temp.png", "w") {| FH | Fh.print png_hex.scan (/. {2} /) Map {| U | U.to_i (16)}. Use the pack ("C *"}}  

to open the file in binary mode For "w" for "wb" :

  "b" binary file mode is pressed EOL & lt ; - & gt; CRLF conversion on Windows and set external encoding to ASCII-8 BIT, unless explicitly specified.  

Anyway, you can create an array and 8-in- To write a bit integer you can call:

  file. Open ("temp.png", "wb") {| fh | Png_hex.scan (/. {2} /) { | E | Fh.putc (e.hex)}}  

No comments:

Post a Comment