guetzli icon indicating copy to clipboard operation
guetzli copied to clipboard

How to do batch compress images?

Open abcfy2 opened this issue 7 years ago • 21 comments

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?

abcfy2 avatar Mar 19 '17 03:03 abcfy2

👍

teleject avatar Mar 19 '17 03:03 teleject

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.

bherila avatar Mar 19 '17 05:03 bherila

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

:)

abcfy2 avatar Mar 19 '17 06:03 abcfy2

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

asm-man avatar Mar 19 '17 06:03 asm-man

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

chai2010 avatar Mar 19 '17 09:03 chai2010

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.

abcfy2 avatar Mar 21 '17 03:03 abcfy2

Edited: I did it one way here

error-0x29A avatar Mar 21 '17 04:03 error-0x29A

Nice work @abcfy2 , your find command is even better than what I had in mind!!

bherila avatar Mar 22 '17 00:03 bherila

@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 avatar Mar 23 '17 18:03 FrankDraws

@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 {} {}

abcfy2 avatar Mar 24 '17 01:03 abcfy2

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

yraen69 avatar Mar 24 '17 14:03 yraen69

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 .

thehelp1 avatar Mar 24 '17 15:03 thehelp1

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 \;

smithaa02 avatar Mar 25 '17 17:03 smithaa02

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 guetzli

gerfra avatar Mar 29 '17 10:03 gerfra

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.

romainmenke avatar Mar 29 '17 17:03 romainmenke

@gerfra When I downloaded the package above windows complained about a virus

mosa99 avatar May 08 '17 16:05 mosa99

try KrojamSoft BatchRename

tulpan avatar May 31 '17 14:05 tulpan

Get

atomissariyaporn avatar Jun 29 '17 13:06 atomissariyaporn

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

wstoettinger avatar Aug 11 '17 17:08 wstoettinger

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.

imakatman avatar Jan 21 '19 21:01 imakatman

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

retroindex avatar Feb 27 '20 07:02 retroindex