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. Advertising

  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. Advertising

  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. Advertising

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:
    432
    Joerg Jooss
    Apr 8, 2005
  2. Sverre
    Replies:
    2
    Views:
    3,517
    Sverre
    Dec 17, 2009
  3. Steven D'Aprano

    Doctests and decorated methods don't get along

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

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

    blacklight, Apr 15, 2010, in forum: C Programming
    Replies:
    12
    Views:
    700
  5. Lorenzo

    Stored Query and DATASETs don't get along ?

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

Share This Page