View Single Post
Old 02-02-2023, 09:04 AM   #8
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 80,055
Karma: 147977995
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
Quote:
Originally Posted by isarl View Post
If you are willing to do more work from the commandline then epubs make this pretty easy. Every epub file is also a zip file which you can simply unzip into a directory. You could then run in-place image compression only on files matching a regular expression, like \.jpe?g$, then re-zip the file. Of course, doing it this way will be destructive, so proceed with caution.

I would likely do this using a combination of the following tools: find and its -execdir option, unzip, xargs, and ImageMagick's mogrify.

For a single book, this might look like:
unzip book.epub -d tmp_book
cd tmp_book
find . -iname "*.jpeg" -iname "*.jpg" -execdir mogrify -define jpeg:extent=300KB -size 300x300 '{}' ';'
zip new_book *
mv new_book.zip ..
cd ..
rm -rf tmp_book

The options I specified to mogrify here will enforce a maximum filesize of 300KB, and a maximum image geometry of 300×300. It will preserve the aspect ratio, using each value as a maximum – this command will not force your images to be square. I just happened to choose equal maxwidth and maxheight.

(I guess I didn't end up using xargs after all – find kind of has it built-in.)
Why are you going 300x300 maximum? That makes higer resolution images way too small. I highly suggest you don't do that.
JSWolf is online now   Reply With Quote