Writing byte stream as jpeg format to disk

Navkirat Singh navkirats at gmail.com
Thu Aug 26 20:57:19 CEST 2010


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

On 27-Aug-2010, at 12:07 AM, Stefan Schwarzer wrote:

> Hi Navkirat,
> 
> On 2010-08-26 19:22, Navkirat Singh wrote:
>> 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
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list