[Image-SIG] Question from a newbie

Kevin Cazabon KCAZA@cymbolic.com
Thu, 17 Feb 2000 09:53:17 -0800


oops... forgot a little thing:  after the string.split(), you need to convert the
string values to integers before doing the array.

########
import string, Image, array

file = open(filename, 'r')

text = file.readlines()
file.close()
text = string.split(text) #defaults to splitting on any white space, returning a list
for i in range (len(text)): #this is the addition, sorry!
    text[i] = string.atoi(text[i])

data = array.array('B', text).tostring()
datasize = (320,240)
im = Image.fromstring('L', datasize, data)

im.save(yourfilename, "TIF")





>>> Kevin Cazabon <KCAZA@cymbolic.com> 02/17/00 09:41AM >>>
How about something simpler...

#########
import string, Image, array

file = open(filename, 'r')

text = file.readlines()
file.close()
text = string.split(text) #defaults to splitting on any white space, returning a list

data = array.array('B', text).tostring()
datasize = (320,240)
im = Image.fromstring('L', datasize, data)
###########


>>> "D. Lopez-De-Ipina" <dl231@eng.cam.ac.uk> 02/17/00 09:18AM >>>
Hi,

I have just started today having a look to PIL, I need to display an image
composed by a matrix of greyscale values saved to a file. The file
contains one line for each image pixel row and separates each pixel's
greyscale value by an space.

I have been trying for a while to modify the example Scripts/viewer.py to
create a new image using method Image.fromstring. However, I haven't had
much success yet. I know it must have been a very silly and simple mistake
but I can not see it. What I do is the following:

imageFile = open(filename)
    
data = array.array("i")
for iRow in range(240):
  dataLine = imageFile.readline()   
  for iCol in range(320):
    iPos = string.find(dataLine, " ")
    data.append(int(dataLine[:iPos]))
    dataLine = dataLine[iPos+1:]
im = Image.fromstring("L", (320,240), data )

imageFile.close()
    
UI(root, im).pack()

root.mainloop()

If anybody could point out what I have done wrong I would be very
grateful.

Regards,

Diego


_______________________________________________
Image-SIG maillist  -  Image-SIG@python.org 
http://www.python.org/mailman/listinfo/image-sig 
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                 

_______________________________________________
Image-SIG maillist  -  Image-SIG@python.org 
http://www.python.org/mailman/listinfo/image-sig