Command Line Interface Photographs Conversion
Tags: linux, learning, challenges, beginners
Published: 2018-06-28 16:17:05

---
title: Command Line Interface Photographs Conversion
published: true
description: 
tags: #Linux #Learning #Challenges #Beginner
---

<img src="http://egmgem.com/blog/wp-content/uploads/2018/06/Swiss-Or-Corkscrew-768x512.jpg" alt="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

<pre>gm help</pre>

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

<pre>gm convert -size 85x85 "infile.jpg" -resize 85x85 +profile "*" "outfile.jpg"</pre>

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:

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

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

<pre>chmod 777 gmthumbs</pre>

to make it executable.

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

<img src="http://egmgem.com/blog/wp-content/uploads/2018/06/Listing-1.png" alt="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 (<em><strong>0filename.jpg</strong></em>) as shown in the following image:

<img src="http://egmgem.com/blog/wp-content/uploads/2018/06/Listing-2.png" alt="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.

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

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

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

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 <em><strong>output</strong></em> had been created.

<img src="http://egmgem.com/blog/wp-content/uploads/2018/06/Listing-3.png" alt="Listing showing original files intact, and new folder output in place.">

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

<img src="http://egmgem.com/blog/wp-content/uploads/2018/06/Listing-4.png" alt="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 <em><strong>feh</strong></em>, 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/