3

I am writing python code to take an image from the web and calculate the standard deviation, ... and do other image processing with it. I have the following code:

from scipy import ndimage  
from urllib2 import urlopen  
from urllib import urlretrieve  
import urllib2  
import Image  
import ImageFilter  

def imagesd(imagelist):  
 for imageurl in imagelist:  
     opener1 = urllib2.build_opener()  
     page1 = opener1.open(imageurl)  
     im = page1.read()  
     #localfile = urlretrieve(  
     #img = Image.fromstring("RGBA", (1,1), page1.read())  
     #img = list(im.getdata())  
     # page1.read()  
     print img  
     #standard_deviation(p   

Now I keep going back and forth because I am not sure how to take the image directly from the web, without saving it to disk, and passing it to the standard deviation function.

Any hints/help would be greatly appreciated.

Thanks.

1
  • 1
    ??? You have the image. You have unpacked it. You have a standard deviation function. Why would you need to save it to disk? What, exactly, is the question? Commented Jan 27, 2010 at 10:14

1 Answer 1

4

PIL (Python Imaging Library) methods "fromstring" and "frombuffer" expect the image data in a raw, uncompacted, format. When you do page1.read() you get the binary file data. In order to have PIL understanding it, you have to make this data mimick a file, and pass it to the "Image.open" method, which understands the file format as it is read from the web (i.e., the .jpg, gif, or .png data instead of raw pixel values)

Try something like this:

from cStringIO import StringIO
(...)
    data = StringIO(page1.read())
    img = Image.open(data)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.