PythonAnywhere Forums

Loading image for manipulation from URL

Hi everyone,

Today I've had some serious issues with trying to load images into my views.py.

First of all Im on Django 17 and Python 3.4, apparently this means no urllib, urllib2, StringIO, cStringIO, BytesIO.

So I have requests and BeautifulSoup to work with. And also Pillow.

There is an image here: http://uploads.im/lyd0w.png

I'm trying to return the size:

rq = requests.get('http://uploads.im/lyd0w.png')
image = Image.open(rq)

#soup = BeautifulSoup(rq.content, 'html.parser')
#imgs = soup.select('img') (Didn't work)

sz = image.size
return render_to_response('crophop/imgur.html',{'link':sz})

Nothing has been working. I've seen loads and loads of examples with urllib and StringIO which is what makes it so frustrating.

I've spent hours today trying to make this work. Does anyone have an idea how I can either make the code work or get StringIO onto my system?

Thanks,

Fred

Solution:

Works using django17 and python 3.4

http://scikit-image.org/docs/dev/api/skimage.novice.html

Not using skimage - > io as a lot of other areas suggest.

Using a much easier way than urllib / string - array conversions:

from skimage import novice

picture = novice.open("URL")
wdth = picture.width (and anything else)

EASY AS THAT F Y I

Hope it helps someone else. I feel like something as simple as reading should be simple, and this novice is by far the best solution in so far as not needed a bunch of other libraries which may or may not with w/e other versions of stuff you have. I'm surprised it took me a whole day to find it, as well as there was not a single mention of it on anything stack exchange. Oh well.

Fred

cool!

Glad you worked it out :-)