Saturday 16 October 2010

Transforming pictures in Linux

I use GNU/Linux for most of my computing needs. This includes manipulating photos I upload to this blog. I could open each image one by one in GIMP, resize them, and then save them with the new file name - but that would be time consuming and monotonous. With the use of the Imagemagick command line tool set, and the Linux command line, I've found a way to script up the renaming and resizing of the images.

Here are the steps:
1. Transfer the images from your camera to the computer. Linux is very USB aware, and when I plug in my camera, it offers to either start an instance of a photo viewer, a visual file browser, or do nothing. I choose to open the visual file browser, so I can drag and drop the image files onto a directory / folder on my desktop. Here are those files:Images before transformation2. Next is to open up a command line, and run some commands. Some of the commands are important, some can be ignored. I will start by going into the directory and checking if the files are there:
evilric@hawk:~/Desktop/fake_air_jordan_VIs$ ls
DSC05049.JPG DSC05056.JPG DSC05063.JPG DSC05070.JPG DSC05077.JPG
DSC05050.JPG DSC05057.JPG DSC05064.JPG DSC05071.JPG DSC05078.JPG
DSC05051.JPG DSC05058.JPG DSC05065.JPG DSC05072.JPG DSC05079.JPG
DSC05052.JPG DSC05059.JPG DSC05066.JPG DSC05073.JPG
DSC05053.JPG DSC05060.JPG DSC05067.JPG DSC05074.JPG
DSC05054.JPG DSC05061.JPG DSC05068.JPG DSC05075.JPG
DSC05055.JPG DSC05062.JPG DSC05069.JPG DSC05076.JPG
The command is "ls" to list the contents of the directory. It is optional. Keep in mind these are all the original sized images, directly from the camera, taken in 7 megapixels - 3072 x 2304. With limited space on servers, and limited bandwidth, the idea is to reduce them all to 1024 x 768 as well as renaming them to something more readable.
3. Check to see the space used by this directory:
evilric@hawk:~/Desktop/fake_air_jordan_VIs$ du -sh
81M .
The command is "du" with the "-sh" flags. It is optional. The output says that all the files consume 81 megabytes in storage.
4. Now run mini-script to convert the images and rename them at the same time:
evilric@hawk:~/Desktop/fake_air_jordan_VIs$ myCount=0;for i in $(ls); do echo "$i -> fake_air_jordan_VIs_$myCount.jpg"; convert $i -resize 1024x768 fake_air_jordan_VIs_$myCount.jpg; myCount=$[$myCount+1];done; for i in $(seq 0 9); do mv fake_air_jordan_VIs_$i.jpg fake_air_jordan_VIs_0$i.jpg; done
DSC05049.JPG -> fake_air_jordan_VIs_0.jpg
DSC05050.JPG -> fake_air_jordan_VIs_1.jpg
DSC05051.JPG -> fake_air_jordan_VIs_2.jpg
DSC05052.JPG -> fake_air_jordan_VIs_3.jpg
DSC05053.JPG -> fake_air_jordan_VIs_4.jpg
DSC05054.JPG -> fake_air_jordan_VIs_5.jpg
DSC05055.JPG -> fake_air_jordan_VIs_6.jpg
DSC05056.JPG -> fake_air_jordan_VIs_7.jpg
DSC05057.JPG -> fake_air_jordan_VIs_8.jpg
DSC05058.JPG -> fake_air_jordan_VIs_9.jpg
DSC05059.JPG -> fake_air_jordan_VIs_10.jpg
DSC05060.JPG -> fake_air_jordan_VIs_11.jpg
DSC05061.JPG -> fake_air_jordan_VIs_12.jpg
DSC05062.JPG -> fake_air_jordan_VIs_13.jpg
DSC05063.JPG -> fake_air_jordan_VIs_14.jpg
DSC05064.JPG -> fake_air_jordan_VIs_15.jpg
DSC05065.JPG -> fake_air_jordan_VIs_16.jpg
DSC05066.JPG -> fake_air_jordan_VIs_17.jpg
DSC05067.JPG -> fake_air_jordan_VIs_18.jpg
DSC05068.JPG -> fake_air_jordan_VIs_19.jpg
DSC05069.JPG -> fake_air_jordan_VIs_20.jpg
DSC05070.JPG -> fake_air_jordan_VIs_21.jpg
DSC05071.JPG -> fake_air_jordan_VIs_22.jpg
DSC05072.JPG -> fake_air_jordan_VIs_23.jpg
DSC05073.JPG -> fake_air_jordan_VIs_24.jpg
DSC05074.JPG -> fake_air_jordan_VIs_25.jpg
DSC05075.JPG -> fake_air_jordan_VIs_26.jpg
DSC05076.JPG -> fake_air_jordan_VIs_27.jpg
DSC05077.JPG -> fake_air_jordan_VIs_28.jpg
DSC05078.JPG -> fake_air_jordan_VIs_29.jpg
DSC05079.JPG -> fake_air_jordan_VIs_30.jpg
This command is the what does the conversion. I have written it all on one line, but I am sure you could have this in a nicely formatted script. I am being lazy here. The script resets the counter at 0, and for each item it finds in the directory, it will resize it, and rename it. The second loop in the line makes the first 10 files have an additional 0 in their file name. This is only to allow listing them in order, and does not affect the files at all. This step is NOT optional.
5. You now have the new files and the old files in the one directory. You can stop at this point, and just use the new files. Additionally you can check that the files have been transformed:
evilric@hawk:~/Desktop/fake_air_jordan_VIs$ ls -alh
total 93M
drwxr-xr-x 2 evilric evilric 4.0K 2010-10-16 01:06 .
drwxr-xr-x 14 evilric evilric 4.0K 2010-10-16 00:34 ..
-rwxr-xr-x 1 evilric evilric 2.9M 2010-10-12 11:31 DSC05049.JPG
-rwxr-xr-x 1 evilric evilric 2.7M 2010-10-12 11:31 DSC05050.JPG
-rwxr-xr-x 1 evilric evilric 2.9M 2010-10-12 11:32 DSC05051.JPG
-rwxr-xr-x 1 evilric evilric 2.7M 2010-10-12 11:32 DSC05052.JPG
-rwxr-xr-x 1 evilric evilric 2.9M 2010-10-12 11:33 DSC05053.JPG
-rwxr-xr-x 1 evilric evilric 3.0M 2010-10-12 11:33 DSC05054.JPG
-rwxr-xr-x 1 evilric evilric 2.6M 2010-10-12 11:34 DSC05055.JPG
-rwxr-xr-x 1 evilric evilric 2.7M 2010-10-12 11:34 DSC05056.JPG
-rwxr-xr-x 1 evilric evilric 2.3M 2010-10-12 11:35 DSC05057.JPG
-rwxr-xr-x 1 evilric evilric 2.4M 2010-10-12 11:35 DSC05058.JPG
-rwxr-xr-x 1 evilric evilric 2.6M 2010-10-12 11:35 DSC05059.JPG
-rwxr-xr-x 1 evilric evilric 3.2M 2010-10-12 11:36 DSC05060.JPG
-rwxr-xr-x 1 evilric evilric 2.7M 2010-10-12 11:36 DSC05061.JPG
-rwxr-xr-x 1 evilric evilric 2.6M 2010-10-12 11:37 DSC05062.JPG
-rwxr-xr-x 1 evilric evilric 2.8M 2010-10-12 11:37 DSC05063.JPG
-rwxr-xr-x 1 evilric evilric 3.3M 2010-10-12 11:38 DSC05064.JPG
-rwxr-xr-x 1 evilric evilric 3.2M 2010-10-12 11:38 DSC05065.JPG
-rwxr-xr-x 1 evilric evilric 2.0M 2010-10-12 11:38 DSC05066.JPG
-rwxr-xr-x 1 evilric evilric 2.3M 2010-10-12 11:39 DSC05067.JPG
-rwxr-xr-x 1 evilric evilric 2.1M 2010-10-12 11:39 DSC05068.JPG
-rwxr-xr-x 1 evilric evilric 2.0M 2010-10-12 11:39 DSC05069.JPG
-rwxr-xr-x 1 evilric evilric 2.1M 2010-10-12 11:39 DSC05070.JPG
-rwxr-xr-x 1 evilric evilric 2.6M 2010-10-12 11:40 DSC05071.JPG
-rwxr-xr-x 1 evilric evilric 2.4M 2010-10-12 11:40 DSC05072.JPG
-rwxr-xr-x 1 evilric evilric 2.4M 2010-10-12 11:40 DSC05073.JPG
-rwxr-xr-x 1 evilric evilric 2.3M 2010-10-12 11:40 DSC05074.JPG
-rwxr-xr-x 1 evilric evilric 2.7M 2010-10-12 11:41 DSC05075.JPG
-rwxr-xr-x 1 evilric evilric 3.0M 2010-10-12 11:41 DSC05076.JPG
-rwxr-xr-x 1 evilric evilric 2.9M 2010-10-12 11:42 DSC05077.JPG
-rwxr-xr-x 1 evilric evilric 2.5M 2010-10-12 11:42 DSC05078.JPG
-rwxr-xr-x 1 evilric evilric 3.3M 2010-10-12 11:44 DSC05079.JPG
-rw-r--r-- 1 evilric evilric 426K 2010-10-16 01:04 fake_air_jordan_VIs_00.jpg
-rw-r--r-- 1 evilric evilric 390K 2010-10-16 01:04 fake_air_jordan_VIs_01.jpg
-rw-r--r-- 1 evilric evilric 435K 2010-10-16 01:04 fake_air_jordan_VIs_02.jpg
-rw-r--r-- 1 evilric evilric 437K 2010-10-16 01:04 fake_air_jordan_VIs_03.jpg
-rw-r--r-- 1 evilric evilric 462K 2010-10-16 01:04 fake_air_jordan_VIs_04.jpg
-rw-r--r-- 1 evilric evilric 499K 2010-10-16 01:04 fake_air_jordan_VIs_05.jpg
-rw-r--r-- 1 evilric evilric 378K 2010-10-16 01:04 fake_air_jordan_VIs_06.jpg
-rw-r--r-- 1 evilric evilric 374K 2010-10-16 01:04 fake_air_jordan_VIs_07.jpg
-rw-r--r-- 1 evilric evilric 328K 2010-10-16 01:04 fake_air_jordan_VIs_08.jpg
-rw-r--r-- 1 evilric evilric 353K 2010-10-16 01:05 fake_air_jordan_VIs_09.jpg
-rw-r--r-- 1 evilric evilric 402K 2010-10-16 01:05 fake_air_jordan_VIs_10.jpg
-rw-r--r-- 1 evilric evilric 452K 2010-10-16 01:05 fake_air_jordan_VIs_11.jpg
-rw-r--r-- 1 evilric evilric 384K 2010-10-16 01:05 fake_air_jordan_VIs_12.jpg
-rw-r--r-- 1 evilric evilric 395K 2010-10-16 01:05 fake_air_jordan_VIs_13.jpg
-rw-r--r-- 1 evilric evilric 408K 2010-10-16 01:05 fake_air_jordan_VIs_14.jpg
-rw-r--r-- 1 evilric evilric 451K 2010-10-16 01:05 fake_air_jordan_VIs_15.jpg
-rw-r--r-- 1 evilric evilric 479K 2010-10-16 01:05 fake_air_jordan_VIs_16.jpg
-rw-r--r-- 1 evilric evilric 287K 2010-10-16 01:05 fake_air_jordan_VIs_17.jpg
-rw-r--r-- 1 evilric evilric 352K 2010-10-16 01:05 fake_air_jordan_VIs_18.jpg
-rw-r--r-- 1 evilric evilric 305K 2010-10-16 01:05 fake_air_jordan_VIs_19.jpg
-rw-r--r-- 1 evilric evilric 259K 2010-10-16 01:05 fake_air_jordan_VIs_20.jpg
-rw-r--r-- 1 evilric evilric 293K 2010-10-16 01:05 fake_air_jordan_VIs_21.jpg
-rw-r--r-- 1 evilric evilric 366K 2010-10-16 01:05 fake_air_jordan_VIs_22.jpg
-rw-r--r-- 1 evilric evilric 334K 2010-10-16 01:05 fake_air_jordan_VIs_23.jpg
-rw-r--r-- 1 evilric evilric 330K 2010-10-16 01:05 fake_air_jordan_VIs_24.jpg
-rw-r--r-- 1 evilric evilric 310K 2010-10-16 01:05 fake_air_jordan_VIs_25.jpg
-rw-r--r-- 1 evilric evilric 381K 2010-10-16 01:05 fake_air_jordan_VIs_26.jpg
-rw-r--r-- 1 evilric evilric 429K 2010-10-16 01:05 fake_air_jordan_VIs_27.jpg
-rw-r--r-- 1 evilric evilric 423K 2010-10-16 01:06 fake_air_jordan_VIs_28.jpg
-rw-r--r-- 1 evilric evilric 390K 2010-10-16 01:06 fake_air_jordan_VIs_29.jpg
-rw-r--r-- 1 evilric evilric 459K 2010-10-16 01:06 fake_air_jordan_VIs_30.jpg
As seen in the size listing, the new files are considerably smaller.
6. At this stage, I will delete the original files from the directory, and check the space used:
evilric@hawk:~/Desktop/fake_air_jordan_VIs$ rm DSC050*
evilric@hawk:~/Desktop/fake_air_jordan_VIs$ du -sh
12M .
The reduction in size has gone from 81 megabytes to 12 megabytes. The final result is not noticeable in the file browser:
Images after transformation
Why do it like this? It is lazy, involves less keystrokes, and allows you step away for the computer for the 2 minutes (or so) it takes to transform the images. The alternative would be close to 10 to 15 minutes of manually opening images, clicking this and that, and then saving the result. If you have several hundred holiday pictures, you could run a similar script, and then process them all while you make and enjoy a coffee.

As mentioned on this blog page, you are more than welcome to copy the text, modify it, play with it, but if you pass it onto someone else, or publish it on your own web site / blog, then make a mention of me as the original inspiration.

2 comments:

  1. You can do away with the second loop by replacing the two instances of

    fake_air_jordan_VIs_$myCount.jpg

    with

    fake_air_jordan_VIs_$(printf "%02d" $myCount).jpg

    ReplyDelete
  2. Here's an update line:

    $ for i in $(ls | sed 's/\///'); do echo $i; cd $i; myCount=0;for j in $(ls); do echo "$j -> ${i}_$(printf "%02d" $myCount).jpg"; convert $j -resize 1024x768 ${i}_$(printf "%02d" $myCount).jpg; myCount=$[$myCount+1];done; cd ..; done

    ReplyDelete