开发者

file.write() and sys.stdout.write() are giving me two different outputs - Python

The code below takes a JPEG image and converts it to a string. That string is then saved into the image variable. Then, the string is written to a.jpg using File IO and then written to b.jpg by me piping stdout to开发者_开发问答 the file.

import thumb
import sys

x = thumb.Thumbnail('test.jpg')
x.generate(56, 56)

image = str(x)

with open('a.jpg', 'wb') as f:
    # saving to a.jpg
    f.write(image)

# saving to b.jpg
sys.stdout.write(image)

Usage:

python blah.py > b.jpg

This results in two image files (a.jpg and b.jpg). These images should be identical... But they aren't.

a.jpg

b.jpg

I can see, by looking at each image in Notepad, that linebreaks are, somehow, being added to b.jpg. Resulting in a corrupted image.

Why is a.jpg different to b.jpg?


You write your data to a.jpg as binary, while b.jpg get written in text mode. When in binary mode otherwise special characters (such as newlines or EOF marker) are not treated special, while in text mode they are.

In Python 3 you can switch modes:

  • http://docs.python.org/py3k/library/sys.html#sys.stdin

The standard streams are in text mode by default. To write or read binary data to these, use the underlying binary buffer. For example, to write bytes to stdout, use sys.stdout.buffer.write(b'abc').


Untested (Python 2):

import sys, os

binout = os.fdopen(sys.stdout.fileno(), 'wb')
binout.write(b'Binary#Data...')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