DEV Community

Eric Gitonga
Eric Gitonga

Posted on • Updated on

Command Line Interface Photographs Conversion

Do I choose between the Graphical User Interface Swiss Army Knife, or go for the Command Line Interface Corkscrew?

There is something magical about working on the command line. Sure, many find it super esoteric and arcane, but I love it. The world of Windows had really spoiled me big time, getting me used to the GUI for everything. Sure, there’s the command prompt, and for those that want more power, the aptly named PowerShell, but that still did not move me. After switching to Linux, I am reacquainting myself with the command line interface (CLI), and I find my love for it growing more and more.

In my regular photography duties, I realized I needed to generate thumbnails of images for a certain project. I am still exploring the ins and outs of digiKam, RawTherapee and GIMP, but something in me kept telling me that for this particular task, a CLI tool was better suited. So off I went searching. I had heard of and used ImageMagick before, but initial searching led me to GraphicsMagick, which I decided to try out.

After installing it, I ran GraphicsMagick with the command

gm help

to see what options it had. Among the options, the one that was of interest to me was the convert option. On searching for help about it from the man page, I found an example that fit my needs. The full command I needed was

gm convert -size 85x85 "infile.jpg" -resize 85x85 +profile "*" "outfile.jpg"

On testing it with an image, it worked just fine, giving me the output I expected.

Next up was trying to figure out how to apply this command to an entire directory of images. I went googling around for instructions on how to do that. After a bit of searching, I came across a listing that I modified to suit my specific needs as shown below:

for i in *.jpg;
do
filename=${i%.*}
gm convert -size 85x85 "$filename.jpg" -resize 85x85 +profile "*" "$#filename.jpg"
done

I saved this to a file gmthumbs (since what I wanted to do was to convert full size images to thumbnails) and used

chmod 777 gmthumbs

to make it executable.

I ran the command ./gmthumbs in a test directory I had set up with a number of images.

Listing showing the files I used to test the conversion script

I didn’t get the results I expected, since only the first file was converted and given a new name (0filename.jpg) as shown in the following image:

Listing of files showing just the first image converted

I played around with the output variable name, having just the pound sign in the output file name, adding the dollar sign to it, then, eventually, removing the pound sign but keeping the dollar sign. That last option, listed below, gave me the results I needed.

for i in *.jpg;
do
filename=${i%.*}
gm convert -size 85x85 "$filename.jpg" -resize 85x85 +profile "*" "$filename.jpg"
done

My only problem now was the original files getting overwritten by the converted files. What I needed it to do was write the files to a new directory. I added the command to make an output directory and modified the output filename variable to have it moved to that new output folder as shown in the listing below

mkdir output;
for i in *.jpg;
do
filename=${i%.*}
gm convert -size 85x85 "$filename.jpg" -resize 85x85 +profile "*" "output/$filename.jpg"
done

On running the command and then asking for a listing of files, I found that the original files were still intact, and a new directory output had been created.

Listing showing original files intact, and new folder output in place.

Going into that directory, I found the converted files in place.

Listing showing output folder with properly converted files.

Success! Now I can create thumbnails of images in any folder quickly and easily using this nifty script.

Before I close, I want to point out feh, which is a command line utility that I used to view the images. Running it as is without any options loads the images in a folder to a window that you can scroll through. Very handy for when I want to just view images I am working in from the command line without having to load a bulky GUI!

Now off to explore what other magical CLI tools I can incorporate into my workflow.

Resources:

ImageMagick: https://www.imagemagick.org/script/index.php

GraphicsMagick: http://www.graphicsmagick.org/

Feh: https://feh.finalrewind.org/

Top comments (4)

Collapse
 
millebi profile image
Bill Miller

Don't forget old-school:
ls *.jpg | xargs -l1 -i{} imagemagik ... "{}" ... "./output/{}"

Collapse
 
egimba profile image
Eric Gitonga

Thanks for that! Had no idea about it, so it is something new I have learnt to add to my arsenal.

Collapse
 
shaiay profile image
shaiay

I'm not sure why you need the $filename variable.
Why not use $i as the filename?

for i in *.jpg; do
    gm convert -size 85x85 "$i" -resize 85x85 +profile "*" "output/$i"
done
Collapse
 
egimba profile image
Eric Gitonga

Thank you for pointing that out. I don't need it really, I just used it as is from the example I found online. Using $i definitely saves on the typing!