[IMAGE-SIG] fromstring/tostring problem
Andrew Kuchling
amk@magnet.com
Thu, 17 Jul 1997 12:08:10 -0400 (EDT)
This is almost certainly due to stupidity on my part, but here goes...
what's going on in this test program? It creates a little test image,
converts it to a string form with tostring(), and creates a new image
with Image.fromstring(), but the two images don't match. What am I
missing? Do fromstring() and tostring() work in opposite directions
vertically?
import Image, ImageDraw, string
# Create a blank 25x25 image
im=Image.new ('L', (25,25), 0 )
# Fill in lines 10 through 20
draw=ImageDraw.ImageDraw(im)
draw.setink(255)
for i in range(10,21): draw.line( (0,i), (24,i) )
im.show()
# Convert to string
data=im.tostring()
# Prints 125, 374 which is 5*25, 15*25-1. Shouldn't it be 10*25, or 250?
print string.find(data, '\377'), string.rfind(data, '\377'), len(data)
# This image should be identical to im, right?
im2=Image.fromstring('L', (25,25), data)
# It's not, as the following printout shows
for i in range(0, 25):
print i, im.getpixel( (0,i) ), im2.getpixel( (0,i) )
im2.show()
The output of this script is:
125 374 625 << 5*25, 15*25-1, 25*25
0 0 0
1 0 0
2 0 0
3 0 0
4 0 0
5 0 255 << Mismatch!
6 0 255
7 0 255
8 0 255
9 0 255
10 255 255
11 255 255
12 255 255
13 255 255
14 255 255
15 255 0
16 255 0
17 255 0
18 255 0
19 255 0
20 0 0
21 0 0
22 0 0
23 0 0
24 0 0
Andrew Kuchling
amk@magnet.com
http://people.magnet.com/%7Eamk/
_______________
IMAGE-SIG - SIG on Image Processing with Python
send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org
_______________