Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python image processing of picture directly from the web

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.

like image 635
Jack Avatar asked Mar 09 '26 02:03

Jack


1 Answers

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)
like image 78
jsbueno Avatar answered Mar 10 '26 16:03

jsbueno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!