exif_delete icon indicating copy to clipboard operation
exif_delete copied to clipboard

When you remove JPEG metadata, the quality of the images changes

Open Shyryp opened this issue 11 months ago • 2 comments

When you remove JPEG metadata, the quality and size of the image change

The thing is that your code does not take into account that Pillow saves files with default quality settings. You need to take this into account in your code, because otherwise JPEG images lose quality, and if they were previously compressed, they increase in size (since they acquire the default quality that Pillow sets).

You need to do something with the "quality" and "subsampling" parameters at the .save() stage.

However, I do not yet understand how to fix this, since the original JPEG files do not have metadata about quality and subsampling, and the "keep" value does not work because Pillow sets format = None when creating a new image, which leads to an error.

If you set the values ​​"quality" = 100 and "subsampling" = 0, then the created images will weigh almost as much as PNG images, although compression methods that reduce their weight could have been applied to the original JPEG images earlier.

if original_format == "JPEG":
    save_params["quality"] = 100
    save_params["subsampling"] = 0

img_no_metadata.save(file_path, format=original_format, **save_params)

This problem needs to be solved, because otherwise the library spoils the quality and volume of the original JPEG files (and possibly other formats).

Shyryp avatar Dec 28 '24 18:12 Shyryp

Confirmation from the author of another library that JPEG images are recoded when using the Pillow library: https://github.com/sh1d0wg1m3r/Metadata-Removal-Tool/issues/2

You also need to do something so that users of your library do not lose image quality and size when resaving with the removal of metadata.

Good luck with solving the problem!

Shyryp avatar Dec 28 '24 18:12 Shyryp

Well, I didn't know THAT about the JPEG format. Fun.

john-science avatar Dec 29 '24 17:12 john-science