For some stupid reason I ended up having to convert a whole lot of HTML files to images. Not wanting to waste space, the plan was to compress the files as much as possible without loosing too much quality.
So here’s how to do it:
for f in *.html; do
wkhtmltoimage -f png $f $f.png
mogrify -trim $f.png
pngquant --speed 1 $f.png --ext .png.o
mv $f.png.o $f.png
doneHere’s what each line does:
for f in *.html; dowill loop through each.htmlfile in the current directorywkhtmltoimage -f png $f $f.pngwill convert the.htmlfiles to.png(see the wkhtmltoimage project page)mogrify -trim $f.pngwill auto-crop the image and cut off the body padding, this saves a few bytes (see the mogrify documentation)pngquant --speed 1 $f.png --ext .png.ocompresses the.pngfiles (note that pngquant uses lossy compression; to compress losslessly, use pngcrush instead)mv $f.png.o $f.pngreplaces the original png with the optimised one