Skip to content

Instantly share code, notes, and snippets.

@rayantony
Last active April 8, 2017 14:50
Show Gist options
  • Save rayantony/6657b75f15759e730efb to your computer and use it in GitHub Desktop.
Save rayantony/6657b75f15759e730efb to your computer and use it in GitHub Desktop.
bash-imagemagick-useful-tricks-not-hacks

ImageMagick in Bash/Console : Not all 'Tricks' necessitate using the word 'Hack' (so lets cool it on that shit ok?)

For many of these, I have created a sort of framework(s) around the standard IM commands to ease in modularization. So quickly do our Im strings vanish from memory or clumsily integrate into a million other things, I'm taking some steps to make them easy to use and share if only in a linux/ubuntu environment.

As well many of these are coded from the start to be able to take multiple file types where applicable, as well 'x' number of them via spaced command line params or selecting large groups of files with the mouse to convert which is often the objective but virtually never coded for in Linux/Bash and virtually unpublished and unpracticed in Dos/Windows. The compression isnt much mystery but this modularization and scaling of input/outputs is a common feature that allows you to pipe it in your regular flow or not. Dos/Windows/ Linux as long as you can pipe it drag and drop and select groups things are just so goddamn easy, and satisfying ya know? But even better without Windows at all ;)

If you use Nautilus/Unity/Gnome then drop completed scripts (not the solo lines only) in $HOME/.gnome2/nautilus scripts/... without the '.sh' extension and permission it root but executable. Good to go, you should have context menu action.

see also: https://github.com/rayantony/RappleShots

#####Note: This is a demo with no warranty or anything it simply does something based on some stuff - dont overthink it.

Have Fun!

by ray anthony

rayantony.github.io twitter.com/rayanthonyrcc

copyright 2015 ray anthony© all applicable rights reserved all liabilities waived
#!/bin/bash
# sample imagemagick lines to reformat image of type jpeg without the crappy gaussian blur bs
# compress imagemagick pngs or jpeg png max weight 450kb, jpeg 300
# nautilus integration: $USER/.gnome2/nautilus-scripts/compress/[this file] (root owned, executable, no '.sh')
# by ray anthony
# note: does use "-strip"
while [ "$#" -gt 0 ]; do
picture="$1"
filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"
case $extension in
png) convert "$picture" -strip -sampling-factor "4:2:0" -quality "97%" -define "png:dct-method=float" -define "png:extent=450kb" "$picture"
;;
jpg) convert "$picture" -strip -sampling-factor "4:2:0" -define "jpeg:dct-method=float" -quality "92%" -define "jpeg:extent=300kb" "$picture"
;;
esac
shift
done
#!/bin/bash
# sample imagemagick lines to reformat image of type jpeg without the crappy gaussian blur bs
# compress imagemagick jpeg # compress imagemagick pngs or jpeg png max weight 650kb, jpeg 400
# nautilus integration: $USER/.gnome2/nautilus-scripts/compress/[this file] (root owned, executable, no '.sh')
# by ray anthony
# note: does use "-strip"
while [ "$#" -gt 0 ]; do
picture="$1"
filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"
case $extension in
png) convert "$picture" -strip -sampling-factor "4:2:0" -quality "97%" -define "png:dct-method=float" -define "png:extent=650kb" "$picture"
;;
jpg) convert "$picture" -strip -sampling-factor "4:2:0" -define "jpeg:dct-method=float" -quality "92%" -define "jpeg:extent=400kb" "$picture"
;;
esac
shift
done

livewire png

###put this line in .bashrc and type bash, or paste into the console:

alias livewire='curl -L -O https://gist.githubusercontent.com/rayantony/6657b75f15759e730efb/raw/d976e015840b7791cb3a5336bc985e687aa8a6a8/pngtojpg.sh;bash pngtojpg.sh'

then simply type livewire test.png where "test.png" is any png or larger uncompressed image in the directory you are actively in. note: it will create a fitest.jpg in that directory so be careful if you happen to have one or copy the source file to a temp directory and work from their as a sandbox/jail.

livewire test.png

this is a demo with no warranty or anything it simply does something based on some stuff - dont overthink it.

-ray anthony

@ rayantony.github.io

magic.md

###by ray anthony ##imagemagic lines DRAFT

fastchrubkey 2014-2015

convert rasberry.jpg -background transparent -fuzz "20%" -transparent white rasberry.png
convert rasberry.jpg -resize 128\!x128\! favicon.ico
 convert onlyhdlogo.png -background black -repage 512\!x512\! -resize 512\!x512\! -shear X12 -fuzz "25%" -transparent black -background transparent onlyhdlogotrans.png && convert onlyhdlogotrans.png -shear 12x12 -sketch 20 onlyhdlogotrans2.png

##DRAFT function logomaker

 function logomaker(){ cmdimg="$@";printf "\nenter image 1\n";read img;printf "\nimage 2\n";read img2;printf "\nimage 3 \n";read img3;printf "\ndoin it...\n";convert $img -background white -repage 512\!x512\! -resize 512\!x512\! -shear 12x12 -fuzz "15%" -transparent white -background transparent $img2 && convert $img2 -shear 12x12 -sketch 20 $img3 && convert $img3 favicon.ico; }

