![]() |
#1 |
Connoisseur
![]() 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. |
![]() |
![]() |
![]() |
#2 | |
Junior Member
![]() 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:
|
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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:
Note: You can configure the compression ratio by modifying the settings of the chain. |
![]() |
![]() |
![]() |
#4 |
Connoisseur
![]() Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
|
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? Last edited by tatagi; 01-18-2023 at 05:36 AM. |
![]() |
![]() |
![]() |
#5 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 287
Karma: 2534928
Join Date: Nov 2022
Location: Canada
Device: Kobo Aura 2
|
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).
|
![]() |
![]() |
Advert | |
|
![]() |
#6 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
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. |
|
![]() |
![]() |
![]() |
#7 |
null operator (he/him)
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 21,718
Karma: 29711016
Join Date: Mar 2012
Location: Sydney Australia
Device: none
|
|
![]() |
![]() |
![]() |
#8 |
Connoisseur
![]() Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
|
many thanks. yes that's the most foolproof way but at the same time very tiresome to individually edit files one at a time.
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. thanks for the tip, I don't know anything about linux but I will see what I can find out . |
![]() |
![]() |
![]() |
#9 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
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 Last edited by capink; 01-20-2023 at 12:31 AM. |
|
![]() |
![]() |
![]() |
#10 |
Connoisseur
![]() Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
|
that's brilliant ![]() 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. |
![]() |
![]() |
![]() |
#11 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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$
|
![]() |
![]() |
![]() |
#12 |
Connoisseur
![]() Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
|
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. |
![]() |
![]() |
![]() |
#13 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
Also, a word of advice: Never ever delete the originals. You have been warned. |
|
![]() |
![]() |
![]() |
#14 | |
Connoisseur
![]() Posts: 52
Karma: 10
Join Date: Oct 2022
Device: none
|
Quote:
![]() 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. |
|
![]() |
![]() |
![]() |
#15 | |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 287
Karma: 2534928
Join Date: Nov 2022
Location: Canada
Device: Kobo Aura 2
|
Quote:
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 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. |
|
![]() |
![]() |
![]() |
|
![]() |
||||
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 |