I am having issues with the part of the following script where it takes the inputted image and checks its dimensions. If it's not a certain dimension then it resizes the image.

My issue is what would I put inside the if-block to check that using ImageMagick?

My current code:

#Change profile picture f(x)
#Ex. changeProfilePicture username /path/to/image.png
function changeProfilePicture() {
    filePath="$(readlink -f "$2")"
    fileName="${filePath##*/}" #baseName + fileExtension

    echo "Checking if imagemagick is installed..."
    if ! command brew ls --versions imagemagick >/dev/null 2>&1; then
        echo "Installing imagemagick..."
        brew install imagemagick -y
        echo "imagemagick has been installed."
        echo "imagemagick has already been installed."

    # check the file extension. If it's not png, convert to png.
    echo "Checking file-extension..."
    if ! [[ $fileExtension = "png" ]]; then
            echo "Converting to ''.png'..."
            convert $fileName "${baseName}".png
            echo "File conversion was successful."
        echo "File-extension is already '.png'"

    # check the dimensions, if its not 96x96, resize it to 96x96.
    #I don't know what to put inside the following if-block:
    if ! [[  ]]; then
        echo "Resizing image to '96x96'..."
        convert $fileName -resize 96x96 "${fileName}"
        echo "Image resizing was successful."
        echo "Image already has the dimensions of '96x96'."

    echo "changing profile picture to " "$filePath"
    sudo cp "$filePath" /var/lib/AccountsService/icons/
    cd /var/lib/AccountsService/icons/
    sudo mv $fileName "${userName}"
    cd ~/Desktop

2 Answers 2

identify -format '%w %h' your_file

Will output the width, a space, then the height.

If you wanted to store each separately, you could do:

width =`identify -format '%w' your_file`
height=`identify -format '%h' your_file`
  • backticks are deprecated. use width=$(identify -format '%w') instead.
    – cas
    Jun 8, 2016 at 3:14
  1. First, unlikely you have 96x96 existing picture, so most of the case you need to convert. You don't have to identify and compare the dimensions.
  2. Don't trust extension of filename, .png doesn't means it's a PNG image.
  3. Test command and then install is an unnecessary checking and not portable (apt-get, dnf, ... etc). And the fact is it should output "command not found" if this case happen. Moreover this checking might slowing down your function.

So, why not simply do:

#Ex. changeProfilePicture username /path/to/image.png
function changeProfilePicture () {
    sudo mkdir -p -- '/var/lib/AccountsService/icons/'"$1"
    sudo convert "$2" -set filename:f '/var/lib/AccountsService/icons/'"$1/%t" -resize 96x96 '%[filename:f].png'


  1. If you want to ignore aspect ratio to ensure the output always 96x96, then change -resize 96x96 to -resize 96x96\!, read this.
  2. The reason why .png doesn't put as part of filename:f above is because:

Warning, do not include the file suffix in the filename setting! IM will not see it, and save the image using the original file format, rather than the one that was included in filename setting. That is the filename will have the suffix you specify, but the image format may different!

  • Yeah that's more efficient, however, I get this error every time I run it. Take a look at this image: i.gyazo.com/b11c21ce82a0df7dc69214e84050b23b.png Jun 7, 2016 at 17:26
  • @NicholasAdamou oops, edited.
    – 林果皞
    Jun 7, 2016 at 17:35
  • 1. is not so unlikely if (some of) the image files have already been resized. checking to avoid resizing again is worthwhile. 2. is correct. and 3. is very good advice - users don't expect or want general non-installer scripts to install stuff...at most, it should print a warning message that required tools are missing, and maybe even a hint about how to install what it needs.
    – cas
    Jun 8, 2016 at 3:19
  • @cas 1. According to imagemagick.org/Usage/resize , Resize will do nothing at all if the resized image is the same size except "-filter" setting. I believe imagemagik smart enough to handle same size.
    – 林果皞
    Jun 8, 2016 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.