View Single Post
Old 12-12-2022, 08:53 PM   #5
LostOnTheLine
Connoisseur
LostOnTheLine ought to be getting tired of karma fortunes by now.LostOnTheLine ought to be getting tired of karma fortunes by now.LostOnTheLine ought to be getting tired of karma fortunes by now.LostOnTheLine ought to be getting tired of karma fortunes by now.LostOnTheLine ought to be getting tired of karma fortunes by now.LostOnTheLine ought to be getting tired of karma fortunes by now.LostOnTheLine ought to be getting tired of karma fortunes by now.LostOnTheLine ought to be getting tired of karma fortunes by now.LostOnTheLine ought to be getting tired of karma fortunes by now.LostOnTheLine ought to be getting tired of karma fortunes by now.LostOnTheLine ought to be getting tired of karma fortunes by now.
 
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 Max Width all images larger than will be downscaled to. If {maxHeight} removed only the Width will be used
  • -resize {maxWidth}
"maxHeight":
  • The Max Height all images larger than will be downscaled to. If {madWidth} removed only the Height will be used
  • -resize x{maxHeight}
If BOTH are set
  • All images larger than those dimensions will be shrunk to fit inside.
  • 1200x2000 will result in 540x900 with the defaults 600x900 set
  • -resize {maxWidth}x{maxHeight}
"qualityNum":
  • The Quality all images being downscaled will be saved as
  • -quality {qualityNum}
    • JPEG= Quality Level 1-100
    • PNG= Compression Level <10=0,10-19=1,20-29=2,90-99=9
  • If none will attempt to use the same as the original image or use 92 if it cannot determine per ImageMagick standards
"tallImages":
  • If set to True will rotate all images that are wider than tall 90° Counterclockwise
  • -rotate -90>
"trimMaps":
  • If set to True will trim borders on all images with the word "map" in the name (Case ignored)
  • -fuzz 18% -trim
"trimComics":
  • If set to True will trim borders on all images with the word "comic" or "manga" in the name (Case ignored)
  • -fuzz 1% -gravity Center -define trim:minSize={int(w*0.9)}x{int(h*0.9)} -trim
"customArg":
  • A place to put any custom arguments to be added to the commands for all images.
  • All ","(Commas) & " "(Spaces) are treated as argument separators so they will end up formatted properly
    • -fuzz_____1%,_-gravity,__Center will end up as -fuzz_1%_-gravity_Center
"stripComments":
  • If set to True will remove any comments or text chunks from images
  • -strip

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:
  • Adding the ability to Detect Low Color Images like Logos that have only 1-8 unique colors & converting them to indexed.
  • I currently have it set to ignore images smaller than 100kb, this is mostly to solve the problem of it trying to rotate a wide logo image when {tallImages} is set & because the quality set to 90 can shrink the resolution size of some logos while making the fileSize increase. Trying to find a better way around this, possibly if the original size is <100kb or something setting a {logoQuality} Setting, but as logos are often PNG that would increase the size by lowering the compression. Also some of my test books have logos that are 3000x1300 pixel wide PNGs when the images are 1200x1700 so resolution cannot be used for this. Trying to find a better way to do that.
  • Trying to improve the Comic Crop to properly Center the crop in the Initial image instead of the detected borders. I got it to a point I consider acceptable, but it's still not ideal. The goal is to have it detect the size of the border, add a buffer, then remove the same amount as the furthest point from all sides. Once I get this sorted out I plan to add the ability to Separate the Top & Bottom Crop & give Separate Border Buffer arguments to those
  • Possibly add a {comicBorder} argument to the Preferences JSON
    • Biggest Hurdle: It's not intuitive to have to say 90 to have it set the max Border size to be 90% of the original
    • Other Hurdle: this value will need to be converted to decimal so will need to be a pure number, 90% will cause a fault
  • ADDED IN 0.0.3 - Add an option to use the Larger of the {maxWidth}x{maxHeight} for the rescale. E.g. In the example given:
    • All images larger than those dimensions will be shrunk until ONE side is small enough.
    • 1200x2000 will result in 600x1000 with the defaults 600x900 set
    • -resize {maxWidth}x{maxHeight}^
  • Add a -Filter Argument instead of requiring it to be set in {customArgs}
    • Hurdle: Any entry not exactly as an available option will result in a fault. This would be easy with a GUI UI where a dropdown could select them, but that is beyond me
Attached Files
File Type: zip ImgShrinker-Alt_v0.0.2.zip (5.3 KB, 112 views)
File Type: zip ImgShrinker_v0.0.3.zip (5.9 KB, 103 views)

Last edited by LostOnTheLine; 01-01-2023 at 09:21 AM.
LostOnTheLine is offline   Reply With Quote