I was trying to change camera model name using PIL and piexif library for python. It was a successful attempt but when i checked the images, the image size got reduced from 8-9 MB to 1-2 MB.
How can i change the exif data without changing the image size?
My code is following:
img = Image.open(image_)
exif_dict = piexif.load(img.info['exif'])
exif_dict = piexif.load(image)
#new camera model name
exif_dict['0th'][272] = b'new_camera_model_name'
# Converting to bytes
exif_bytes = piexif.dump(exif_dict)
#Saving Image
img.save(image, exif=exif_bytes)
You can't actually achieve your stated aim in the way you are proceeding. You are reading in an image written by some other, unknown JPEG library and writing it out, possibly using a different quality value with the JPEG library that PIL uses under the covers. The JPEG standard allows image encoders/decoders to use varying levels of accuracy and to make varying tradeoffs between accuracy, image size and speed so it is unlikely that 2 JPEG encoders will come to the same result - even different versions of the same library may encode differently.
So, if you really want to change your meta-data without changing the image quality, you will be better off using a tool such as
exiftoolwhich does not decode and re-encode your data. At the command line, you would use:You can then see the new setting with:
There is a Python binding, but personally I would just use a Python
subprocesswith the regular, command-line tool. YMMV.