TAGS :Viewed: 6 - Published at: a few seconds ago

[ Process img from url using Python ]

How can i process image from url ? I am opening image from specific url and trying to process it .

    import PIL
    from PIL import ImageFont
    from PIL import Image
    from PIL import ImageDraw
    img = Image.open(url)
    ---Lines of code-- 

This giving me error : IOError: [Errno 22] invalid mode ('rb') or filename

Whats wrong i am doing here ?

Answer 1

It's a bit more convoluted than that. First you need to download the data using urllib. Then you store that data in a StringIO object. Then finally you open from that object in PIL:

import urllib
import StringIO
imgdata = urllib.urlopen(url).read()
img = Image.open(StringIO.StringIO(imgdata))