Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > PIL and requests don't get along

Reply
Thread Tools

PIL and requests don't get along

 
 
Roy Smith
Guest
Posts: n/a
 
      10-23-2012
I have a url from which I can get an image. I want to use PIL to
manipulate that image. Getting the image is easy:

>>> import requests
>>> r = requests.get(url)


There's a bunch of factory functions for Image, but none of them seem
to take anything that requests is willing to give you. Image.new()
requires that you pass it the image size. Image.open() takes a file
object, but

>>> Image.open(r.raw)


doesn't work because r.raw gives you a socket which doesn't support
seek(). I end up doing:

>>> r = requests.get(url)
>>> data = cStringIO.StringIO(r.content)
>>> image = Image.open(data)


which works, but it's gross. Is there something I'm missing here?
 
Reply With Quote
 
 
 
 
Alex Clark
Guest
Posts: n/a
 
      10-23-2012
On 2012-10-23 18:06:59 +0000, Roy Smith said:

> I have a url from which I can get an image. I want to use PIL to
> manipulate that image. Getting the image is easy:
>
>>>> import requests
>>>> r = requests.get(url)

>
> There's a bunch of factory functions for Image, but none of them seem
> to take anything that requests is willing to give you. Image.new()
> requires that you pass it the image size. Image.open() takes a file
> object, but
>
>>>> Image.open(r.raw)

>
> doesn't work because r.raw gives you a socket which doesn't support
> seek(). I end up doing:
>
>>>> r = requests.get(url)
>>>> data = cStringIO.StringIO(r.content)
>>>> image = Image.open(data)

>
> which works, but it's gross. Is there something I'm missing here?



No idea but you can open a ticket here if you think it's appropriate:
https://github.com/python-imaging/Pillow/issues


--
Alex Clark · https://www.gittip.com/aclark4life/


 
Reply With Quote
 
 
 
 
Kushal Kumaran
Guest
Posts: n/a
 
      10-24-2012
On 23 Oct 2012 14:06:59 -0400, (Roy Smith) wrote:
> I have a url from which I can get an image. I want to use PIL to
> manipulate that image. Getting the image is easy:
>
> >>> import requests
> >>> r = requests.get(url)

>
> There's a bunch of factory functions for Image, but none of them seem
> to take anything that requests is willing to give you. Image.new()
> requires that you pass it the image size. Image.open() takes a file
> object, but
>
> >>> Image.open(r.raw)

>
> doesn't work because r.raw gives you a socket which doesn't support
> seek(). I end up doing:
>
> >>> r = requests.get(url)
> >>> data = cStringIO.StringIO(r.content)
> >>> image = Image.open(data)

>
> which works, but it's gross. Is there something I'm missing here?


That is pretty much what the requests module documentation says here:

http://docs.python-requests.org/en/l...sponse-content

--
regards,
kushal
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      10-24-2012
In article <mailman.2725.1351058163.27098.python->,
Kushal Kumaran <kushal.kumaran+> wrote:

> On 23 Oct 2012 14:06:59 -0400, (Roy Smith) wrote:
> > I have a url from which I can get an image. I want to use PIL to
> > manipulate that image. Getting the image is easy:
> >
> > >>> import requests
> > >>> r = requests.get(url)

> >
> > There's a bunch of factory functions for Image, but none of them seem
> > to take anything that requests is willing to give you. Image.new()
> > requires that you pass it the image size. Image.open() takes a file
> > object, but
> >
> > >>> Image.open(r.raw)

> >
> > doesn't work because r.raw gives you a socket which doesn't support
> > seek(). I end up doing:
> >
> > >>> r = requests.get(url)
> > >>> data = cStringIO.StringIO(r.content)
> > >>> image = Image.open(data)

> >
> > which works, but it's gross. Is there something I'm missing here?

>
> That is pretty much what the requests module documentation says here:
>
> http://docs.python-requests.org/en/l...y-response-con
> tent


Heh, I hadn't even noticed that. I guess this is as good as it gets.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Doctests and decorated methods don't get along Steven D'Aprano Python 7 02-09-2010 08:40 AM
PIL: problem to convert an image array to PIL format Sverre Python 2 12-17-2009 04:33 PM
Wireless and x64 don't get along =?Utf-8?B?dWljb21hNw==?= Windows 64bit 11 01-09-2006 11:30 AM
web requests and mobile requests Fernando Arámburu ASP .Net 1 04-08-2005 07:13 PM
[PIL] is there a downloadable docs for PIL Egor Bolonev Python 2 12-24-2004 11:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57