Skip to content

Instantly share code, notes, and snippets.

@enijar
Last active June 8, 2016 13:05
Show Gist options
  • Save enijar/98ff70fba6ba9222f90e9563a4d6cb66 to your computer and use it in GitHub Desktop.
Save enijar/98ff70fba6ba9222f90e9563a4d6cb66 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Resize all png images in a given directory, using pngquant.
# Usage:
# bash crushPng.sh "path/to/files/"
# Check that a path has been provided from the cli.
if [ -z "$1" ]; then
echo "A path must be provided."
exit 1
fi
# Check that pngquant is installed.
if [ -z $(type "pngquant") ]; then
echo "pngquant command not found. Install before running this script."
exit 1;
fi
# Replace trailing slash
path=$(echo $1 | sed -e 's/\/$//')
for file in $(find "$path" | grep .png | sed 's/^[0-9]*://g'); do
pngquant 256 $file --ext -new.png
newFile=$(echo $file | sed 's/.png/-new.png/g')
# If a new file was created by pngquant then rename that new file name
# to the original file name, otherwise notify that the file did not
# need to be resized.
if [ -f "$newFile" ]; then
mv $newFile $file
echo "$file resized."
else
echo "$file did not need to be resized."
fi
done
# This command can take a while when running over a directory with
# lots of bug PNGs in it. Notify the user when this process is complete.
say "Crush PNG command complete."
#!/usr/bin/env bash
# Lists the ppi for all images. If a second argument is supplied then
# only images with that ppi will be shown.
# Usage:
# bash ppiLocator.sh "path/to/files/" "*.png" 72 eq
# Check that a path has been provided from the cli.
if [ -z "$1" ]; then
echo "No path provided."
exit 1
fi
# Check that a file type has been provided from the cli.
if [ -z "$2" ]; then
echo "No file type provided."
exit 1
fi
path=$(echo $1 | sed -e 's/\/$//')
files=$(find $path -type f -name $2)
for file in $files; do
ppi=$(identify -verbose -units PixelsPerInch -format '%x' $file 2>/dev/null)
ppi=$(printf "%.0f" $ppi)
# If no ppi is passed from the cli then list the ppi for each file.
if [ -z "$3" ]; then
echo "$file is $ppi ppi"
else
# if operator not specified
if [ -z "$4" ]; then
if [ "$ppi" -eq "$3" ]; then
echo "$file is $ppi ppi"
fi
else
# if operator specified
if [ "$4" = "lt" -a "$ppi" -lt "$3" ]; then
echo "$file is $ppi ppi"
fi
if [ "$4" = "gt" -a "$ppi" -gt "$3" ]; then
echo "$file is $ppi ppi"
fi
if [ "$4" = "eq" -a "$ppi" -eq "$3" ]; then
echo "$file is $ppi ppi"
fi
fi
fi
done
#!/usr/bin/env bash
# Renames files in a folder from 1 to the total number of files. For
# example if there are 100 files with different names, this script
# will renames them from 1 to 100 with the given extension, say png.
# Usage: bash rename.sh "path/to/file/sequence" "png" 1
# Check if the first argument from the command line is present.
if [ -z "$1" ]; then
echo "No directory provided."
exit 1
fi
# Check if the second argument from the command line is present.
if [ -z "$2" ]; then
echo "No file extension provided."
exit 1
fi
# Replace trailing slash
path=$(echo $1 | sed -e 's/\/$//')
# If the directory doesn't exist then exit with a warning.
if [ ! -d $path ]; then
echo "No such directory exists."
exit 1
fi
# Sort files in alphanumerical order.
files=$(ls $path | sort -k1,1n)
i=0
# If there is a third parameter then assign i to it's value.
if [ ! -z "$3" ]; then
i=$3
fi
# Increment the counter and rename the file.
for file in $files; do
((i++))
mv "$path/$file" "$path/$i.$2"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment