Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre

Notices

Reply
 
Thread Tools Search this Thread
Old 01-17-2023, 09:07 PM   #1
tatagi
Connoisseur
tatagi began at the beginning.
 
Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
command line commands for image compression?

Hello!

I prefer not to add files first into library which is not necessary for my use case. Instead, I want to do it out of court, that's why I find command line interface a good option.

having tried loseless compression but it was not giving me the desired result, so I am considering to do lossy compression to size epubs by a great margin.

problem is I don't know what exactly is the command line command to do this , obviously.
I have thousands of files to do this. so batch command would be better.

any help would be grateful.
tatagi is offline   Reply With Quote
Old 01-17-2023, 09:33 PM   #2
djrx
Junior Member
djrx began at the beginning.
 
Posts: 9
Karma: 10
Join Date: Jan 2023
Device: Kindle Scribe (2022)
Depends on your operating system. If you're using Linux/Mac, you can install Homebrew. Once brew is installed, you can install imagemagick

Quote:
If source is too big
Install ImageMagick using Homebrew

$/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
$brew install imagemagick

### Set the max file size
for i in *.jpg; do
convert "$i" -define jpeg:extent=500kb "${i%.jpg}-new.jpg"
done


### To change just the quality of the file
for i in *.jpg; do
convert "$i" -quality 80% "${i%.jpg}-new.jpg"
done


### Or if no need to keep the original
for i in *.jpg; do
mogrify -quality 80% *.jpg
done


### convert png to jpg
for i in *.png; do
convert "$i" "${I%.png}.jpg"
done

OR

magick mogrify -format jpg *.png
rm *.png
Just copy and paste the whole block of lines for the options you want into a terminal window and go to town
djrx is offline   Reply With Quote
Advert
Old 01-17-2023, 11:03 PM   #3
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Calibre command line ebook-polish only allows for lossless compression of images per the documentation.

If you insist on lossy compression, Editor Chains plugin allows you to use calibre's image compression functionality (among many other editor functions) from the command line:
  • Install the plugin from calibre (Calibre main library > preferences > get plugins).
  • Open the calibre editor, and then import the chain attached below (Editor > plugins menu > Editor Chains > add chains > right click > import chain).
  • From the command line you can type this command to compress images in your ebook (epub and azw3 formats supported):

    Code:
    calibre-debug -r "Editor Chains" "Compress Images" path_to_input.epub path_to_output.epub

Note: You can configure the compression ratio by modifying the settings of the chain.
Attached Files
File Type: zip compress_images.zip (347 Bytes, 113 views)
capink is offline   Reply With Quote
Old 01-18-2023, 05:31 AM   #4
tatagi
Connoisseur
tatagi began at the beginning.
 
Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
Quote:
Originally Posted by djrx View Post
.
thank you. but it is windows 10 that I am using. it seems like imagemagick is useful for img files, not compressing image files that are within epubs directly.

Quote:
Originally Posted by capink View Post
.
thank you.
oh, btw you're the developer of that plugin. that's nice

could you elaborate it a little further?

for example, all of my epubs are in C:\users\foldername
and some epubs are in its subfolders like
C:\users\foldername\red
C:\users\foldername\blue
etc.

in this case, what is the exact commands for bulk compress to keep new epubs with same name and folder structure?

calibre-debug -r "Editor Chains" "Compress Images" "C:\users\foldername\*.epub" "C:\users\newfoldername\*.epub"

will it work? or can I just specify the folder name so it compresses all the epubs inside?

Last edited by tatagi; 01-18-2023 at 05:36 AM.
tatagi is offline   Reply With Quote
Old 01-18-2023, 08:03 AM   #5
isarl
Addict
isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.
 
Posts: 287
Karma: 2534928
Join Date: Nov 2022
Location: Canada
Device: Kobo Aura 2
Quote:
Originally Posted by tatagi View Post
thank you. but it is windows 10 that I am using. it seems like imagemagick is useful for img files, not compressing image files that are within epubs directly.
Epubs are just zipped directories with a specific set of included files like the container file. If you unzip an ePub, compress the image files, and re-zip the directory, you will have created a new ePub with smaller images. Note that the compressed images should have the exact same filenames, and be in the exact same locations/subdirectories, as the original images (which should be removed before re-zipping).
isarl is offline   Reply With Quote
Advert
Old 01-18-2023, 03:43 PM   #6
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by tatagi View Post
thank you. but it is windows 10 that I am using. it seems like imagemagick is useful for img files, not compressing image files that are within epubs directly.



