01-02-2022, 02:41 PM | #1 |
2B || !2B
Posts: 851
Karma: 194010
Join Date: Feb 2010
Location: Austria
Device: Sony PRS505/650/T1/tolino vision 5
|
[Plugin] ImgShrinker (ImageMagick-Wrapper)
Released: January 2, 2022
Current Version: "0.0.1" This plugin uses the ImageMagick command line utility to reduce the width of Image files while keeping the aspect-ratio. Mostly for the use with Sony-PRS-Readers as some larger images will slow those down considerably. (And because I'm doing this on an regular basis, and so -after all - it is worth to get a script up to do the job.) Disclaimer: Since this plugin will replace the images in your ebook with resized images, you might want to save your ePub or create a checkpoint before running this plugin. Credits Doitsu: this is mostly based on his JPEGOptimizer - Lossless image optimization Plugin. Even this posts source is shamelessly copied. System requirements Windows-OS (though, it should be possible to add MacOs and Linux support, but I have no chance to test on these systems) This plugin requires at least Sigil 0.9.8. If you uncheck the Use Bundled Python option, you'll also need to install Python 3. Installation 1. Select Manage Plugins from the Plugins menu and enable the Use Bundled Python option, if it isn't already enabled (or greyed out). 2. Click Add Plugin and select ImgShrinker_v0.0.1.zip. This will install the plugin, which you can select via Plugins > Edit > ImgShrinker. 3. Download and install ImageMagick - remember the path to the binary - it has to be set on first run Settings To select the new maxWidth, edit ImgShrinker.json and change the maxWidth value. For example: Code:
"maxWidth": "600", Windows users can also press WIN+R, paste %localappdata%\sigil-ebook\sigil\plugins_prefs\ImgShrinker into the Run dialog box and hit Enter. License: GNU General Public License v3 (GPL-3) Extended version with more options by LostOnTheLine down in Post#5 Last edited by Mark Nord; 12-19-2022 at 10:33 AM. |
01-03-2022, 12:16 PM | #2 |
Sigil Developer
Posts: 8,099
Karma: 5450184
Join Date: Nov 2009
Device: many
|
I will add this to the Sigil Plugin Index.
Thank you! ps. Now added. Last edited by KevinH; 01-03-2022 at 12:22 PM. |
Advert | |
|
12-06-2022, 07:52 AM | #3 |
Connoisseur
Posts: 71
Karma: 800000
Join Date: Jun 2021
Device: Kindle Paperwhite (PW1|PW3|PW4), Kindle Voyage
|
Is there any way to adjust other settings aside from the width?
Before I started using Sigil I was unzipping epubs, using BIMP to batch convert & shrink images in GIMP, turning wide images, then re-zipping them. I generally batch convert by the height rather than the width, this is especially necessary when there are wide images that have been rotated. I also use JPG with quality levels typically set to 90, 1x1 Subsampling, Low-Halo interpolation for the scaling, float-point DCT, etc. I generally sacrifice conversion time for better quality/size because it can work in the background. Using the plugin I was very disappointed with the size difference (I typically shrink a 30-40MB ePUB down to 4-10MB), The Plugin made the images smaller than my old method & the 37MB ePUB I was testing on only got down to 18MB despite the images being smaller. If I could adjust at least the quality setting that'd make a huge difference as I'm fairly certain it's keeping the quality level at around something like 98% which is a lot more than I personally need. When calibre converts my ePUBs to KFX the size ends up near to what I was getting using this method, I'd just like to have the ePUB at that resolution & AZW3 as well Since the "maxWidth": "1000" is set via the JSON it would be nice to have other options, like "maxHeight": "1200", "Quality": "90", "Interpolation": "LowHalo", "DCT": "FloatingPoint" I appreciate that this exists, I just wish there were more options. Last edited by LostOnTheLine; 12-07-2022 at 12:28 PM. Reason: trying to ensure no misunderstandings |
12-12-2022, 05:19 PM | #4 | |
Connoisseur
Posts: 71
Karma: 800000
Join Date: Jun 2021
Device: Kindle Paperwhite (PW1|PW3|PW4), Kindle Voyage
|
From the documents on ImageMagick Geometry it looks like you are using the widthxheight argument with a set width, & I believe the same would be used for a "maxHeight": "###" value
Okay, so I looked at the plugin.py & it looks like I can set the "maxWidth": "x####" to be MaxHeight instead. So "maxWidth": "x1000" = MaxHeight@1000 Interpolation would be under -filter but it seems like there are a lot of arguments & options. Would it be possible to add a "custom arguments" field that could be populated with any of the CLI Options available for ImageMagick? that alone would make it so that any of the options I want, so long as I know what ImageMagick calls for them, would be able to be set. you could include a link to https://imagemagick.org/script/command-line-options.php in the docs so people could find & set their own, we just need a way to make it work with the plugin. It looks like the line Quote:
Alright. I'm making a modified version that includes more options I didn't realize I never sent this. I have gotten to a decent point & will post a modified version of this plugin in a moment |
|
12-12-2022, 08:53 PM | #5 |
Connoisseur
Posts: 71
Karma: 800000
Join Date: Jun 2021
Device: Kindle Paperwhite (PW1|PW3|PW4), Kindle Voyage
|
Modified version of the Plugin (I labeled it v0.0.2)
So I have added some arguments to the Preferences JSON that can be set.
I'm by no means a programmer or much experienced with Python, so my code is certainly not very elegant. The original author or anyone else is free, even encouraged, to modify it to make it better. For now, I'm having python detect the image dimensions from the file, then do a 90% integer from those measurements & use that, with the center, for the Manga/Comics Border Trim. It's not perfectly centered (it centers on where the detected borders on not the original image), but it's at least a workable solution. There is a request in for ImageMagick to make the trim:minSize= options have the ability to set a percentage directly, that will be much better, but until it exists this is the best I could do with my knowledge. The ImgShrinker.json Plugins_Prefs file has some guides & instructions added as values since JSON does not allow comments. These can be removed & will only be recreated if the ImgShrinker.json file is removed. I'm not sure the proper way to give instructions for these, Ideally I'd like to see these made into an option in the UI, but that's beyond me so it is what it is. Would be nicer if it used YAML but this is the best I can do with the available system. It currently offers the following options to be applied to images in an ePUB "maxWidth":
The Full Argument string for an image if everything was called, (with Map Arguments not present, they will be replacing Comic ones for Maps) Code:
{magick_exe_path} mogrify -quality {qualityNum} -fuzz 1% -gravity Center -define trim:minSize={int(w*0.9)}x{int(h*0.9)} -trim {customArg} -rotate '-90>' -strip -resize {maxWidth}x{maxHeight} -path {temp_dir} {original_img_path} If there's any other arguments someone thinks should be options for an ePUB please let me know. It started with only the option to set the {maxWidth} & nothing else, so I've been adding things I think would be useful but I'm sure others havve different wants than I do Things I'm still working on:
Last edited by LostOnTheLine; 01-01-2023 at 09:21 AM. |
Advert | |
|
12-17-2022, 07:48 AM | #6 |
Guru
Posts: 670
Karma: 4568205
Join Date: Jan 2010
Location: Sweden
Device: Kobo Forma
|
Would it be possible to add the feature to also make them grey-scale (if color)?
|
12-19-2022, 11:22 AM | #7 | |
2B || !2B
Posts: 851
Karma: 194010
Join Date: Feb 2010
Location: Austria
Device: Sony PRS505/650/T1/tolino vision 5
|
Quote:
with " -colorspace LinearGray" or " -colorspace Gray" Taken from the recommended ImageMagick-Documentation Last edited by Mark Nord; 12-19-2022 at 11:25 AM. |
|
12-31-2022, 09:25 AM | #8 | ||
Connoisseur
Posts: 71
Karma: 800000
Join Date: Jun 2021
Device: Kindle Paperwhite (PW1|PW3|PW4), Kindle Voyage
|
Quote:
Sorry it took so long to reply, I'm new to this forum & expect from others like StackExchange, Reddit, Github, DockerHub, etc, etc, to get some kinda notification when there's a reply. I'll try to check back every few days at least now that I know it doesn't Quote:
But do note that from my experiences the size difference is usually negligible for converting grayscale images that are saved as RGB & I think ImageMagic does that automagically if the only colors are grays. When reducing quality to 90 I personally don't greyscale the couple of color images for my kindle because I generally already shrink the ≈40MB book to roughly 5MB & grayscaling a couple images only reduces that to like 4.8MB. I've also noticed that if the images are indexed the file size often increases when converting to grayscale, so I've personally stopped as doing side by side versions & saving like 200-500kb on most books but then gaining 1-3MB on others I decided was counter productive. I'll put that in the instructions value for the Grayscale option (in a much sorter way) but I'd definitely suggest trying with it on & off & comparing before deciding to use it. Thanks for the suggestion. It's an obvious one but one I didn't think of. I want this to be as easy & customizable as possible, & I'm trying to figure out how to make a GUI settings menu, but it seems my only option is to make it so you have to see the settings menu each run & click-to-start or have no GUI way to do it. I'd be interested to hear if anyone has thoughts on this or suggestions on a better way, or if people just prefer editing JSON. If it were YAML I'd probably not bother but JSON I feel is less intuitive & non-programer friendly. Last edited by LostOnTheLine; 12-31-2022 at 09:35 AM. Reason: Clarifying |
||
12-31-2022, 11:53 PM | #9 |
Connoisseur
Posts: 71
Karma: 800000
Join Date: Jun 2021
Device: Kindle Paperwhite (PW1|PW3|PW4), Kindle Voyage
|
I just did a test of the different options & it seems the LinearGray gives a darker end image so I plan to give both options
As for size, here's a comparison. Taken from just the folder OEBPS/Images Test ePub start size: - - - - - - - 38.8MB - - Original size no colorspace : - - 7.14MB - - 31.66MB Saved LinearGray: - - - - - 6.75MB - - 390KB difference Gray: - - - - - - - - - - 6.28MB - - 860KB difference This book has 9 color images with 4 being mostly grey with a splash of color & the other 5 being full, rich, color The text in images look much better with LinearGray, but the images, in general look overly dark The image shades look more faithful to the original (IMO) with Gray but the text is less crisp in the images with text, still readable, but less crisp, even slightly less crisp than the color one with no colorspace adjustment Last edited by LostOnTheLine; 12-31-2022 at 11:57 PM. |
01-01-2023, 03:16 AM | #10 |
Connoisseur
Posts: 71
Karma: 800000
Join Date: Jun 2021
Device: Kindle Paperwhite (PW1|PW3|PW4), Kindle Voyage
|
So it looks like we're going to have 4 options. I opted for using integers instead of strings because that way if someone uses "colorGray": true it will give the same result as "colorGray": 1 which is the one I believe most will want. The instructions will say as much, but out off habit I expect some to put "colorGray": "1" which will result in it skipping the grayscale altogether. I think that's the best solution, but am happy to hear arguments saying otherwise & it'll be easy enough to change if people prefer the other.
The options I have now are: 1 -colorspace Grey 2 -colorspace LinearGray 3 -grayscale rec601luma 4 -grayscale rec709luma Technically 2 & 4 should actually be the same thing, & they appear, to me, to be, but I figure some may want 1 or the other not knowing they are the same or maybe there are slight differences that I can't see in the images I tested with. Point in fact the rec601luma looks identical in most images but gives more distinction between a red & green item that look the same gray in 709/Gray & different in 601. It also preserves a gradient that fades from dark to light blue in a couple images better with more variance & a cleaner fade the same with red fades in another. I can't really say it's better, I just like the way it looks for these images better, maybe because they originated in a 601 colorspace, while 709, the current standard, may have the same thing in reverse for other books. As such I've left both options for users to pick. |
01-01-2023, 05:38 AM | #11 |
Connoisseur
Posts: 71
Karma: 800000
Join Date: Jun 2021
Device: Kindle Paperwhite (PW1|PW3|PW4), Kindle Voyage
|
I updated Comment 5 with the new version containing the Grayscale options. I'll also add it here.
I ended up making it so it will accept either colorGray=1, colorGray="1", & even colorGray=true Probably sloppy code-wise but it works & less chance for errors. I did learn that there are at least 2 other Grayscale options possible, but it was taking too long to find them so mayhaps they'll be added in a future update. EDIT: I realized the "shrink so 1 side fits" option was new as well, so that's also included in this update Last edited by LostOnTheLine; 01-01-2023 at 11:36 PM. |
01-02-2023, 06:52 AM | #12 |
Guru
Posts: 670
Karma: 4568205
Join Date: Jan 2010
Location: Sweden
Device: Kobo Forma
|
Thank you! Works great! :-)
And thank you Mark as well. PS. As a curiosity, even though I done this manually earlier, this got triggered for me when I recently bought an epub which was over 500MB! Probably the largest I've ever bought. Full of large color images. |
01-03-2023, 03:09 PM | #13 | |
Connoisseur
Posts: 71
Karma: 800000
Join Date: Jun 2021
Device: Kindle Paperwhite (PW1|PW3|PW4), Kindle Voyage
|
Quote:
I've seen some that size, but they are always comics & usually CBZ format which isn't easy to get looking nice in ePub Anyway, glad it was useful! |
|
01-04-2023, 10:37 AM | #14 |
Guru
Posts: 670
Karma: 4568205
Join Date: Jan 2010
Location: Sweden
Device: Kobo Forma
|
A non-fiction (Marie Kondo's latest. Lots of large color images.).
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Plugin] ACE - DAISY EPUB Accessibility Checker wrapper | Doitsu | Plugins | 37 | 07-15-2024 11:38 AM |
ImageMagick for 2.5.8 DXG anyone? | _spacemonkey | Kindle Developer's Corner | 11 | 12-08-2018 09:15 PM |
Aura Any tutorial to install ImageMagick | alexyung | Kobo Developer's Corner | 7 | 10-16-2017 02:08 PM |
Change "Remove ImageMagick" Breaks Generate Cover Plugin | JimmXinu | Development | 6 | 05-13-2016 12:07 AM |
[Plugin] CSSLint - Simple CSSLint wrapper | Doitsu | Plugins | 1 | 03-23-2016 06:51 PM |