unsigned char array -> buffer

H

Henri Schomaecker

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
 
D

Diez B. Roggisch

Henri said:
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.
 
H

Henri Schomaecker

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
 
D

Diez B. Roggisch

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

Michel Claveau - abstraction méta-galactique non t

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
 
W

Wayne Witzel III

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

Henri Schomaecker

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top