Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run guetzli on all Jpeg images in a folder, and subfolders

I have just com across https://github.com/google/guetzli - And would like to write a script to run it on all jpeg images in a project on windows.

How would I loop though, all files in the folder and sub folder and get the relative paths to input into the command? Finally as guetzli is reasonably slow is there a way to wait for the execution to finish or put a delay in?

Thanks, Lewis

like image 399
Lewis Quaife Avatar asked Oct 18 '22 15:10

Lewis Quaife


2 Answers

For anyone interested, this is the code I went for;

setlocal disableDelayedExpansion
for /f "delims=" %%A in ('forfiles /s /m *.jpg /c "cmd /c echo @relpath"') do 
(
  set "file=%%~A"
  setlocal enableDelayedExpansion
  echo !file:~2!
  CALL "guetzli.exe" --quality 85 !file:~2! !file:~2!
  endlocal
)

Just put it into put it into the root directory with the guetzli executable

like image 106
Lewis Quaife Avatar answered Oct 21 '22 03:10

Lewis Quaife


I just had to do this, and wrote these simple few lines of Python to handle it. No attempt here to be super-Pythonic. Only numeric jpgs is specific to the folder I was handling. Had to use quality=95 because of compression artifacts on very light skin tones still noticeable at 90. Still went from 6.4meg to 2.2meg for 25 420x280 jpg images (originals were max quality). At qual=85 size was 1.7meg, so 0.5meg for noticeable reduction in artifacts is acceptable. Stil big savings - Guetzli is great. HTH. -Steve

import os
import time
import subprocess

files = os.listdir('./')

jpgs = []
for file in files:
    if file[len(file)-3:] != 'jpg':
        continue
    try:
        fnum = int(file.split('.')[0])
    except:
        continue
    jpgs.append(file)
    jpgs.sort()


todo = len(jpgs)
done = 1
print '# %d = todo' % (todo)
start_tot = time.time()
for jpg in jpgs:
    fnum = int(jpg.split('.')[0])
    cmd = 'guetzli --quality 95 %d.jpg %d_g.jpg' % (fnum, fnum)
    print '# %d of %d' % (done, todo)
    print '# %s' % (cmd)
    start_img = time.time()
    subprocess.call(cmd, shell=True)
    t_this = time.time() - start_img
    t_tot = (time.time() - start_tot)
    avg = t_tot / float(done)
    done += 1
    print '#---%d done: %0.2f sec, %0.2f avgSec, %0.2f minTot' % ( fnum, t_this, avg, t_tot/60.0)
#end
like image 23
stevep Avatar answered Oct 21 '22 03:10

stevep