Skip to content

Instantly share code, notes, and snippets.

@fuzzywalrus
Created August 20, 2017 19:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuzzywalrus/2cd69866afb25954e41d53b3d319dafd to your computer and use it in GitHub Desktop.
Save fuzzywalrus/2cd69866afb25954e41d53b3d319dafd to your computer and use it in GitHub Desktop.
Simple image conversion script design create from a double density asset both @2x and @1x versions of said image in JPEG, JPEG2000, WebP for HTML5 picture elements.
# simple image conversion script design create from a double density asset both @2x and @1x versions of said image in JPEG, JPEG2000, WebP for HTML5 picture elements.
# Requires Zenity, ImageMagick with JPEG2000, MozJPEG, and Webp
# brew install imagemagick --with-openjpeg
# brew install webp
# brew install mozjpeg
# brew install zenity
file=$(zenity --file-selection --title="Select a File")
datafill=`zenity --forms \
--title="Example" \
--add-entry="Select WebP Quality" \
--add-entry="Select JPEG2000 Quality" \
--add-entry="Select JPEG Quality" \
--separator="|"`
echo $datafill
arrIN=(${datafill//|/ })
JPEG2000quality=${arrIN[0]}
WebPquality=${arrIN[1]}
JPEGquality=${arrIN[2]}
echo $file
# vars for use later
webpExt=".webp"
jp2Ext=".jp2"
jpgExt=".jpg"
ppmExt=".ppm"
pngExt=".png"
twoX="@2x"
oneX="@1x"
# make half size PNG path name, create a 1x PNG.
halfsizeFile="${file%.*}"$oneX$pngExt
echo $halfsizeFile
convert $file -resize 50% $halfsizeFile
# webp
convert $file -quality $WebPquality "${file%.*}"$twoX$webpExt
convert $halfsizeFile -quality $WebPquality "${file%.png}"$oneX$webpExt
# jpeg2000
convert $file -quality $JPEG2000quality "${file%.*}"$twoX$jp2Ext
convert $halfsizeFile -quality $JPEG2000quality "${file%.*}"$oneX$jp2Ext
# ppm temp for MozJPEG
ppm2x="${file%.*}"$twoX$ppmExt
convert $file $ppm2x
ppm1x="${file%.*}"$oneX$ppmExt
convert $halfsizeFile $ppm1x
# jpeg (MozJPEG)
cjpeg -quality $JPEGquality $ppm2x > ${ppm2x%.ppm}$twoX$jpgExt
cjpeg -quality $JPEGquality $ppm1x > ${ppm1x%.ppm}$oneX$jpgExt
# remove temp ppm
rm $ppm2x
rm $ppm1x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment