Question

I am working with binary files. They are jpeg images. I need to edit them with Python and save them. I've been using this script, and seemed to work fine so far:

import os, sys

newpath= r'C:/Users/Umberto/Desktop/temporary'
if not os.path.exists (newpath):
    os.makedirs (newpath)

data= open ('C:/Users/Umberto/Desktop/Prove_Script/Varie/_BR_Browse.001_2065642654_1.BINARY', 'rb+')
edit_data= str (data.read () )
out= open (newpath+ '/preview.BINARY', 'w')

# do my edits in a secon time...

out.write (edit_data)
data.close ()
out.close ()

Anyway, a problem (out of Python) arised: my two files are supposed to be the same, but they are not! By opening'em in a hex editor they look slightly different (the original is lower in size than the new one, namely 163 KB, agaist 167). Moreover, when I open them they ARE different. They are still viewed as images, but one looks fine (the original) while the other is totally a mess... What went wrong? Is the code I am using changing something that I don't know, and, if so, what? I hope You could help me.

Was it helpful?

Solution

You don't say what you're doing in

# do my edits in a secon time...

but that aside, the line

edit_data= str (data.read () )

will quite drastically change your data. You're opening a binary file, converting the contents to a string, and then saving the contents to a different file, this will change things.

edit_data = data.read()

Changing to above will fix the snippet that you've provided, but if you're editing the data elsewhere this will also change things.

Suggestion

If you're going to be editing binary files a lot, it might be a good idea to use the with syntax

with open(my_file, 'rb+') as fo:
    edit_data = fo.read()

and then you don't have to worry about closing the file and so on. Once you have edit_data, this will be an array of bytes that you can edit in place before saving your data again

with open(my_out_file, 'wb') as fo:
    fo.write(edit_data)

much cleaner and simpler!

OTHER TIPS

Use binary mode:

open (newpath+ '/preview.BINARY', 'wb')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top