I'm trying to do a very simple read/write of an image file. The requirement is to strip out a few extra newlines throughout the file. However, I'm having issues literally writing the unmodified file back correctly.
import os, sys
picture = "IMAGE NAME.jp2"
rawImg = open(picture, "rb").read()
newImg = open("IMAGE NAME 2.jp2", "wb")
newImg.write(rawImg)
The above code should read in the first image, then output it to a new file. When attempting this, however, the second file has a corrupted header (according to gimp). When opening the two in Notepad++, it becomes apparent that the last handful of characters have been dropped. How many characters depend on the particular source image, ranging from a small few to a hundred. This is also present with both .jp2 and .png files.
I have tried using the "rU" option, as well as reading line by line with readline() to no avail. As a smaller note, it would be ideal if the newline characters weren't also converted for some reason, since I literally just want to read/write perfectly.