I use the following code to rotate an JPG image by 180 degrees, by updating EXIF header. I want to avoid re-encoding the image :
import PIL
import piexif
filename = "somefile.jpg"
img = PIL.Image.open(filename)
exif_dict = piexif.load(img.info['exif'])
exif_dict["0th"][274] = 3 #180 deg rotate
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, filename)
It seems to work (Windows can open image, and image is rotated accordingly) The main issue is that JFIF header seems to be lost :
You can't do that with the piexif library, as you can see in here. Use other libraries. The following is an example of using the exif library.
import exif
with open('src.jpg', 'rb') as sf, open('dest.jpg', 'wb') as df:
img = exif.Image(sf)
img.orientation = 3
df.write(img.get_file())