Last active
March 24, 2017 19:01
-
-
Save robotsandcake/15a0968f055d17227879e49d0fb9d9b6 to your computer and use it in GitHub Desktop.
This bash script will take a folder full of PNG and JPEG files, convert them into various sizes and place them into different folders based on size.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # This work is licensed under the WTFPL. | |
| # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
| # Version 2, December 2004 | |
| # Copyright (C) 2017 Stuart Turner <[email protected]> | |
| # Everyone is permitted to copy and distribute verbatim or modified | |
| # copies of this license document, and changing it is allowed as long | |
| # as the name is changed. | |
| # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
| # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
| # 0. You just DO WHAT THE FUCK YOU WANT TO. | |
| # Download and Make Ready # | |
| # This script uses the convert command which requires the ImageMagick package. | |
| # On MacOS you can install it with homebrew using "brew install imagemagick" ymmv. | |
| # Usage # | |
| # Place this script in a folder full of JPEG, PNG or GIFS. | |
| # In the Terminal, navigate to the folder with the images and this script in. | |
| # The command "cd ~/images/tobeconverted" for example would work. | |
| # Next make the script executable with "chmod +x resize-with-imagemagick.sh" | |
| # Running # | |
| # Run the script with this command whilst in the folder full of images: | |
| # "./resize-with-imagemagick.sh" | |
| # Expected Outcome # | |
| # It creates folders to receive the copies it will create. | |
| # The folders names will be the widths of the images within. | |
| # Copies of the images are created and placed in the correct folder. | |
| # If a folder doesn't exist, the script will create it. | |
| # Up-to-date # | |
| # The most up-to-date version of the script will be at this URL: | |
| # https://gist.github.com/escapologyBB/15a0968f055d17227879e49d0fb9d9b6 | |
| # Alternative # | |
| # If you would rather use sips which comes installed on MacOS, I have another script uses that: | |
| # https://gist.github.com/escapologyBB/15a0968f055d17227879e49d0fb9d9b6 | |
| # Change the values between the quotes if you need to have different widths. | |
| echo "Checking if the right directories exist, I will create them if they don't" | |
| folder_0="320" | |
| folder_1="480" | |
| folder_2="640" | |
| folder_3="980" | |
| folder_4="1280" | |
| folder_5="1600" | |
| folder_6="1920" | |
| # Check if directory exists, if not create it | |
| if [ -d $folder_0 ] | |
| then | |
| echo "Directory $folder_0 exists" | |
| else | |
| mkdir $folder_0 | |
| fi | |
| # Check if directory exists, if not create it | |
| if [ -d $folder_1 ] | |
| then | |
| echo "Directory $folder_1 exists" | |
| else | |
| mkdir $folder_1 | |
| fi | |
| # Check if directory exists, if not create it | |
| if [ -d $folder_2 ] | |
| then | |
| echo "Directory $folder_2 exists" | |
| else | |
| mkdir $folder_2 | |
| fi | |
| # Check if directory exists, if not create it | |
| if [ -d $folder_3 ] | |
| then | |
| echo "Directory $folder_3 exists" | |
| else | |
| mkdir $folder_3 | |
| fi | |
| # Check if directory exists, if not create it | |
| if [ -d $folder_4 ] | |
| then | |
| echo "Directory $folder_4 exists" | |
| else | |
| mkdir $folder_4 | |
| fi | |
| # Check if directory exists, if not create it | |
| if [ -d $folder_5 ] | |
| then | |
| echo "Directory $folder_5 exists" | |
| else | |
| mkdir $folder_5 | |
| fi | |
| # Check if directory exists, if not create it | |
| if [ -d $folder_6 ] | |
| then | |
| echo "Directory $folder_6 exists" | |
| else | |
| mkdir $folder_6 | |
| fi | |
| # Let's do the actual conversion now, any JPEG or PNG in the | |
| # current directory will be converted to the appropriate size | |
| # and moved to the appropriate folder. | |
| # You can change the values" if you would like a different width. | |
| width_0="320" | |
| width_1="480" | |
| width_2="640" | |
| width_3="980" | |
| width_4="1280" | |
| width_5="1600" | |
| width_6="1920" | |
| echo "Okay, who's up for some image conversion?! I know I am!" | |
| echo "Converting images with a maximum width of $width_0" | |
| # Maximum width of 320 pixels | |
| for i in $( ls *.jpg ); do convert -resize $width_0 $i $folder_0/$i; done | |
| for i in $( ls *.png ); do convert -resize $width_0 $i $folder_0/$i; done | |
| echo "Converting images with a maximum width of $width_1" | |
| # Maximum width of 480 pixels | |
| for i in $( ls *.jpg ); do convert -resize $width_1 $i $folder_1/$i; done | |
| for i in $( ls *.png ); do convert -resize $width_1 $i $folder_1/$i; done | |
| echo "Converting images with a maximum width of $width_2" | |
| # Maximum width of 640 pixels | |
| for i in $( ls *.jpg ); do convert -resize $width_2 $i $folder_2/$i; done | |
| for i in $( ls *.png ); do convert -resize $width_2 $i $folder_2/$i; done | |
| echo "Converting images with a maximum width of $width_3" | |
| # Maximum width of 980 pixels | |
| for i in $( ls *.jpg ); do convert -resize $width_3 $i $folder_3/$i; done | |
| for i in $( ls *.png ); do convert -resize $width_3 $i $folder_3/$i; done | |
| echo "Converting images with a maximum width of $width_4" | |
| # Maximum width of 1280 pixels | |
| for i in $( ls *.jpg ); do convert -resize $width_4 $i $folder_4/$i; done | |
| for i in $( ls *.png ); do convert -resize $width_4 $i $folder_4/$i; done | |
| echo "Converting images with a maximum width of $width_5" | |
| # Maximum width of 1600 pixels | |
| for i in $( ls *.jpg ); do convert -resize $width_5 $i $folder_5/$i; done | |
| for i in $( ls *.png ); do convert -resize $width_5 $i $folder_5/$i; done | |
| echo "Converting images with a maximum width of $width_6" | |
| # maximum width of 1920 pixels | |
| for i in $( ls *.jpg ); do convert -resize $width_6 $i $folder_6/$i; done | |
| for i in $( ls *.png ); do convert -resize $width_6 $i $folder_6/$i; done | |
| echo "All directories created and image conversions placed in the right place (hopefully!)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment