Skip to content

Instantly share code, notes, and snippets.

@mmdmthr
Forked from ahadyekta/convert
Last active December 4, 2021 13:11
Show Gist options
  • Select an option

  • Save mmdmthr/840a6338004b4a58c3103545c657f311 to your computer and use it in GitHub Desktop.

Select an option

Save mmdmthr/840a6338004b4a58c3103545c657f311 to your computer and use it in GitHub Desktop.
Batch convert JPG to WebP in Ubuntu
#First install the webp converter by this
sudo apt-get install webp
#go inside the directory where all images are there
#make sure all images are in RGB color mode, otherwise you will get error for CMYK images.
#Convert all images to RGB by this command (you should install ImageMagik to do that)
for f in *.jpg; do convert -colorspace RGB "$f" "${f}"; done
#finally convert all images to Webp format
for f in *.jpg; do cwebp -q 90 "$f" -o "${f}".webp; done
#Now you have filename.jpg.webp beside filename.jpg for all your images.
#You can set the nginx or other webservers to conditionally show webp instead of jpg if browser support
#Read this page to setup it in nginx config : https://github.com/uhop/grunt-tight-sprite/wiki/Recipe:-serve-WebP-with-nginx-conditionally
# resize image to width 25, keeping aspect ratio
convert -geometry 25x src/image1.png out/image1.png
# resize image to height 25, keeping aspect ratio
convert -geometry x25 src/image1.png out/image1.png
# concatenate images horizontally
convert +append src/image1.png src/image2.png out/image12horiz.png
# concatenate images vertically
convert -append src/image1.png src/image2.png out/image12vert.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment