Why does the size of my image decrease when I add a comment to an image?

蹲街弑〆低调 提交于 2020-01-24 22:48:12

问题


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.

  • I have an image that is 2007KB. It doesn't contain a user comment in it's exif data.
  • When I add a comment ("Hi I like potatoes" just a test comment) via the properties page (Windows 10) it decreases the size of my image to 1991KB.
  • When I use a python script to add a user comment ("blah blah blah") it decreases the file size to 726KB.

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

  • EXIF InteroperabilityOffset
  • EXIF OffsetSchema
  • EXIF Padding
  • GPS GPSProcessingMethod
  • Image ExifOffset
  • Image GPSInfo
  • Image Padding
  • Image XPComment
  • Interoperability InteroperabilityIndex
  • Interoperability InteroperabilityVersion
  • Thumbnail JPEGInterchangeFormat
  • Thumbnail JPEGInterchangeFormatLength.

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))

回答1:


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)



来源:https://stackoverflow.com/questions/40321166/why-does-the-size-of-my-image-decrease-when-i-add-a-comment-to-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!