Lymphatus / caesium-image-compressor

Caesium is an image compression software that helps you store, send and share digital pictures, supporting JPG, PNG, WebP and TIFF formats. You can quickly reduce the file size (and resolution, if you want) by preserving the overall quality of the image.
https://saerasoft.com/caesium
GNU General Public License v3.0
3.31k stars 202 forks source link

Compress images inside the archive #270

Closed Miraihi closed 3 months ago

Miraihi commented 3 months ago

Hello, Matteo. I'd like to thank you for your work on the best image converter I had pleasure using. Thanks for the "Maximum output size" feature, I haven't seen anything like that anywhere else.

What I'd love to request is the ability to compress images inside the archive. You see, the most common comic format is "cbz", what's essentially a .zip file. There are also ".cbr" and ".cb7" for ".rar" and ".7z" respectively. It'd be very convenient if I could just drag and drop the .cbz file into Caesium and get all the contents compressed, instead of unpacking, compressing, and then archiving again.

Lymphatus commented 3 months ago

Thanks for using Caesium, the kind words and the advice! Unfortunately, supporting different archive file is a bit out of scope and might require a lot of work for a very specific use-case. Supporting .zip archives may come in the future because I also plan to use them for other features, but for .rar/.7z it might require a lot of time. I can give you a suggestion to solve your use-case: Caesium supports importing/compressing passing the list of files by command line. You can leverage this to write a mini-script (different for different operating systems) to do all the work automatically.

Let me show an example on the steps, I can get into more details if you are willing to share your configuration.

  1. Open Caesium, go to Preferences -> When import files directly -> Select "Import and Compress"
  2. Remaining in the Preferences, Perform and action after success compression should be set to "Close the application"
  3. Setup your compression parameters (like level, lossless etc...) and destination folder
  4. You can now close Caesium
  5. Write a small script (.sh on Linux or .bat for Windows) that unzips the comics folder as first thing, then calls Caesium passing the unzipped folder. Something like unzip comics.cbr -d destination_folder && /path/to/Caesium destination_folder && zip -r comics_compressed.cbz caesium_destination_folder (example on Linux machines)
  6. If you run the script, it will unzip, import and compress into Caesium, which will close itself automatically and run the archive process

It's not completely straightforward and requires a bit of "hacking", but it's possible. If you are on Windows, 7zip has a command line interface and I can help you come out with an automated script, if you want. I could also test it myself as what I told you is something that just came up to my mind.

Lymphatus commented 3 months ago

Another alternative would be using caesium-clt, which is basically the same as this project, just without an UI. Would require writing a script like I said, but with less setup. The only thing is that caesium-clt it's a little bit behind in features as I lack time to maintain both projects.

Miraihi commented 3 months ago

Thank you for the solution. Unfortunately I don't have any experience writing batch scripts so it'll require some learning from me. I'm mostly concerned about .zip/.cbz format so that compatability alone would be great.

In the meantime I'd be satisfied with a script that unzips all the .cbz files in one folder, compresses them, and then puts the compressed .cbz files in another folder. Not sure how possible it is to automate.

Lymphatus commented 3 months ago

This is an example using 7zip. Just tested on my machine and seems to work. Remember to setup Caesium as I wrote before before running the script.

"C:\Program Files\7-Zip\7z.exe" x -y -o"C:\Users\<your-user-name>\Pictures\MyFolder" "C:\Users\<your-user-name>\Pictures\MyFolder\auto_test.zip"
"C:\Program Files\Caesium Image Compressor\Caesium Image Compressor.exe" "C:\Users\<your-user-name>\Pictures\MyFolder\auto_test"
"C:\Program Files\7-Zip\7z.exe" a "C:\Users\<your-user-name>\Pictures\MyFolder\compressed.zip" "C:\Users\<your-user-name>\Pictures\MyFolder\caesium_destination"

If you write this into a myscript.bat file and run it, it will uncompress C:\Users\<your-user-name>\Pictures\MyFolder\auto_test.zip, import it to Caesium and compress (the destination folder of the compression is set into Caesium itself). Caesium will close and then 7zip will run again to compress C:\Users\<your-user-name>\Pictures\MyFolder\caesium_destination into an archive. It might require a little bit of tweaking, especially on 7zip parameters, depending on the comics archive structure. But should be a good starting point.

Miraihi commented 3 months ago

Well, after some research I've found way to implement what I want. Thanks for showing me where to start! This batch file compresses all the .cbz files in E:\Outputs\ and puts the newly compressed versions in E:\Outputs\Compressed\

set caesium="C:\Program Files\Caesium Image Compressor\Caesium Image Compressor.exe"
set archive="C:\Program Files\7-Zip\7z.exe"
for %%f in (*.cbz) do (
mkdir "E:\Outputs\%%~nf"
%archive% x -y -o"E:\Outputs\%%~nf" "E:\Outputs\%%f" 
%caesium% "E:\Outputs\%%~nf"
%archive% a -mx9 "E:\Outputs\Compressed\%%~nf.cbz" "E:\Outputs\%%~nf" 
rd /s /q "E:\Outputs\%%~nf"
)

The only gripe with this script is that if there's another folder inside the source .cbz archive, it won't compress anything. In other words, it's non-recursive, and I haven't found any way to fix this (My collection is a bit of a mess and sometimes there's another folder inside the archive).

Lymphatus commented 3 months ago

The only gripe with this script is that if there's another folder inside the source .cbz archive, it won't compress anything.

Are you referring to Caesium not importing and compressing? If you input a folder that contains only other folders, you should check the option "Import files in subfolders when opening a directory" under Preferences. That allows Caesium to recursively open directories. If it's 7zip that does not correctly unzip, or there's another zip inside, you might want to look for something like this.

Glad I helped find a solution!

Miraihi commented 3 months ago

If you input a folder that contains only other folders, you should check the option "Import files in subfolders when opening a directory" under Preferences

Totally forgot about that option. Thanks, worked like a charm!