How to write pbm file in Python 3?

A

André

I'm trying to create pbm (portable bitmap) files using Python 3 and
have run into a problem. The example I am using is the Python 2
Mandelbrot program found at
http://shootout.alioth.debian.org/u32/program.php?test=mandelbrot&lang=python&id=1

When I run it using Python 2 with size=100, I get a file of size 1311
bytes. This file contains the correct image.

When I run it using Python 3 with the same parameter (and replacing
xrange by range - the only change suggested by 2to3), I get a file of
size 1812 bytes; this file does not contain the right image.

Any help would be much appreciated.

André
 
C

Chris Rebert

I'm trying to create pbm (portable bitmap) files using Python 3 and
have run into a problem.   The example I am using is the Python 2
Mandelbrot program found at
http://shootout.alioth.debian.org/u32/program.php?test=mandelbrot&lang=python&id=1

When I run it using Python 2 with size=100, I get a file of size 1311
bytes.  This file contains the correct image.

When I run it using Python 3 with the same parameter (and replacing
xrange by range - the only change suggested by 2to3), I get a file of
size 1812 bytes; this file does not contain the right image.

Any help would be much appreciated.

Have you tried changing all instances of chr(<whatever>) to
bytes([<whatever>]) ?
In Python 3, chr() always returns Unicode rather than a bytestring; I
suspect this might be the cause of your problem.

If my suggestion fixes things, then arguably 2to3 ought to have warned
about chr() and thus you should report this as a bug.

Cheers,
Chris
 
A

André

I'm trying to create pbm (portable bitmap) files using Python 3 and
have run into a problem.   The example I am using is the Python 2
Mandelbrot program found at
http://shootout.alioth.debian.org/u32/program.php?test=mandelbrot&lan....
When I run it using Python 2 with size=100, I get a file of size 1311
bytes.  This file contains the correct image.
When I run it using Python 3 with the same parameter (and replacing
xrange by range - the only change suggested by 2to3), I get a file of
size 1812 bytes; this file does not contain the right image.
Any help would be much appreciated.

Have you tried changing all instances of chr(<whatever>) to
bytes([<whatever>]) ?
Yes, I had. And I got the following traceback:

Traceback (most recent call last):
File "mandel_orig.py", line 39, in <module>
main()
File "mandel_orig.py", line 30, in main
cout(bytes([byte_acc]))
TypeError: must be str, not bytes

André
 
C

Chris Rebert

I'm trying to create pbm (portable bitmap) files using Python 3 and
have run into a problem.   The example I am using is the Python 2
Mandelbrot program found at
http://shootout.alioth.debian.org/u32/program.php?test=mandelbrot&lan....
When I run it using Python 2 with size=100, I get a file of size 1311
bytes.  This file contains the correct image.
When I run it using Python 3 with the same parameter (and replacing
xrange by range - the only change suggested by 2to3), I get a file of
size 1812 bytes; this file does not contain the right image.
Any help would be much appreciated.

Have you tried changing all instances of chr(<whatever>) to
bytes([<whatever>]) ?
Yes, I had.  And I got the following traceback:

Traceback (most recent call last):
 File "mandel_orig.py", line 39, in <module>
   main()
 File "mandel_orig.py", line 30, in main
   cout(bytes([byte_acc]))
TypeError: must be str, not bytes

Ah, right. This is the problem of not having Python 3 to test with.
Try also changing:
cout = sys.stdout.buffer.write

Cheers,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top