png jpg compression imagemagick functions

png and jpg compression and conversion in ImageMagick in the console

#png to png compress

convert in.png -strip -sampling-factor "4:2:0" -quality "99%" new3.png

#alt png to png compress

convert in.png -strip -interlace Plane -filter Lanczos -sampling-factor "4:2:0" -quality "99%" out.png

#png to jpg compress

convert in.png -strip -sampling-factor "4:2:0"  -quality "99%" -define "jpeg:dct-method=float"out.jpg

#170x170 png to jpg (force size)

convert in.png -strip -sampling-factor "4:2:0" -quality "97%" -define "jpeg:dct-method=float" -define "jpeg:extent=14kb"  out.jpg

#alt 170x170 png to jpg

convert in.png -strip-sampling-factor "4:2:0" -quality "97%" -define "jpeg:dct-method=float" out.jpg
#!/bin/bash
# by rayanthony.io
# test app convert single file standard input assumed png high quality low size jpg out spin up from online raw url
# if useful after test trials immed add support for bulk submit fancy rename , type convert options verify input and PWD and attend to hack breakfasts and call your crowdfund PR specialist or VC rep in the morning
# output created by app /tmp/tmpimg
# aug 17 2015
set -x
infile=$1
mkdir -p /tmp/tmpimg
cp $infile /tmp/tmpimg/.
outf=`echo "$1" | sed 's/\.\w*$/'.jpg'/'`
cp $outf /tmp/tmpimg/.
convert "${infile}" -strip -sampling-factor "4:2:0" -quality -define "jpeg:dct-method=float" -define "jpeg:extent=14kb" "97%" "${outf}"
xdg-open "${outf}"
ls -lash
sleep 3
cd "$OLDPWD"
echo "Done! "
exit 0
#!/bin/bash
# by rayanthony.io
# test app convert single file standard input assumed png high quality low size jpg out spin up from online raw url
# if useful after test trials immed add support for bulk submit fancy rename , type convert options verify input and PWD and attend to hack breakfasts and call your crowdfund PR specialist or VC rep in the morning
# output created by app /tmp/tmpimg
# aug 17 2015 test run v1
#set -x
infile=$1
#mkdir -p /tmp/tmpimg
#cp $infile /tmp/tmpimg/.
outf=`echo "$1" | sed 's/\.\w*$/'.jpg'/'`
#cp $outf /tmp/tmpimg/.
touch $outf
convert "${infile}" -strip -sampling-factor "4:2:0" -quality "97%" -define "jpeg:dct-method=float" -define "jpeg:extent=14kb" "${outf}"
xdg-open "${outf}"
ls -lash
sleep 5
#cd "$OLDPWD"
echo "Done! "
rm -y pngtojpg.sh
exit 0
#!/bin/bash
# by rayanthony.io
# test app convert single file standard input assumed png high quality low size jpg out spin up from online raw url
# if useful after test trials immed add support for bulk submit fancy rename , type convert options verify input and PWD and attend to hack breakfasts and call your crowdfund PR specialist or VC rep in the morning
# output created by app /tmp/tmpimg
# aug 17 2015 test run v1
#set -x
infile=$1
#mkdir -p /tmp/tmpimg
#cp $infile /tmp/tmpimg/.
outf=`echo "$1" | sed 's/\.\w*$/'.png'/'`
#cp $outf /tmp/tmpimg/.
touch $outf
convert "${infile}" -strip -sampling-factor "4:2:0" -quality "97%" "${outf}"
xdg-open "${outf}"
ls -lash
sleep 5
#cd "$OLDPWD"
echo "Done! "
rm -y pngtopng.sh
exit 0
#!/bin/bash
# sample imagemagick lines to reformat image of type jpeg without the crappy gaussian blur bs
# compress imagemagick jpeg compacts jpegs. can be used as a starting point then for further trimmming a more aggressive method after
# nautilus integration: $USER/.gnome2/nautilus-scripts/compress/[this file] (root owned, executable, no '.sh')
# by ray anthony
# note: does use strip
while [ "$#" -gt 0 ]; do
infile="$1"
filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"
case $extension in
jpg) convert -quiet -regard-warnings "$infile" -strip +repage "$tmpA1"
fullsize=`convert $tmpA1 ${ftype}:- | convert - -ping -format "%b" info:`
size2=`convert xc: -format "%[fx:$size*1000]" info:`
i=1
diffsize=0
iterate=1
while [ $iterate -eq 1 ]; do
size2=`convert xc: -format "%[fx:$size2-($diffsize*1000)]" info:`
pratio=`convert xc: -format "%[fx:100*sqrt($size2/$fullsize)]" info:`
convert $tmpA1 -resize ${pratio}% ${ftype}:$outfile
newsize=`convert $outfile -ping -format "%b" info:`
newsize=`convert xc: -format "%[fx:$newsize/1000]" info:`
echo "i=$i; newsize=${newsize}kB"
diffsize=`convert xc: -format "%[fx:($newsize-$size)]" info:`
iterate=`convert xc: -format "%[fx:$diffsize>($toler*$size/100)?1:0]" info:`
i=$(($i+1))
done
;;
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment