victordomingos / optimize-images

A command-line interface (CLI) utility written in pure Python to help you reduce the file size of images.
https://no-title.victordomingos.com/projects/optimize-images
MIT License
269 stars 49 forks source link

Destroys EXIF data despite --keep-exif #40

Open varnav opened 2 years ago

varnav commented 2 years ago

Describe the bug Destroys EXIF data despite --keep-exif

To Reproduce

pip3 install pillow optimize-images
optimize-images --keep-exif .

Optimized image will have it's EXIF data wiped.

Expected behavior Image gets optimized, EXIF data intact.

varnav commented 2 years ago

Did some debugging, here's what happening:

File "C:\src\optimize-images\optimize_images\img_optimize_jpg.py", line 80, in optimize_jpg piexif.transplant(os.path.expanduser(task.src_path), tmp_buffer) File "C:\src\optimize-images\venv\lib\site-packages\piexif_transplant.py", line 26, in transplant if image[0:2] == b"\xff\xd8": TypeError: '_io.BytesIO' object is not subscriptable

It seems that transplant wants real file as 2nd parameter, but gets BytesIO instead. At same time it seems that BytesIO can be used as a 3rd parameter.

victordomingos commented 2 years ago

Hi! Thanks for reporting this issue. It seems to be happening on piexif's side, but it needs to be investigated. Does it happen always, regardless of the image file being processed?

Can you please provide the output of optimize-images --version (please anonymise the file paths if necessary)?

varnav commented 2 years ago

Looks like here:

piexif.transplant(os.path.expanduser(task.src_path), tmp_buffer)

you're giving tmp_buffer that is BytesIO. But transplant wants path to actual file.

Always the same.

I'm using latest revision from this repo.

victordomingos commented 2 years ago

I understand. Thanks for the details. It should not be very complicated to fix, but I can't promise a date. I will try to fix in a few days it if no one submits a pull request first.

varnav commented 2 years ago

You actually don't need piexif, Pillow can handle this:

Load all EXIF data:

exif = img.info['exif']

Save it:

img.save('P4072956_thumb.jpg', exif=exif)

YellowTech commented 1 year ago

pip install optimize-images==1.4 does not suffer from this problem