thank you.
oh, btw you're the developer of that plugin. that's nice

could you elaborate it a little further?

for example, all of my epubs are in C:\users\foldername
and some epubs are in its subfolders like
C:\users\foldername\red
C:\users\foldername\blue
etc.

in this case, what is the exact commands for bulk compress to keep new epubs with same name and folder structure?

calibre-debug -r "Editor Chains" "Compress Images" "C:\users\foldername\*.epub" "C:\users\newfoldername\*.epub"

will it work? or can I just specify the folder name so it compresses all the epubs inside?

No. You will need to write a shell script to achieve this. I am a linux user, so I can easily write a bash script that would work only in linux. Unfortunately, I don't know much about Windows Batch scripts to assist with this. Maybe someone else can help with this.
capink is offline   Reply With Quote
Old 01-18-2023, 05:04 PM   #7
BetterRed
null operator (he/him)
BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.
 
Posts: 21,718
Karma: 29711016
Join Date: Mar 2012
Location: Sydney Australia
Device: none
FWIW ==>> How to Install Linux Bash Shell on Windows [Step-by-Step Guide]

BR
BetterRed is offline   Reply With Quote
Old 01-19-2023, 11:16 PM   #8
tatagi
Connoisseur
tatagi began at the beginning.
 
Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
Quote:
Originally Posted by isarl View Post
.
many thanks. yes that's the most foolproof way but at the same time very tiresome to individually edit files one at a time.

Quote:
Originally Posted by capink View Post
.
thank you again. at least with your plugin, It doesn't unnecessarily leave caches by opening first file in editor's GUI interface. very useful for that reason alone.

here's another question. could you please make chain action settings to also remove all the embedded fonts in tools-manage fonts - select all fonts - remove selected fonts? if this is combined with compress images settings, it would be much better.

Quote:
Originally Posted by BetterRed View Post
.
thanks for the tip, I don't know anything about linux but I will see what I can find out .
tatagi is offline   Reply With Quote
Old 01-20-2023, 12:24 AM   #9
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by tatagi View Post
thank you again. at least with your plugin, It doesn't unnecessarily leave caches by opening first file in editor's GUI interface. very useful for that reason alone.

here's another question. could you please make chain action settings to also remove all the embedded fonts in tools-manage fonts - select all fonts - remove selected fonts? if this is combined with compress images settings, it would be much better.
There is already an action called "Remove Files" which can be used to remove any kind of files by specifying the names of the files you want to remove (regex supported), after that you only need to remove any css @font-face rules using a simple regex.

A chain that contains all the above action is attached here. I changed the name of the chain to "Misc Action", so if you want to run it from the command line:

Code:
calibre-debug -r "Editor Chains" "Misc Actions" path_to_input.epub path_to_output.epub
P.S. You can run the chain all books in a calibre library if you have the Action Chains plugin installed. So if you want, you can add all your books to temporary calibre library, select all the books there, run the chain and finally save the files to disk again using the save to functionality of calibre.
Attached Files
File Type: zip misc_actions.zip (573 Bytes, 115 views)

Last edited by capink; 01-20-2023 at 12:31 AM.
capink is offline   Reply With Quote
Old 01-20-2023, 04:10 AM   #10
tatagi
Connoisseur
tatagi began at the beginning.
 
Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
Quote:
Originally Posted by capink View Post
.

that's brilliant thanks a lot you saved me a lot of times and efforts.

In most cases it works like a charm, but this is what I thought.

it's kind of rare issue I can think of, but I tested it with my sample epubs
but the problem is it can delete some files that has .ttf or .otf as part of their name also (e.g "this is the list of .ttf files.html" )

I want to make sure I don't mistakenly delete any crucial files that must be present, regex in this regard is not really fail-proof. any ideas?

Last edited by tatagi; 01-20-2023 at 04:15 AM.
tatagi is offline   Reply With Quote
Old 01-20-2023, 04:58 AM   #11
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
To be absolutely sure, anchor the regex with $ to make sure the .ttf is the last part of the filename:

Code:
(?i).+\.ttf$
capink is offline   Reply With Quote
Old 01-20-2023, 06:42 AM   #12
tatagi
Connoisseur
tatagi began at the beginning.
 
Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
Quote:
Originally Posted by capink View Post
.
I might be so paranoid but it still won't stop it from deleting folder or some files with no extensions that contain .ttf


I rechecked the plugin's release thread https://www.mobileread.com/forums/sh....php?p=4174583

