jarun / imgp

:camera_flash: Fast batch image resizer and rotator
GNU General Public License v3.0
1.03k stars 50 forks source link

Feature idea: flag to ignore (or not exit(1) on) erroneous input files in batch usage #43

Closed metaleap closed 3 years ago

metaleap commented 3 years ago

imgp is real handy for my mass-resizing needs, thanks a bunch! This is just a little idea for future users, I have a workaround, so feel free to close it =)

With 1000s of comic books (100000s of pages) to down-size, it happens that one encounters occasional corrupt jpgs. My bash script after extractions and other preps ultimately call imgp -m -x 75 -q 66 -w ./*.jpg. The latter part likely gets expanded by bash into all the jpgs matched in current dir.

Anyway, one way or another imgp is called once and works through multiple files. Now just 0.01% of those may cause your PIL open or resize or other calls to fail, in my case as follows:

Traceback (most recent call last):
  File "/bin/imgp", line 676, in <module>
    main()
  File "/bin/imgp", line 653, in main
    if resize_image(path):
  File "/bin/imgp", line 302, in resize_image
    img = img.resize((_hres, _vres), antialias)
  File "/usr/lib/python3.9/site-packages/PIL/Image.py", line 1916, in resize
    self.load()
  File "/usr/lib/python3.9/site-packages/PIL/ImageFile.py", line 259, in load
    raise OSError(
OSError: image file is truncated (1 bytes not processed)

Not to worry about that particular error, just would be neat to have a flag such that when imgp processes multiple files and just one of them is problematic, it continues processing the other ones and exits with success code 0. It might still warn on stderr or something.

I'll work around it for now by looping over the jpg files in the dir and doing one imgp call for each file individually.

jarun commented 3 years ago

The problem is optimistic human nature, it runs so fast it is easy to miss the error... maybe not on the first run but you will miss an ignored error if you are using imgp over a couple of months without any issues.

Now, let us consider all errors in general - during open, read, write... anytime during the process.

For lazy people like me, the natural next step is to remove the original files without examining all 25 output files one by one. Say, I miss the one cute picture I had of my kid from when he was 2!!! I hope you get the picture. When you set the flag you have no way to determine the amount of damage it can do to you.

So the single reason I wouldn't introduce such a flag is the damage that can be done is irreversible.

But I would request you to add an exception handler and fail more gracefully as you can reproduce the issue.