Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change EXIF data on JPEG without altering picture

I change the exif on a jpeg using piexif to read and write exif data, which seems to work fine. The issue is when I read and write the jpeg, even tho I don't change the bytes, it saves the picture with different pixels and the picture that was read. I need it to be exactly the same pixels. I understand this is because jpeg is a lossy format, but the only way I've found around it is to save it as a png and then export it as a jpeg with Mac Preview, which is not good, because I have hundreds of pictures.

def adjust_img(path):
   img = PIL.Image.open(path)
   exif_dict = piexif.load(img.info['exif'])
   new_exif = adjust_exif(exif_dict)
   exif_bytes = piexif.dump(new_exif)
   pc = path.split('/')
   stem = '/'.join(pc[:-1])
   img.save('%s/_%s' % (stem,pc[-1]), "JPEG", exif=exif_bytes, quality=95, optimize=False)

How can I preserve the picture and just alter the exif?

like image 971
syzygy Avatar asked Nov 29 '18 16:11

syzygy


People also ask

Can photo EXIF data be changed?

Yes EXIF data can be altered. You can change the fields in post with certain programs. You can also fake the date simply by changing the date and time of the camera before taking the picture, there's nothing that says a camera has to have the exact date and time.

Can you manipulate photo metadata?

While metadata can be useful, sometimes it can also be considered a security concern for many people. Thankfully, you cannot only edit metadata, but the operating system also lets you remove in bulk certain properties that might contain personal information, such as name, location, etc.


1 Answers

https://piexif.readthedocs.io/en/latest/functions.html

exif_dict = piexif.load(path)
new_exif = adjust_exif(exif_dict)
exif_bytes = piexif.dump(new_exif)
piexif.insert(exif_bytes, path)
like image 149
hMatoba Avatar answered Oct 13 '22 02:10

hMatoba