optimize-images icon indicating copy to clipboard operation
optimize-images copied to clipboard

Destroys EXIF data despite --keep-exif

Open varnav opened this issue 3 years ago • 6 comments

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 avatar May 24 '22 20:05 varnav

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.

varnav avatar May 25 '22 18:05 varnav

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

victordomingos avatar May 25 '22 19:05 victordomingos

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.

varnav avatar May 25 '22 19:05 varnav

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.

victordomingos avatar May 25 '22 20:05 victordomingos

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)

varnav avatar May 26 '22 01:05 varnav

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

YellowTech avatar Feb 16 '23 11:02 YellowTech