dave
dave

Reputation: 7877

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?

Upvotes: 2

Views: 1912

Answers (1)

miku
miku

Reputation: 188164

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:

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...')

Upvotes: 2

Related Questions