Thanks for posting this bug report.
> 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.
>
> Any ideas what's going wrong here?
Yes. Line endings.
PyPNG is writing to stdout which is opened in text mode. In text mode
on windows every '\n' character that is written will get converted to
CR LF in the result file. This is a disaster for binary files like
PNG.
It may be possible to reopen stdout so that it's in binary mode. It's
certainly possible to write to a different file instead of stdout
(possible as in, someone would need to write code to do it). I don't
have access to a Windows machine, so it's a bit difficult for me to
test.
David Jones
PyPNG maintainer
Nice.
If I was doing it then I'd probably adapt that recipe slightly:
# On Windows, try forcing stdout to be binary
try:
import msvcrt
import os
import sys
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
except:
# Not on an MSVCRT platform, nothing to do
pass
Nathan, if you try something like that out and provide a patch for
PyPNG I'll add it. Thanks!
drj
Lovely. Thanks for that. I'll add it (when i get round to it).
If you feel like you're likely to make any more changes, I can easily
add you as a developer to the googlecode project if you want.
drj