PIL and requests don't get along

Discussion in 'Python' started by Roy Smith, Oct 23, 2012.

  1. Roy Smith

    Roy Smith Guest

    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?
     
    Roy Smith, Oct 23, 2012
    #1
    1. Advertisements

  2. Roy Smith

    Alex Clark Guest

    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/
     
    Alex Clark, Oct 23, 2012
    #2
    1. Advertisements

  3. 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/latest/user/quickstart/#binary-response-content

    --
    regards,
    kushal
     
    Kushal Kumaran, Oct 24, 2012
    #3
  4. Roy Smith

    Roy Smith Guest

    In article <>,
    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/latest/user/quickstart/#binary-response-con
    > tent


    Heh, I hadn't even noticed that. I guess this is as good as it gets.
     
    Roy Smith, Oct 24, 2012
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernando Arámburu

    web requests and mobile requests

    Fernando Arámburu, Apr 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    638
    Joerg Jooss
    Apr 8, 2005
  2. Kevin Smith

    PEP 318: Can't we all just get along?

    Kevin Smith, Aug 17, 2004, in forum: Python
    Replies:
    38
    Views:
    850
    Paul Morrow
    Aug 20, 2004
  3. Delaney, Timothy C (Timothy)

    RE: PEP 318: Can't we all just get along?

    Delaney, Timothy C (Timothy), Aug 20, 2004, in forum: Python
    Replies:
    7
    Views:
    339
    Colin J. Williams
    Aug 21, 2004
  4. jalkadir
    Replies:
    2
    Views:
    587
  5. Sverre
    Replies:
    2
    Views:
    4,298
    Sverre
    Dec 17, 2009
  6. Steven D'Aprano

    Doctests and decorated methods don't get along

    Steven D'Aprano, Feb 6, 2010, in forum: Python
    Replies:
    7
    Views:
    543
    Diez B. Roggisch
    Feb 9, 2010
  7. blacklight

    chroot() and popen() won't get along

    blacklight, Apr 15, 2010, in forum: C Programming
    Replies:
    12
    Views:
    991
  8. Lorenzo

    Stored Query and DATASETs don't get along ?

    Lorenzo, Jul 6, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    254
    Lorenzo
    Jul 14, 2005
Loading...