2

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)

1 Answers1

-1

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 exiftool which does not decode and re-encode your data. At the command line, you would use:

exiftool -Model='My Funky Camera' image.jpg

You can then see the new setting with:

exiftool image.jpg

ExifTool Version Number         : 11.11
File Name                       : image.jpg
Directory                       : .
File Size                       : 105 kB
File Modification Date/Time     : 2019:10:23 12:54:34+01:00
File Access Date/Time           : 2019:10:23 12:54:35+01:00
File Inode Change Date/Time     : 2019:10:23 12:54:34+01:00
File Permissions                : rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
JFIF Version                    : 1.01
Exif Byte Order                 : Big-endian (Motorola, MM)
Camera Model Name               : My Funky Camera              <--- HERE IT IS
X Resolution                    : 1
Y Resolution                    : 1
Resolution Unit                 : None
...
...
Megapixels                      : 1.6

There is a Python binding, but personally I would just use a Python subprocess with the regular, command-line tool. YMMV.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432