Writing byte stream as jpeg format to disk

Navkirat Singh navkirats at gmail.com
Thu Aug 26 23:25:38 CEST 2010


On 27-Aug-2010, at 2:40 AM, Robert Kern wrote:

> On 8/26/10 3:47 PM, Navkirat Singh wrote:
>> 
>> On 27-Aug-2010, at 1:57 AM, MRAB wrote:
>> 
>>> On 26/08/2010 21:14, Navkirat Singh wrote:
>>>> 
>>>> On 27-Aug-2010, at 1:32 AM, Dave Angel wrote:
>>>> 
>>>>> Navkirat Singh wrote:
>>>>>> 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
>>>>> If by "decoding them to a string" you mean converting to Unicode, then
>>>>> you've already trashed the data. That's only valid if the bytes had
>>>>> been encoded from valid Unicode characters, and then only if you use
>>>>> the corresponding decoding technique.
>>>>> 
>>>>> If you mean some other decoding, then the question is meaningless
>>>>> without telling us just what the decoding is, preferably with some code.
>>>>> 
>>>>> It also might be useful to know what version of Python you're using,
>>>>> when you post the code.
>>>>> 
>>>>> DaveA
>>>>> 
>>>> 
>>>> Dave,
>>>> 
>>>> I am using Python3 and I receive a byte stream with a jpeg attached sent
>>>> by the web browser over a socket, which looks like this:
>>>> 
>>>> b': image/jpeg\r\nAccept: text/*\r\nReferer:
>>>> http://127.0.0.1:8001/\r\nAccept-Language: en-us\r\nAccept-Encoding:
>>>> gzip, deflate\r\nContent-Length: 91783\r\nConnection:
>>>> keep-alive\r\n\r\n\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00\x84\x00\x03\x02\x02\x03\x02\x02\x03\x03\x03\x03\x04\x03\x03\x04\x05\x08\x05\x05\x04\x04\x05\n\x07\x07\x06\x08\x0c\n\x0c\x0c\x0b\n\x0b\x0b\r\x0e\x12\x10\r\x0e\x11\x0e\x0b\x0b\x10\x16\x10\x11\x13\x14\x15\x15\x15\x0c\x0f
>>>> 
>>>> From the above, I need to:
>>>> 
>>>> a) Split the header content from the image content, which comes after
>>>> the keep-alive\r\n\r\n part
>>>> 
>>>> b) Then write the image content to file for further use as a jpeg.
>>>> 
>>> Try:
>>> 
>>>    image = header.split(b'keep-alive\r\n\r\n', 1)[-1]
>>>    open(image_path, 'wb').write(image)
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>> 
>> I think I forgot to mention that the original is a stream of bytes decoded using ISO-8859-1 as utf-8 trhrew errors (lack of knowlegdge again).
>> 
>> @MRAB - the split() method in python 3 works only on strings and throws an error if I try to use bytes
> 
> This is incorrect.
> 
> Python 3.1.2 (r312:79360M, Mar 24 2010, 01:33:18)
> [GCC 4.0.1 (Apple Inc. build 5493)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> 
> >>> bytes = b'Connection: keep-alive\r\n\r\nbody'
> >>> bytes.split(b'\r\n\r\n', 1)[-1]
> b'body'
> 
> 
> FYI: the JPEG data is not in the header. The b'\r\n\r\n' sequence delimits the header from the body. Do not rely on "Connection: keep-alive" being the last header.
> 
> -- 
> 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
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Thanks Everyone,

@Robert - Thanks a lot for your time :-) , I did know that the body starts after the occurrence two CRLF sequences, but I was following RFC2616 as a guide, which specifically mentions:

	"The presence of a message-body in a request is signaled by the inclusion of a
     Content-Length or Transfer- Encoding header field in the
          request’s message-headers"

Which has not been done by the author of the library, hence I said what I did. Or I have misunderstood the RFC

Regards,
Nav





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100827/db3a33a4/attachment.html>


More information about the Python-list mailing list