Skip to content

Instantly share code, notes, and snippets.

@KristobalJunta
Last active January 2, 2022 15:15
Show Gist options
  • Save KristobalJunta/a190c6c4055612e1b163f53e291aafb8 to your computer and use it in GitHub Desktop.
Save KristobalJunta/a190c6c4055612e1b163f53e291aafb8 to your computer and use it in GitHub Desktop.
A simple script to batch convert images to telegram sticker accepted format, dimensions & file size
#!/bin/bash
# requiements:
# - imagemagick
# - pngquant
# - npm and renamer package
# convert jpg files to png
for f in *.jpg; do
echo "Processing $f"
n="${f%.*}"
convert $f "$n.png"
done
# resize png files to 512x512
for f in *.png; do
echo "Processing $f"
convert $f -resize 512x512 "$f"
done
# lossy optimise pngs
for f in *.png; do
echo "Processing $f"
pngquant $f
done
# clear the directory and move out optimized files only
rm *.jpg
if [ ! -d "done" ]; then
mkdir "done"
fi
mv *-fs8* "done/"
renamer -f "-fs8" "done/*.png"
rm *.png
echo "All done!"
@Elbobburrito
Copy link

Elbobburrito commented Apr 30, 2018

So, I tried using this script, and it created a directory "done" and started changing all the files.....

Then immediately deleted every file in the directory I ran it in, and "done" is empty. Did I do something wrong?

@htdhnsdjetsr
Copy link

yo this script sucks ass, it deleted all of my memes

@KristobalJunta
Copy link
Author

@Elbobburrito @ottersarecool sorry to hear that.
The intended behavior here is:

  1. input is a directory with PNG and/or JPG files.
  2. the script converts all JPGs to PNGs, then resizes and optimizes (lossy) the PNGs.
  3. processed PNGs are put to "done" folder and source files are removed

I intended it to work such way - it suited my workflow. Maybe there should be a warning before starting the script or smth like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment