Writing byte stream as jpeg format to disk

N

Navkirat Singh

Hey guys,

I am programming a webserver, I receive a jpeg file with the POST method.The file (.jpeg) is encoded in bytes, I parse the bytes by decoding them to a string. I wanted to know how i could write the file (now a string) as a jpeg image on disk. When I try to encode the same string to a bytes and write them in binary format to disk, the file is not recognized as jpeg. I would be grateful if someone could help me with this.


Regards,
Nav
 
J

John Bokma

Navkirat Singh said:
Hey guys,

I am programming a webserver, I receive a jpeg file with the POST
method.The file (.jpeg) is encoded in bytes, I parse the bytes by
decoding them to a string.

Why?
 
G

Grant Edwards

why? I am not quite sure what you have not understood.

You're starting with JPEG data. If you want to write it to a file,
then write it to a file.

Whatever process you're describing as "I parse the bytes by decoding
them to a string" is not needed and is apparently converting the JPEG
data into something that's not JPEG data.
 
S

Stefan Schwarzer

Hi Navkirat,

I am programming a webserver, I receive a jpeg file with
the POST method.The file (.jpeg) is encoded in bytes, I
parse the bytes by decoding them to a string. I wanted to
know how i could write the file (now a string) as a jpeg
image on disk. When I try to encode the same string to a
bytes and write them in binary format to disk, the file is
not recognized as jpeg. I would be grateful if someone
could help me with this.

I guess you mean you "see" a byte string in your server and
want to write that to disk. Assuming the string you got is
the correct image data in the first place, you can, in
Python 2.x, write the string data to disk like this:

fobj = open("some_image.jpg", "wb")
fobj.write(byte_string)
fobj.close()

Note that you should use "wb" as mode to write as binary.
Otherwise you'll get automatic line ending conversion (at
least on Windows) which will give the result you describe.

If my answer doesn't help, you probably need to describe in
more detail what you're doing, including showing some real
code.

Stefan
 
N

Navkirat Singh

I am sorry, maybe I was not elaborate in what I was having trouble with. I am using a jpegcam library, which on my web page captures a webcam image and sends it to the server via the POST method. On the Server side (python 3), I receive this image as a part of header content in bytes (I know thats not how it should be done, but the author has some reason for it), so I first convert the headers to a string so I can separate them. From the separated headers I fish out the content of the image file (which is a string). This is where I get stuck, how am I supposed to convert it back to an image, so I can save as a jpeg on disk.

Regards,
Nav
 
M

MRAB

I am sorry, maybe I was not elaborate in what I was having trouble
with. I am using a jpegcam library, which on my web page captures a
webcam image and sends it to the server via the POST method. On the
Server side (python 3), I receive this image as a part of header
content in bytes (I know thats not how it should be done, but the
author has some reason for it), so I first convert the headers to a
string so I can separate them. From the separated headers I fish out
the content of the image file (which is a string). This is where I
get stuck, how am I supposed to convert it back to an image, so I can
save as a jpeg on disk.
[snip]
What does that string look like? Try printing out repr(image[ : 100]).
If it looks like plain bytes, then write it to file. If it looks like a
series of hex digits, then decode to bytes before writing.
 
R

Robert Kern

why? I am not quite sure what you have not understood.

Why decode the bytes to (presumably) unicode strings just to encode them back to
bytes again? JPEG is not composed of unicode characters; you need to leave them
as bytes.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
N

Navkirat Singh

Why decode the bytes to (presumably) unicode strings just to encode them back to bytes again? JPEG is not composed of unicode characters; you need to leave them as bytes.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

The image bytes are a part of a HTTP header content ( not the message body ). To separate the header content from the image I have to first convert the bytes to string to perform parsing. The resultant string then needs to be converted back to the image. Hence, my problem.
 
N

Navkirat Singh

I am sorry, maybe I was not elaborate in what I was having trouble
with. I am using a jpegcam library, which on my web page captures a
webcam image and sends it to the server via the POST method. On the
Server side (python 3), I receive this image as a part of header
content in bytes (I know thats not how it should be done, but the
author has some reason for it), so I first convert the headers to a
string so I can separate them. From the separated headers I fish out
the content of the image file (which is a string). This is where I
get stuck, how am I supposed to convert it back to an image, so I can
save as a jpeg on disk.
[snip]
What does that string look like? Try printing out repr(image[ : 100]).
If it looks like plain bytes, then write it to file. If it looks like a
series of hex digits, then decode to bytes before writing.

Thanks MRAB, your suggestions have always been very helpful to me. I shall let you know on what I see.

Regards,
Nav
 
L

Lawrence D'Oliveiro

In message
I receive a jpeg file with the POST method.The file (.jpeg) is encoded in
bytes, I parse the bytes by decoding them to a string. I wanted to know
how i could write the file (now a string) as a jpeg image on disk.

I assume the JPEG data is received along with other field values in the
POST. You’ll be saving those other fields in a database, right? So why not
save the JPEG image there as well?
 
L

Lawrence D'Oliveiro

Navkirat said:
The image bytes are a part of a HTTP header content ( not the message body
).

In which case, won’t they be in some encoding like Base-64? I don’t think
you’re allowed arbitrary binary bytes in an HTTP header.
 
R

Robert Kern

In message


I assume the JPEG data is received along with other field values in the
POST. You’ll be saving those other fields in a database, right? So why not
save the JPEG image there as well?

No, the only thing in the body of the POST are the bytes of the JPEG. He was
incorrect in thinking that the JPEG data was arriving in the header. See the
later posts in the thread for complete answers to his problem.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top