Pages

09 November 2012

Display image dimensions (ImageMagick).

Identify is part of ImageMagick, and displays various information about picture files. Full documentation can be found here. The various formatting escapes can be found here.
This is a good example on what can be displayed using the special escapes:

  1. clive@dogmatix > identify -format '%f [%wx%h] [%b bytes] [%[EXIF:Model]] [%[EXIF:DateTime]]\n' *JPG
  2. IMG_0121.JPG [640x480] [69165 bytes] [Canon PowerShot S3 IS] [2010:07:10 23:47:34]
  3. IMG_0122.JPG [640x480] [96095 bytes] [Canon PowerShot S3 IS] [2010:07:10 23:47:35]
  4. IMG_0123.JPG [640x480] [82048 bytes] [Canon PowerShot S3 IS] [2010:07:10 23:47:35]
  5. IMG_0124.JPG [640x480] [40431 bytes] [Canon PowerShot S3 IS] [2010:07:10 23:47:36]
This is an example I use often to get the image dimensions (Width x Height):
  1. clive@dogmatix > identify -format '%wx%h' IMG_0364.JPG
  2. 640x480
This is an example of a command I use often to move images into sub directories depending on their dimensions. The sub directories will be automatically created as needed, and the image files will be moved into their specific directories according to their dimensions
N.B. It is important to note that this will NOT handle files with spaces in their names too well (e.g. "This is my file.jpg"), if your file names have spaces in them, then you will need to rename them first before trying this.
  1. clive@dogmatix > pwd
  2. /data/wallpapers
  3. clive@dogmatix > ls
  4. Core_Redux.jpg   Phoenix1_0.jpg  Tangled_by_BJ1.jpg  Thornsofuncontrol1_0.jpg  Vertical_Insanity_v1.jpg  Weldedboxes1_0.jpg
    Grlled_2004.jpg  Science1_0.jpg  The_circle.jpg      Transport_v1.jpg          vienna.jpg
  5. clive@dogmatix > for imgfile in $(identify -format '%wx%h;%f;\n' *jpg); do DIR=$(echo "$imgfile" | awk -F';' '{print $1"_wallpapers"}'); FILE=$(echo "$imgfile" | awk -F';' '{print $2}'); mkdir $DIR; mv "$FILE" $DIR; done
  6. clive@dogmatix > tree
  7. .
  8. +-- 1024x768_wallpapers
  9. ¦   +-- The_circle.jpg
  10. ¦   +-- Transport_v1.jpg
  11. ¦   +-- Vertical_Insanity_v1.jpg
  12. +-- 1280x1024_wallpapers
  13. ¦   +-- Tangled_by_BJ1.jpg
  14. ¦   +-- Thornsofuncontrol1_0.jpg
  15. +-- 1680x1050_wallpapers
  16. ¦   +-- Core_Redux.jpg
  17. ¦   +-- Phoenix1_0.jpg
  18. ¦   +-- Science1_0.jpg
  19. ¦   +-- vienna.jpg
  20. ¦   +-- Weldedboxes1_0.jpg
  21. +-- 1920x1200_wallpapers
  22.     +-- Grlled_2004.jpg
  23.  
  24. 4 directories, 11 files
This is an explanation of what the command on line 5 will do:
  1. for imgfile in $(identify -format '%wx%h;%f;\n' *jpg); do
    This is the loop portion - it will get a list of all .jpg files in the current folder (formatted as
    <width>x<height>;<filename>;) e.g. 1024x768;myfile.jpg
  2. DIR=$(echo "$imgfile" | awk -F';' '{print $1"_wallpapers"}');
    This will create a variable called DIR to hold a special formatted name extracted for each file. From the example above this variable would be: 1024x768_wallpapers.
  3. FILE=$(echo "$imgfile" | awk -F';' '{print $2}');
    This will extract the actual file name from the first point above, so using that example the FILE variable would hold myfile.jpg
  4. mkdir $DIR;
    This will create the folder from point 2 above, i.e. 1024x768_wallpapers
  5. mv "$FILE" $DIR;
    This will now move the file myfile.jpg into the folder 1024x768_wallpapers.
  6. done
    This is the closing portion of the loop. 
If you need any further explanation, please post a comment and I will give you some more detail.
 

No comments:

Post a Comment