diegorep / pypng

Automatically exported from code.google.com/p/pypng
0 stars 0 forks source link

Problem writing PNG files on Windows #62

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
On Windows XP with Python 2.7 (standard install from python.org), when
I try to convert PPM to PNG it seems that PyPNG is generating
malformed PNG files (they're not accepted by GIMP, ImageMagick's
convert, Preview.app, etc.).   I've put a specific example of the
problem at

http://dunfield.info/temp/pypng

On Windows, Mac OS X, and Linux, I did the following:

python png.py problem.ppm > problem-PLATFORM.png

On OS X and LInux, the resulting PNG files "problem-mac.png" and
"problem-linux.png" are fine (and also identical).  Moreover, when I
used "convert" to turn it back into a PPM file, the result was even
identical to the original problem.ppm.

On the other hand, on Windows the resulting PNG file "problem-XP.png"
has slightly different length and seems to have a bad header.   In
particular, when I try to run it through convert, I get:

convert problem-XP.png /tmp/t.ppm
convert: improper image header `problem-XP.png' @ error/png.c/
ReadPNGImage/3020.
convert: missing an image filename `/tmp/t.ppm' @ error/convert.c/
ConvertImageCommand/2970.

Original issue reported on code.google.com by nathan.m...@gmail.com on 2 Jul 2011 at 2:18

Attachments:

GoogleCodeExporter commented 8 years ago
I suspect the ppm file is irrelevant.  Any PPM (or PGM) will result in broken 
output on Windows.

The problem is probably that on Windows stdout is opened in text mode so the 
binary file that PyPNG writes to it gets corrupted.  The PNG format 
deliberately has a newline in it's header so that exactly this problem can be 
detected early when reading a broken PNG.

On windows, we need to convert sys.stdout from text to binary mode.  This is 
not possible portably, but there may be some windows-specific hack.  I do not 
have access to a Windows machine. :)

(it's probably opened in text mode on OS X and Linux too but it makes no 
difference)

Original comment by drj...@googlemail.com on 20 Apr 2012 at 10:00

GoogleCodeExporter commented 8 years ago
Here's a decent hack for this:

http://stackoverflow.com/questions/2374427/python-2-x-write-binary-output-to-std
out

I tested it on a Windows machine with the original example and it works fine 
under Python 2.7.  Attached is a modified version of png.py with this fix and 
its diff against the current version of the repository.  

Original comment by nathan.m...@gmail.com on 20 Apr 2012 at 4:48

Attachments:

GoogleCodeExporter commented 8 years ago
Applied as revision 237.  Thanks!

Original comment by drj...@googlemail.com on 25 Apr 2012 at 12:05

GoogleCodeExporter commented 8 years ago
Great!

Original comment by nathan.m...@gmail.com on 25 Apr 2012 at 12:12

GoogleCodeExporter commented 8 years ago

Original comment by drj...@googlemail.com on 1 Dec 2012 at 11:33