Re: Problem Displaying Pics

Discussion in 'Python' started by Gabriel Genellina, Oct 7, 2009.

  1. En Tue, 06 Oct 2009 17:26:19 -0300, Victor Subervi
    <> escribió:

    > The code in question is generated automatically from another script. I
    > took
    > your idea of the \r\n\r\n and added triple quoting and now it prints out
    > this:


    That's still wrong. The output should be:

    - a line containing Content-Type: image/jpeg
    - a blank line (no more!)
    - the actual image data

    A Content-Type of text/html is just *wrong* for an image...

    --
    Gabriel Genellina
    Gabriel Genellina, Oct 7, 2009
    #1
    1. Advertising

  2. En Wed, 07 Oct 2009 12:00:13 -0300, Victor Subervi
    <> escribió:

    > print '''Content-Type: image/jpeg
    >
    > '''
    > print
    > print content


    On Wed, Oct 7, 2009 at 9:51 AM, Gabriel Genellina <gagsl-
    >wrote:

    > That's still wrong. The output should be:
    >
    > - a line containing Content-Type: image/jpeg
    > - a blank line (no more!)

    ^^^^^^^^^^^^^^^^^^^^^^^^^
    > - the actual image data


    --
    Gabriel Genellina
    Gabriel Genellina, Oct 7, 2009
    #2
    1. Advertising

  3. Victor Subervi wrote:
    > [...]
    > print '''Content-Type: image/jpeg
    >
    > Content-Encoding: base64
    > '''
    > [...]


    You have a spurious blank line between those header lines.

    HTH,

    --
    Carsten Haese
    http://informixdb.sourceforge.net
    Carsten Haese, Oct 7, 2009
    #3
  4. On Wed, 07 Oct 2009 13:24:28 -0700, Victor Subervi
    <> wrote:

    > I did that. In fact, just to make things easier, I wrote out exactly
    > what is
    > supposed to be rendered, as below:
    >
    > #!/usr/local/bin/python
    > import cgitb; cgitb.enable()
    > import MySQLdb
    > import cgi
    > import sys,os
    > sys.path.append(os.getcwd())
    > from login import login
    > user, passwd, db, host = login()
    > form = cgi.FieldStorage()
    > db = MySQLdb.connect(host, user, passwd, db)
    > cursor= db.cursor()
    > sql = "select pic1 from products where id='1';"
    > cursor.execute(sql)
    > content = cursor.fetchall()[0][0].tostring()
    > cursor.close()
    > print '''Content-Type: image/jpeg''' # Only print one line
    >
    > Content-Encoding: base64
    > '''
    > print
    > print content.encode('base64')
    >


    Just to reiterate what Gabriel and Carsten have pointed out, I'd suggest
    changing the last few lines of the script, as the comments below indicate:

    print '''Content-Type: image/jpeg''' # One header line
    print '''Content-Encoding: base64''' # Another header line. Note *no*
    blank line between them
    print # Blank line signals the end of the headers
    print content.encode(base64) # Base64-encoded content comes *after*
    the blank line

    If you include extra blank lines after the headers, the browser will
    misunderstand where the binary image data begins, and so will see a
    malformed JPEG.

    If this doesn't work, I suggest changing the content-type header to
    text/plain and trying to manually decode and check the image data to
    ensure it's valid.

    >
    > On Wed, Oct 7, 2009 at 2:47 PM, Gabriel Genellina
    > <>wrote:
    >
    >> En Wed, 07 Oct 2009 12:00:13 -0300, Victor Subervi
    >> <> escribió:
    >>
    >> > print '''Content-Type: image/jpeg
    >> >
    >> > '''
    >> > print
    >> > print content

    >>
    >> On Wed, Oct 7, 2009 at 9:51 AM, Gabriel Genellina <gagsl-
    >> >wrote:
    >>
    >> > That's still wrong. The output should be:
    >> >
    >> > - a line containing Content-Type: image/jpeg
    >> > - a blank line (no more!)

    >> ^^^^^^^^^^^^^^^^^^^^^^^^^
    >> > - the actual image data

    >>
    >> --
    >> Gabriel Genellina
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>




    --
    Rami Chowdhury
    "Never attribute to malice that which can be attributed to stupidity" --
    Hanlon's Razor
    408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
    Rami Chowdhury, Oct 7, 2009
    #4
  5. On Wed, 07 Oct 2009 14:05:25 -0700, Victor Subervi
    <> wrote:

    > print 'Content-Type: image/jpeg'
    > print 'Content-Encoding: base64'
    > print
    > print content.encode('base64')
    >
    > I did change it to text/plain, but I don't know how I'm supposed to
    > manually
    > decode it. Yes, it printed out a bunch of crap to the screen. I've gotten
    > that far before, and once I read "Adobe" something, so yes, it's an
    > image.
    > TIA,
    > V
    >


    Hopefully the 'crap' you're getting is the base64-encoded representation
    of your binary image -- you should be able to use Python to decode that,
    and then use any image viewer to verify that the JPEG is sound.

    Can you remind us of the URL where that code (with the text/plain content
    type) is running?


    > On Wed, Oct 7, 2009 at 3:52 PM, Rami Chowdhury
    > <>wrote:
    >
    >> On Wed, 07 Oct 2009 13:24:28 -0700, Victor Subervi <
    >> > wrote:
    >>
    >> I did that. In fact, just to make things easier, I wrote out exactly
    >> what
    >>> is
    >>> supposed to be rendered, as below:
    >>>
    >>> #!/usr/local/bin/python
    >>> import cgitb; cgitb.enable()
    >>> import MySQLdb
    >>> import cgi
    >>> import sys,os
    >>> sys.path.append(os.getcwd())
    >>> from login import login
    >>> user, passwd, db, host = login()
    >>> form = cgi.FieldStorage()
    >>> db = MySQLdb.connect(host, user, passwd, db)
    >>> cursor= db.cursor()
    >>> sql = "select pic1 from products where id='1';"
    >>> cursor.execute(sql)
    >>> content = cursor.fetchall()[0][0].tostring()
    >>> cursor.close()
    >>> print '''Content-Type: image/jpeg''' # Only print one line
    >>>
    >>> Content-Encoding: base64
    >>> '''
    >>> print
    >>> print content.encode('base64')
    >>>
    >>>

    >> Just to reiterate what Gabriel and Carsten have pointed out, I'd suggest
    >> changing the last few lines of the script, as the comments below
    >> indicate:
    >>
    >> print '''Content-Type: image/jpeg''' # One header line
    >> print '''Content-Encoding: base64''' # Another header line. Note *no*
    >> blank
    >> line between them
    >> print # Blank line signals the end of the
    >> headers
    >> print content.encode(base64) # Base64-encoded content comes
    >> *after*
    >> the blank line
    >>
    >> If you include extra blank lines after the headers, the browser will
    >> misunderstand where the binary image data begins, and so will see a
    >> malformed JPEG.
    >>
    >> If this doesn't work, I suggest changing the content-type header to
    >> text/plain and trying to manually decode and check the image data to
    >> ensure
    >> it's valid.
    >>
    >>
    >>
    >>> On Wed, Oct 7, 2009 at 2:47 PM, Gabriel Genellina
    >>> <
    >>> >wrote:
    >>>
    >>> En Wed, 07 Oct 2009 12:00:13 -0300, Victor Subervi
    >>>> <> escribió:
    >>>>
    >>>> > print '''Content-Type: image/jpeg
    >>>> >
    >>>> > '''
    >>>> > print
    >>>> > print content
    >>>>
    >>>> On Wed, Oct 7, 2009 at 9:51 AM, Gabriel Genellina <gagsl-
    >>>> >wrote:
    >>>>
    >>>> > That's still wrong. The output should be:
    >>>> >
    >>>> > - a line containing Content-Type: image/jpeg
    >>>> > - a blank line (no more!)
    >>>> ^^^^^^^^^^^^^^^^^^^^^^^^^
    >>>> > - the actual image data
    >>>>
    >>>> --
    >>>> Gabriel Genellina
    >>>> --
    >>>> http://mail.python.org/mailman/listinfo/python-list
    >>>>
    >>>>

    >>
    >>
    >> --
    >> Rami Chowdhury
    >> "Never attribute to malice that which can be attributed to stupidity" --
    >> Hanlon's Razor
    >> 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
    >>




    --
    Rami Chowdhury
    "Never attribute to malice that which can be attributed to stupidity" --
    Hanlon's Razor
    408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
    Rami Chowdhury, Oct 7, 2009
    #5
  6. En Thu, 08 Oct 2009 12:30:16 -0300, Victor Subervi
    <> escribió:

    > http://13gems.com/stxresort/cart/getpic1.py?id=1&x=1
    >
    > On Wed, Oct 7, 2009 at 4:11 PM, Rami Chowdhury
    > <>wrote:
    >
    >> On Wed, 07 Oct 2009 14:05:25 -0700, Victor Subervi <
    >> > wrote:
    >>
    >> print 'Content-Type: image/jpeg'
    >>> print 'Content-Encoding: base64'
    >>> print
    >>> print content.encode('base64')


    Are you sure this is your actual code?

    py> import urllib
    py> data =
    urllib.urlopen("http://13gems.com/stxresort/cart/getpic1.py?id=1&x=1").read()
    py> data[:30]
    'Content-Encoding: base64\n\n\n/9j'
    py> data[-30:]
    'XdjTJvaaF5fvfMVgbUM+gatZXFzb\n\n'

    1) The Content-Encoding line is taken as part of the response entity (the
    picture data), but should be part of the header. I bet you have a blank
    line after Content-Type.
    2) Note the \n\n at the end

    py> data = data[27:-2]
    py> decoded = data.decode("base64")
    py> open("test.jpg","wb").write(decoded)

    I tried to open test.jpg, but it's corrupt.

    py> len(decoded)
    65535
    py> decoded[:30]
    '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x02\x01\x00H\x00H\x00\x00\xff\xe1
    \xadExif\x00\x00'

    Very suspicious file size... Looks like a jpeg image that was truncated at
    that size. See whether it is already corrupt in the database and repair it
    (you're using a data type large enough to hold the image, aren't you?)

    After fixing the database, you can omit the unnecesary base64 encoding;
    anyway I'd add a Content-Length header to avoid that spurious \n at the
    end.

    --
    Gabriel Genellina
    Gabriel Genellina, Oct 8, 2009
    #6
    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. Paul  Smith
    Replies:
    5
    Views:
    2,512
    johannsig
    May 22, 2012
  2. harold

    indexing pics in frames

    harold, Nov 30, 2003, in forum: HTML
    Replies:
    8
    Views:
    432
    Whitecrest
    Dec 1, 2003
  3. The Bicycling Guitarist

    IE not reading PICS tags correctly

    The Bicycling Guitarist, Dec 14, 2004, in forum: HTML
    Replies:
    8
    Views:
    399
    Andy Dingley
    Dec 15, 2004
  4. Headbanger

    Scrollbar with pics?

    Headbanger, Mar 11, 2005, in forum: HTML
    Replies:
    3
    Views:
    3,076
    Travis Newbury
    Mar 12, 2005
  5. Don Noll

    PREVENT COPYING WEB PICS HELP

    Don Noll, Jun 13, 2005, in forum: HTML
    Replies:
    35
    Views:
    4,545
    Joel Shepherd
    Jun 17, 2005
Loading...

Share This Page