unsigned char array -> buffer

Discussion in 'Python' started by Henri Schomaecker, Oct 18, 2004.

  1. Hi again,

    as I already wrote, I'm trying to get image binary data into a var holding
    an unsigned char string and then print it directly to the browser.
    I now already did some tries with an array and now found the buffer()
    function. - I thought I would have the solution now and also changed stdout
    to O_BINARY, but still I don't get the data right:

    8<--------8<--------8<--------8<--------8<--------

    imgObj = win32com.client.Dispatch("mine.imgsender")
    myImgData = buffer(imgObj.getImgData())

    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    sys.stdout.write(myImgData)

    8<--------8<--------8<--------8<--------8<--------

    I'm really lost and would be very thankful if someone could help me with
    that.

    Many thanks in advance,
    yours Henri

    --
    | Henri Schomäcker - BYTECONCEPTS, VIRTUAL HOMES
    |     Datendesign für Internet und Intranet
    |         http://www.byteconcepts.de
    |         http://www.virtual-homes.de
    Henri Schomaecker, Oct 18, 2004
    #1
    1. Advertising

  2. Henri Schomaecker wrote:
    > as I already wrote, I'm trying to get image binary data into a var holding
    > an unsigned char string and then print it directly to the browser.
    > I now already did some tries with an array and now found the buffer()
    > function. - I thought I would have the solution now and also changed
    > stdout to O_BINARY, but still I don't get the data right:
    >
    > 8<--------8<--------8<--------8<--------8<--------
    >
    > imgObj = win32com.client.Dispatch("mine.imgsender")
    > myImgData = buffer(imgObj.getImgData())
    >
    > msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    > sys.stdout.write(myImgData)
    >
    > 8<--------8<--------8<--------8<--------8<--------
    >
    > I'm really lost and would be very thankful if someone could help me with
    > that.


    That snipplet of code is totally useless, as none of us has your custom
    mine.imgsender lying around his harddisk, so we can't possibly know what
    data you get, let alone what data you want.

    So if you provided us with the actual data that is returned by getImgData we
    might be able to help.

    --
    Regards,

    Diez B. Roggisch
    Diez B. Roggisch, Oct 18, 2004
    #2
    1. Advertising

  3. Hi Diez,

    oh, I thought I made it clear: The data is a jpg image.
    Let me try to explain it like that:

    I create a com object, which loads a jpg image and then I request that image
    from the com object with the method getImgData().
    As answer to getImageData(), the com object returns a SafeArray of unsigned
    char (Bytes).

    My problem (no problem with asp and perl, same with php) is, that I would
    like to read the imagedata directly into a string of unsigned chars and
    then print it to the user agent (webbrowser) in binary mode.

    So, it would not really make sense to print the imagedata here ;-) - Sorry
    for that confusion.

    I hope it's clear now and would be very glad, If maybe you could help me.
    Again, many thanks in advance,
    yours Henri


    Diez B. Roggisch wrote:

    > Henri Schomaecker wrote:
    >> as I already wrote, I'm trying to get image binary data into a var
    >> holding an unsigned char string and then print it directly to the
    >> browser. I now already did some tries with an array and now found the
    >> buffer() function. - I thought I would have the solution now and also
    >> changed stdout to O_BINARY, but still I don't get the data right:
    >>
    >> 8<--------8<--------8<--------8<--------8<--------
    >>
    >> imgObj = win32com.client.Dispatch("mine.imgsender")
    >> myImgData = buffer(imgObj.getImgData())
    >>
    >> msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    >> sys.stdout.write(myImgData)
    >>
    >> 8<--------8<--------8<--------8<--------8<--------
    >>
    >> I'm really lost and would be very thankful if someone could help me with
    >> that.

    >
    > That snipplet of code is totally useless, as none of us has your custom
    > mine.imgsender lying around his harddisk, so we can't possibly know what
    > data you get, let alone what data you want.
    >
    > So if you provided us with the actual data that is returned by getImgData
    > we might be able to help.
    >


    --
    | Henri Schomäcker - BYTECONCEPTS, VIRTUAL HOMES
    |     Datendesign für Internet und Intranet
    |         http://www.byteconcepts.de
    |         http://www.virtual-homes.de
    Henri Schomaecker, Oct 18, 2004
    #3
  4. Hi,

    > I create a com object, which loads a jpg image and then I request that
    > image from the com object with the method getImgData().
    > As answer to getImageData(), the com object returns a SafeArray of
    > unsigned char (Bytes).
    >
    > My problem (no problem with asp and perl, same with php) is, that I would
    > like to read the imagedata directly into a string of unsigned chars and
    > then print it to the user agent (webbrowser) in binary mode.
    >
    > So, it would not really make sense to print the imagedata here ;-) - Sorry
    > for that confusion.


    It will - as one can see what you actually get, instead of what you _want_
    to get. AFAIK win32 converts variants to appropriate python types, with
    arrays returned as lists - so if things work as documented, you shold get
    something like this:

    l = ["a", "$", ...]

    Making that a (binary) string should be a simple matter of

    l.join("")

    So I'd like to know what you really get- of cours the first few lines are
    sufficient, no need to post a 100kb jpeg binary image.


    --
    Regards,

    Diez B. Roggisch
    Diez B. Roggisch, Oct 18, 2004
    #4
  5. Bonsoir !

    Hi !




    Attention, risque de problème (confusion) avec les chaînes à zéro terminal.

    Attention, risk of problem (confusion) with the string with zero terminal.




    *sorry for my bad english*




    @-salutations
    --
    Michel Claveau
    Michel Claveau - abstraction méta-galactique non t, Oct 18, 2004
    #5
  6. > My problem (no problem with asp and perl, same with php) is, that I would
    > like to read the imagedata directly into a string of unsigned chars and
    > then print it to the user agent (webbrowser) in binary mode.
    >

    # this example assumes you are using PIL
    # this example assume mod_python, but can be easily modified.
    def displayJpeg():
    im = Image.Open(file)
    imdata = StringIO()
    im.save(imdata, "JPEG")
    del im

    req.content_type = "Content-type: image/jpeg"
    return imdata.getValue()

    Hope this helps in some way? I think I might have misunderstood the
    question though.
    Wayne Witzel III, Oct 19, 2004
    #6
  7. Many thanks for the interest,
    I found it out by myself now:

    I just didn't realise the special behaviour of pythons print command.
    I printed out the Content-type with the print command and didn't notice the
    behaviour to add a newline automatically. So I had three newlines between
    the Content-type line and the imagedata. It looked like the setting of
    stdout to binary mode didn't work.

    This works:

    8<--------8<--------8<--------8<--------8<--------

    imgObj = win32com.client.Dispatch("mine.imgsender")

    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

    print 'Content-Type: image/jpeg\n'
    sys.stdout.write(imgObj.getImgData())

    8<--------8<--------8<--------8<--------8<--------

    The most stupid mistakes are the hardest to find ;-)
    Anyway, now everything is great.
    Many thanks,
    yours Henri

    --
    | Henri Schomäcker - BYTECONCEPTS, VIRTUAL HOMES
    |     Datendesign für Internet und Intranet
    |         http://www.byteconcepts.de
    |         http://www.virtual-homes.de
    Henri Schomaecker, Oct 20, 2004
    #7
    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. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    582
    Jack Klein
    May 9, 2005
  2. Ioannis Vranos
    Replies:
    11
    Views:
    758
    Ioannis Vranos
    Mar 28, 2008
  3. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    612
    Ben Bacarisse
    Mar 29, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    791
    James Kanze
    Oct 13, 2008
  5. Gabriel Genellina
    Replies:
    0
    Views:
    514
    Gabriel Genellina
    Aug 13, 2009
Loading...

Share This Page