PIL: reading bytes from Image

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cyberco

    #1

    PIL: reading bytes from Image

    I'm using web.py to send an image to the client. This works
    (shortened):

    print open(path, "rb").read( )

    but this doesn't:

    img = Image.open(path )
    img.thumbnail(( 10,10))
    print img.getdata()

    or

    print img.load()


    How do I get the bytes of the Image object? 'getdata()' seemed the
    way, but unfortunately.. .

  • Max Erickson

    #2
    Re: PIL: reading bytes from Image

    "cyberco" <cyberco@gmail. comwrote:
    I'm using web.py to send an image to the client. This works
    (shortened):
    >
    print open(path, "rb").read( )
    >
    but this doesn't:
    >
    img = Image.open(path )
    img.thumbnail(( 10,10))
    print img.getdata()
    >
    or
    >
    print img.load()
    >
    >
    How do I get the bytes of the Image object? 'getdata()' seemed the
    way, but unfortunately.. .
    >
    getdata() returns the data in a PIL format. Saving the image, in a
    format your client understands, to a file like object like
    StringIO.String IO is an easy path to take.

    Sparklines shows this in action:

    Joe Gregorio - REST, Web, Go, APIs, Dad, Husband, Maker, or any linear combination of such. Googler.



    max

    Comment

    • cyberco

      #3
      Re: PIL: reading bytes from Image

      Thanks,

      I've tried the StringIO option as follows:

      =============== =============== ===
      img = Image.open('/some/path/img.jpg')
      img.thumbnail(( 640,480))
      file = StringIO, StringIO()
      img.save(file, 'JPEG')

      =============== =============== ===

      But it gives me:

      =============== =============== ===

      exceptions.Type Error Traceback (most
      recent call last)

      C:\Python24\lib \site-packages\PIL\Im age.py in save(self, fp, format,
      **params)
      1303
      1304 try:
      -1305 save_handler(se lf, fp, filename)
      1306 finally:
      1307 # do what we can to clean up

      C:\Python24\lib \site-packages\PIL\Jp egImagePlugin.p y in _save(im, fp,
      filename)
      407 )
      408
      --409 ImageFile._save (im, fp, [("jpeg", (0,0)+im.size, 0,
      rawmode)])
      410
      411 def _save_cjpeg(im, fp, filename):

      C:\Python24\lib \site-packages\PIL\Im ageFile.py in _save(im, fp, tile)
      464 bufsize = max(MAXBLOCK, im.size[0] * 4) # see RawEncode.c
      465 try:
      --466 fh = fp.fileno()
      467 fp.flush()
      468 except AttributeError:

      TypeError: descriptor 'fileno' of 'file' object needs an argument
      =============== =============== ===

      It looks similar to the code in Sparklines except for the fact that
      the latter creates an Image from scratch.

      2B
      Saving the image, in a
      format your client understands, to a file like object like
      StringIO.String IO is an easy path to take.
      >
      Sparklines shows this in action:
      >
      Joe Gregorio - REST, Web, Go, APIs, Dad, Husband, Maker, or any linear combination of such. Googler.

      >
      max

      Comment

      • Max Erickson

        #4
        Re: PIL: reading bytes from Image

        "cyberco" <cyberco@gmail. comwrote:
        Thanks,
        >
        I've tried the StringIO option as follows:
        >
        =============== =============== ===
        img = Image.open('/some/path/img.jpg')
        img.thumbnail(( 640,480))
        file = StringIO, StringIO()
        Is the above line exactly what you tried? If it is, the comma and
        space in the line are likely the problem. Try either:

        from StringIO import StringIO
        file=StringIO()

        or

        import StringIO
        file=StringIO.S tringIO()

        (using file as a variable name can cause issues if you later want
        access to the built in object 'file')

        The following:
        >>import Image
        >>import StringIO
        >>im=Image.new( 'RGB', (100,100))
        >>fp=StringIO.S tringIO()
        >>im.save(fp, 'jpeg')
        >>len(fp.getval ue())
        823

        Works for me on python 2.5 on windows.


        max

        Comment

        • Larry Bates

          #5
          Re: PIL: reading bytes from Image

          cyberco wrote:
          Thanks,
          >
          I've tried the StringIO option as follows:
          >
          =============== =============== ===
          img = Image.open('/some/path/img.jpg')
          img.thumbnail(( 640,480))
          file = StringIO, StringIO()
          img.save(file, 'JPEG')
          >
          =============== =============== ===
          >
          But it gives me:
          >
          =============== =============== ===
          >
          exceptions.Type Error Traceback (most
          recent call last)
          >
          C:\Python24\lib \site-packages\PIL\Im age.py in save(self, fp, format,
          **params)
          1303
          1304 try:
          -1305 save_handler(se lf, fp, filename)
          1306 finally:
          1307 # do what we can to clean up
          >
          C:\Python24\lib \site-packages\PIL\Jp egImagePlugin.p y in _save(im, fp,
          filename)
          407 )
          408
          --409 ImageFile._save (im, fp, [("jpeg", (0,0)+im.size, 0,
          rawmode)])
          410
          411 def _save_cjpeg(im, fp, filename):
          >
          C:\Python24\lib \site-packages\PIL\Im ageFile.py in _save(im, fp, tile)
          464 bufsize = max(MAXBLOCK, im.size[0] * 4) # see RawEncode.c
          465 try:
          --466 fh = fp.fileno()
          467 fp.flush()
          468 except AttributeError:
          >
          TypeError: descriptor 'fileno' of 'file' object needs an argument
          =============== =============== ===
          >
          It looks similar to the code in Sparklines except for the fact that
          the latter creates an Image from scratch.
          >
          2B
          >
          >Saving the image, in a
          >format your client understands, to a file like object like
          >StringIO.Strin gIO is an easy path to take.
          >>
          >Sparklines shows this in action:
          >>
          >http://bitworking.org/projects/sparklines/
          >>
          >max
          >
          >
          If it hasn't already bitten you it will: It is a BAD idea to use
          'file' as a variable name. If you do, you mask the built-in file
          function. That is why most people use fp or something else.

          -Larry

          Comment

          Working...