and removing embedded font using editor's builtin tools via editor chains is maybe not supported. (not in currently available actions)

btw I found a way to bulk compress images using for loop commands + editor chains combo. for those interested :

for /r "C:\Users\foldername" %v in (*.epub) do calibre-debug -r "Editor Chains" "Misc Actions" "%v" "%v.epub"

I have yet to find a way to save the output files in separate folder. if any one knows how, please let me know.
tatagi is offline   Reply With Quote
Old 01-20-2023, 08:14 AM   #13
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by tatagi View Post
I might be so paranoid but it still won't stop it from deleting folder or some files with no extensions that contain .ttf
It will not delete directories. And it only deletes files that end with .ttf extension.

Also, a word of advice: Never ever delete the originals. You have been warned.
capink is offline   Reply With Quote
Old 01-20-2023, 10:55 AM   #14
tatagi
Connoisseur
tatagi began at the beginning.
 
Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
Quote:
Originally Posted by capink View Post
It will not delete directories. And it only deletes files that end with .ttf extension.

Also, a word of advice: Never ever delete the originals. You have been warned.
I double checked and confirmed it deletes folders

And I've encountered another problem.

If some image files are missing, the entire action is aborted and gives error message :

Code:
Editor Chains: Starting chain: Misc Actions
Editor Chains: Misc Actions: starting action No. 1: Compress Images
Editor Chains: action "Compress Images" terminated with exception: 'OEBPS/images/img11.jpg'
Traceback (most recent call last):
  File "calibre_plugins.editor_chains.chains", line 177, in _run_loop
  File "calibre_plugins.editor_chains.actions.compress_images", line 89, in run
  File "calibre\ebooks\oeb\polish\images.py", line 87, in compress_images
KeyError: 'OEBPS/images/img11.jpg'
Traceback (most recent call last):
  File "calibre_plugins.editor_chains.cmdline", line 77, in cmdline_run
  File "calibre_plugins.editor_chains.cmdline", line 53, in run_chain
  File "calibre_plugins.editor_chains.action_chains", line 40, in run_editor_chain
  File "calibre_plugins.editor_chains.chains", line 320, in run
  File "calibre_plugins.editor_chains.chains", line 200, in _run_loop
  File "calibre_plugins.editor_chains.chains", line 177, in _run_loop
  File "calibre_plugins.editor_chains.actions.compress_images", line 89, in run
  File "calibre\ebooks\oeb\polish\images.py", line 87, in compress_images
KeyError: 'OEBPS/images/img11.jpg'

it means, only flawless epub can be properly processed..?

Last edited by tatagi; 01-20-2023 at 11:19 AM.
tatagi is offline   Reply With Quote
Old 01-20-2023, 01:57 PM   #15
isarl
Addict
isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.
 
Posts: 287
Karma: 2534928
Join Date: Nov 2022
Location: Canada
Device: Kobo Aura 2
Quote:
Originally Posted by tatagi View Post
many thanks. yes that's the most foolproof way but at the same time very tiresome to individually edit files one at a time.
Why edit them one at a time? With something like ImageMagick you could batch-convert a bunch. I would likely do it something like this, on Linux. Assumes all your image files are jpegs (with extensions of ".jpg" or ".jpeg") and you want a max filesize of 1MB per image file:

Code:
$ unzip -d working_dir "book filename.epub"
$ cd working_dir
$ find . -iname "*.jpg" -o -iname "*.jpeg" -execdir mogrify -define jpeg:extent=1MB '{}' ';'
$ zip -r "book filename.compressed.epub" .
$ mv "book filename.compressed.epub" ..
$ cd ..
$ rm -rf working_dir
Note that ImageMagick's “mogrify” command modifies files in-place. This is (potentially) a DESTRUCTIVE operation!

Last edited by isarl; 01-20-2023 at 02:02 PM. Reason: modify code to use a different filename for the new book, so the mv operation doesn't overwrite the old one.
isarl is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cover image compression ownedbycats Calibre 7 01-23-2021 08:41 AM
Is there a way to execute a non-root command without access to command line Galunid Kindle Developer's Corner 4 05-15-2018 07:50 PM
Kindle Touch-open specific image by command line thaonphuong Kindle Developer's Corner 11 01-12-2016 03:46 AM
2.47 - Image compression not working jlynton Calibre 0 12-25-2015 08:40 AM
Cover Image Compression. Ichi Calibre 3 01-04-2010 06:38 AM


All times are GMT -4. The time now is 05:37 AM.


MobileRead.com is a privately owned, operated and funded community.