I tried to add a comment to my image and it decreased the filesize. I have three images all have different file sizes and different user comment EXIF data .The image without the comment is the largest. Why is the image without the comment the largest? By editing the EXIF data am I compressing or altering the image.
Apart from the filesize and EXIF data I've found no other differences in the images. I've zoomed in on the images and haven't noticed differences. I've looked through the rest of EXIF data and there are some differences. The differences are in
If you have any other questions please let me know.
Here is the python script:
import piexif
import os.path
from PIL import Image
def writeExifComment(filename,comment):
im = Image.open(filename)
fileExtension = os.path.splitext(filename)[1]
exif_dict = piexif.load(im.info["exif"])
exif_dict["Exif"][piexif.ExifIFD.UserComment] = comment
exif_bytes = piexif.dump(exif_dict)
im.save(filename, 'jpeg', exif=exif_bytes)
im.close()
def readExifComment(filename):
data = piexif.load(filename)
exif = data['Exif']
comment = exif.get(37510, '').decode('UTF-8')
return comment
filename = '1.jpg'
writeExifComment(filename,"blah blah blah")
print(readExifComment(filename))
When you save an image (im.save(filename, 'jpeg', exif=exif_bytes)
) using PIL library there is a default quality that you use. This default is 75 (which means - if your original image is in higher quality - during the saving - the quality of the image will decrease, and also the size of the image.
You can change the quality by using quality=X
(X = 1->95, You should avoid any number above 95):
im.save(filename, 'jpeg', exif=exif_bytes, quality=95)
Note that it might create an image with higher size than your original image's size.
There is a question on stack regarding the original quality of image, you can check more here: Determining JPG quality in Python (PIL)