# Ali Murteza Yesil - Tech

# Black Lives Matter

# Resizing 2500 photos

I was asked to write a program to resize thousands of images which were gonna be uploaded to a website. Images had to be the same height for them to look organized in gallery. I remembered how I used imagemagick to write desktop-clock, immitate tty-clock on wallpaper. That experience helped me to design the program in my mind as we continue to speak on the phone.

    /‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\
    | for each image            |
    |   do resize to height=800 |
    \___________________________/
      O
        o  \_\_    _/_/
          .    \__/
               (oo)\_______
               (__)\       )\/\
                   ||----w |
                   ||     ||

# This is a learning opportunity

Since I don't have much of an experience on shell scripting, I was Ducking everything. I learned to do few things during this task.

1. For loops in shell script
2. Getting list of files
3. Correct syntax for resizing with ImageMagick

After reading tutorials and looking at examples everything felt as simple as lego. I just needed to put them together.

Here is `image_shrinker.v0.1_alpha.sh` :

    #!/bin/sh
    mkdir ../output
    for image in ./*
    do
        convert $image -scale 1000000x800 ../output/$image
    done

This version has problems which I can't call "feature" 😁️ :

1. Resolution is hard coded. Btw, only way to define resolution is in WIDTHxHEIGHT format AFAIK.
2. Script must be in same directory as images
3. PWD must point to the directory where script is

## Adjustable resolution

    #!/bin/sh
    mkdir ../output
    WIDTH=$1 ; HEIGHT=$2

    for image in ./*
    do
        convert $image -scale $WIDTHx$HEIGHT ../output/$image
    done

This fixes the hard coded resolution problem but new problem is that the user has to know that WIDTH and HEIGHT must be given. I fix that by showing correct syntax to the user and then exiting :

    #!/bin/sh

    if [ $# -lt 2 ]
    then
        echo "USAGE:"
        echo -e "\t\$IMAGE_DIRECTORY/image_shrinker.sh WIDTH HEIGHT"
        exit 1
    fi

    WIDTH=$1 ; HEIGHT=$2
    mkdir ../output

    for image in *
    do
        convert $image -scale $Resolution ../output/$image
    done

Here is some other attempt to make code a bit more flexible in terms of where images can be and where output can go. Actually no but that is what I was going for 😜️

    #!/bin/sh

    DIR=`pwd`

    for image in ./*.*
    do
    #   Below code will scale images to 800px height without breaking aspect ratio
        convert $DIR/$image -scale 10000x800 -write $DIR/../output/$image
    done

It is hard to ask for help (especially after torturing with my code) but if you would like to give me feedback and advice, reply to toot of this post.

## Comments

Toot on this thread to comment. This blog is a static site. Comments won't appear here.

# Other articles

1. # Digital Cleansing - Music

2. # Swap on SSD done right

3. # Decentralized Internet is More Reliable

4. # Digital Cleansing - Mastodon

5. # Digital Cleansing - Jitsi

6. # Digital Cleansing - NextCloud

7. # Digital Cleansing - Identifying services we use

8. # Digital Cleansing For Better Privacy

9. # Privacy For The Whole Family

Page 1 / 1

## blogroll

- Kev Quirk
- Mike Stone
- Yarmo Mackenbach

## social

- atom feed
- rss feed
- Fostodon

Ali Murteza Yesil I'm a student, privacy advocate and SysAdmin wannabe. I was born in Turkey but I live abroad. Blogging Open Source Software Privacy