Problem on Windows

70 views
Skip to first unread message

Nathan Dunfield

unread,
Jul 1, 2011, 10:16:26 PM7/1/11
to PyPNG Users
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.

Any ideas what's going wrong here?

Thanks,

Nathan

David Jones

unread,
Jul 4, 2011, 3:59:29 AM7/4/11
to pypng...@googlegroups.com
On 2 July 2011 03:16, Nathan Dunfield <nat...@dunfield.info> wrote:
> 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

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

Nathan Dunfield

unread,
Jul 5, 2011, 6:42:21 PM7/5/11
to PyPNG Users
> 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,

Thanks for the quick response. I was trying to use PyPNG in one of
my own programs, but I reported the problem using png.py as a script
to cut down on the possible sources of error. From your response,
I
see that my problem was that I wasn't opening the file in binary
mode;
since I switched to that, it works fine.

Also, I think that

http://code.activestate.com/recipes/65443-sending-binary-data-to-stdout-under-windows/

gives an easy way to fix png.py in script mode on Windows. I'll
try
it out and get back to you if it works.

Best,

Nathan

David Jones

unread,
Jul 6, 2011, 3:24:36 AM7/6/11
to pypng...@googlegroups.com
On 5 July 2011 23:42, Nathan Dunfield <nat...@dunfield.info> wrote:
>> Yes.  Line endings.

>>
> Also, I think that
>
> http://code.activestate.com/recipes/65443-sending-binary-data-to-stdout-under-windows/
>
> gives an easy way to fix png.py in script mode on Windows.   I'll
> try
> it out and get back to you if it works.

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

David Jones

unread,
Jul 6, 2011, 3:25:24 AM7/6/11
to pypng...@googlegroups.com
[oops, pressed send slightly to early]

Nathan, if you try something like that out and provide a patch for
PyPNG I'll add it. Thanks!

drj

Nathan Dunfield

unread,
Jul 11, 2011, 6:06:43 AM7/11/11
to PyPNG Users

> Nathan, if you try something like that out and provide a patch for
> PyPNG I'll add it.  


David,

I tried it out, and this method does indeed make png.py work on
Windows as a script. Below is the output of "svn diff" with this fix.

Best,

Nathan

Index: code/png.py
===================================================================
--- code/png.py (revision 229)
+++ code/png.py (working copy)
@@ -3726,7 +3726,13 @@
else:
parser.error("more than one input file")
outfile = sys.stdout
-
+ # On Windows, stdout needs to be used in binary mode
+ try:
+ import msvcrt, os
+ msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
+ except ImportError:
+ pass # Nothing to do on other OSs.
+
if options.read_png:
# Encode PNG to PPM
png = Reader(file=infile)

David Jones

unread,
Jul 11, 2011, 11:02:08 AM7/11/11
to pypng...@googlegroups.com
On 11 July 2011 11:06, Nathan Dunfield <nat...@dunfield.info> wrote:
>
> David,
>
> I tried it out, and this method does indeed make png.py work on
> Windows as a script.  Below is the output of "svn diff" with this fix.

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

Reply all
Reply to author
Forward
0 new messages