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
img = PIL.Image.open("somefile.jpg")
exif_dict = piexif.load(img.info['exif'])
exif_dict["0th"][274] = 3 #180 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 :
Before:
After: