OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to update exif data without losing JFIF header?

  • Thread starter Thread starter tigrou
  • Start date Start date
T

tigrou

Guest
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 :

Code:
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 :

Before: enter image description here

After: enter image description here
<p>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 :</p>
<pre><code>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)
</code></pre>
<p>It seems to work (Windows can open image, and image is rotated accordingly)
The main issue is that JFIF header seems to be lost :</p>
<p>Before: <a href="https://i.sstatic.net/O9IJTEh1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/O9IJTEh1.png" alt="enter image description here" /></a></p>
<p>After: <a href="https://i.sstatic.net/A22BJ4a8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/A22BJ4a8.png" alt="enter image description here" /></a></p>
 

Latest posts

Top