guetzli
guetzli copied to clipboard
How to do batch compress images?
Imagemagick
is a good tool that can support batch compress images like:
mogrify -verbose -format jpg -quality "85%" **/*.png
But how to do this by using guetzli?
👍
You can use the find
command with -exec or -xargs (see man find
) or, on Windows, you can loop over files in PowerShell and pass them into the command line tool.
Thanks @bherila . But it seems a little inefficient and complex. I have to handle all of the output file names manually.
I wish guetzli could support:
guetzli **/*.png
:)
From the command prompt in Windows, you could use the "for" command to loop through a batch of files:
for %x in (*.png) do guetzli %x %x.jpg
This would, unfortunately, create output files with the doubled extension ".png.jpg". They could subsequently be fixed by following up with this command:
ren *.png.jpg *.jpg
I create a golang bind for guetli, and the apps/gutzli
command support batch compress.
https://github.com/chai2010/guetzli-go https://github.com/chai2010/guetzli-go/blob/master/apps/guetzli/main.go
I've found a good method to do batch compress on bash.
find -name '*.png' -print0 | xargs -0 -I{} -P0 guetzli --verbose --quality 85 {} {}.jpg
This command uses xargs
with -P
flag, can use full cpu.
Edited: I did it one way here
Nice work @abcfy2 , your find
command is even better than what I had in mind!!
@abcfy2 Nice job on the batch compress command.
How do I go about optimizing JPGs in a folder and instead of renaming, just overwriting them all?
@FrankDraws I don't find any good ways to use variables in xargs
. I have to use echo | sed
to replace the suffix .png
to .jpg
.
find -name '*.png' -print0 | xargs -0 --no-run-if-empty -I{} -P4 guetzli --quality 85 --verbose {} $(echo {} | sed 's/\.png$/.jpg/')
If all of your images are jpg
files:
find -name '*.jpg' -print0 | xargs -0 --no-run-if-empty -I{} -P4 guetzli --quality 85 --verbose {} {}
On Windows you can use PowerShell 4.0 workflows with multi threading to loop over folders full of images with some sort of speed... I tried this on an 8 core Intel CPU had it quite happily filling 6 of those cores ... leaving two to browse the wonders of the internet.
#ROUGH AS NAILS
#MAKE SURE YOU CHANGE THE PATHS TO MATCH YOUR COMPUTER!
#1. Change USERNAME to be your own!
#2. Create a folder on your desktop called New Folder (or change the folder name in the script to one that exists)
#3. Expects JPG file extension (for PNG you'll have to work out renaming (maybe something like [io.path]::GetFileNameWithoutExtension($fullName)+".png")
#3. This script doesn't destroy your original JPGs it makes copies and puts them in a shrunk subfolder
#4. Change the ThrottleLimit to match how many CPU's you want this thing to use
#5. Change the path to Guetzli exe on your system (mines in C:\temp and is the 64bit EXE)
Workflow Go-Parallel { $files = Get-ChildItem "C:\Users\USERNAME\Desktop\New folder" -Filter *.jpg ForEach -Parallel -ThrottleLimit 6 ($file in $files) {
$fullName = "C:\Users\USERNAME\Desktop\New folder" + $file $newName = "C:\Users\USERNAME\Desktop\New folder\shrunk" + $file
#If the shrunk folder doesn't exist it then make it
if((Test-Path "C:\Users\USERNAME\Desktop\New folder\shrunk") -eq 0)
{
mkdir "C:\Users\USERNAME\Desktop\New folder\shrunk";
}
InlineScript { C:\temp\guetzli_windows_x86-64.exe --quality 96 --verbose $Using:fullName $Using:newName } }
} Go-Parallel -Verbose
https://circleci.com/docs/1.0/config-sample/
Check out the sample guys.
On Mar 21, 2017 6:31 PM, "Ben Herila" [email protected] wrote:
Nice work @abcfy2 https://github.com/abcfy2 , your find command is even better than what I had in mind!!
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/guetzli/issues/80#issuecomment-288262107, or mute the thread https://github.com/notifications/unsubscribe-auth/AZNZWENG1YgiN7BemmGSfeo-MXm3cGlUks5roGvZgaJpZM4Mhncj .
Xargs is nice but it will try to process all the images at once which can overwhelm your computer. If you have say 50 images, this will mean 50 spawned instances of guetzli which big deal as just one process is very resource intensive.
You can throttle down your Xargs size, or you can simply use -exec. Here is a sample command that works for me by processing the images safely one at a time...
cd (to image directory of pngs to be converted) find . -name '*.png' -print0 -exec guetzli --quality 95 {} {}.jpg \;
I writted a Windows Gui for Windows user and yes you can compress multiple file at once, like batch mode.
Have nice day
https://github.com/gerfra/GuetzliBG
Little tool I wrote to manage compressing an entire folder: https://github.com/romainmenke/simple-guetzli
It doesn't do overwrites but it does saves you time by tracking which files have already been compressed.
@gerfra When I downloaded the package above windows complained about a virus
try KrojamSoft BatchRename
Get
based on the solution of @yraen69 (thanks! for the idea! unfortunately it didn't work for me) I created the following script:
Workflow Go-Parallel {
# the path to the directory including the guetzli.exe and a folder 'input' with all images
$p = "C:\path\to\guetzli"
# the output quality, 84 is the lowest which is allowed in the standard guetzli compilation.
# if you want to go below, you have to compile it yourself
$q = '84'
$command = $p + "\guetzli.exe"
$in_path = $p + "\input"
$out_path = $p + "\output"
if((Test-Path $out_path) -eq 0) {
mkdir $out_path;
}
$files = Get-ChildItem $in_path -Filter *.jpg
ForEach -Parallel -ThrottleLimit 6 ($file in $files) {
$newName = $out_path + "\" + $file.Name
$params = '--quality', $q, '--verbose', $file.FullName, $newName
InlineScript {
& $Using:command $Using:params
}
}
}
Go-Parallel -Verbose
Create a guetzli.ps1 script and execute it in PowerShell.
If you want to use it out of the box, use the following folder structure: guetzli/ +- guetzli.exe +- input/*.jpg +- output/
Cheers
You could also create a bash script. Here's mine:
compressjpg(){
for i in *.jpg; do
echo "Compressing $i..."
guetzli $i ${i%%.*}-comp.jpg
echo "Finished compressing $i!"
done
}
My scripts are in .profile (and I'm using a Mac), but this might not be the case with everybody. Another common file is .bash_profile.
Imagemagick
is a good tool that can support batch compress images like: mogrify -verbose -format jpg -quality "85%" **/*.png But how to do this by using guetzli?
Most simple batch file (no doubled extensions): for %%i in (*.png) do guetzli.exe --quality 85 "%%i" "%%~ni.jpg