{"id":824,"date":"2008-05-28T03:52:44","date_gmt":"2008-05-28T08:52:44","guid":{"rendered":"http:\/\/www.bytebot.net\/blog\/?p=824"},"modified":"2008-05-28T03:52:44","modified_gmt":"2008-05-28T08:52:44","slug":"identifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash","status":"publish","type":"post","link":"https:\/\/www.bytebot.net\/blog\/archives\/2008\/05\/28\/identifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash","title":{"rendered":"Identifying portrait\/landscape in a set of images, with ImageMagick and BASH"},"content":{"rendered":"<p>I haven&#8217;t written much BASH of late, so was a bit rusty. <strong>The goal?<\/strong> A script that would go through a directory of JPGs, find those that are portrait shots, and place them in an appropriate folder. Do so similarly for landscapes.<\/p>\n<p><strong>Use cases?<\/strong> Those new digital photo frames. Buy two, and have many images scroll by, eh?<\/p>\n<p><strong>What&#8217;s been done?<\/strong><br \/>\nMaking use of ImageMagick&#8217;s <a href=\"http:\/\/www.imagemagick.org\/www\/identify.html\">identify<\/a> is what needs to be done. You&#8217;ll be using the <a href=\"http:\/\/www.imagemagick.org\/www\/command-line-options.html#format\"><tt>-format<\/tt><\/a> option, which normally takes a <em>type<\/em> or <em>string<\/em>. A full list of what options are available for <tt><a href=\"http:\/\/www.imagemagick.org\/www\/escape.html\">-format<\/a><\/tt> are available.<\/p>\n<p>The choice is to use <tt>-format '%[exif:orientation]'<\/tt>. The output of <tt>identify -format '%[exif:orientation]'<\/tt> is either:<\/p>\n<ul>\n<li>1 &#8211; portrait<\/li>\n<li>6 &#8211; landscape<\/li>\n<li>8 &#8211; portrait (the way I normally shoot, with the battery grip)<\/li>\n<\/ul>\n<p>I&#8217;ve not seen much documentation about the above, so it seems like these values come from trial and error&#8230; They apply for Canon cameras that have EXIF orientation details. I&#8217;d be interested to hear from others what values they&#8217;re getting (or getting pointed to some documentation).<\/p>\n<p><strong>The shell script?<\/strong><\/p>\n<pre>#!\/bin\/sh\r\n\r\nmkdir portrait\r\nmkdir landscape\r\n\r\nfor i in *.jpg;\r\n\tdo\r\n\ttype=$(\/opt\/local\/bin\/identify -format '%[exif:orientation]' $i)\r\ncase $type in\r\n\t1)\r\n\tmv $i portrait;;\r\n\t6)\r\n\tmv $i landscape;;\r\n\t8)\r\n\tmv $i portrait;;\r\nesac\r\ndone<\/pre>\n<p><strong>Other bits of BASH?<\/strong><br \/>\nIf you do: <tt> type=$(\/opt\/local\/bin\/identify -format '%[exif:orientation]' $i) <\/tt>, you can grab the value from the command (1, 6, 8) and manipulate it. Check by doing <tt>echo $type<\/tt>.<\/p>\n<p>If you&#8217;re after getting the return value from a command (i.e. 0 for success, 127 for error, and so on), you can do <tt>echo rv: $?<\/tt>.<\/p>\n<p>All in all, remember to read the <a href=\"http:\/\/wooledge.org:8000\/BashFAQ\">BashFAQ<\/a>. <a href=\"http:\/\/wooledge.org:8000\/BashGuide\/TheBasics\/TestsAndConditionals\">Tests and Conditionals<\/a> was also essential to my reading.<\/p>\n<p>ImageMagick rocks nonetheless. I&#8217;ve used it before to resize, append and much more&#8230; I&#8217;m thinking that maybe its time to read <a href=\"http:\/\/www.apress.com\/book\/view\/1590595904\">The Definitive Guide to ImageMagick<\/a>, written by <a href=\"http:\/\/www.stillhq.com\/\">Mikal<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I haven&#8217;t written much BASH of late, so was a bit rusty. The goal? A script that would go through a directory of JPGs, find those that are portrait shots, and place them in an appropriate folder. Do so similarly for landscapes. Use cases? Those new digital photo frames. Buy two, and have many images [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[153,158,155,154,157,156],"class_list":["post-824","post","type-post","status-publish","format-standard","hentry","category-general","tag-bash","tag-exif","tag-identify","tag-imagemagick","tag-landscape","tag-portrait"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p4vJD-di","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"amp_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/comments?post=824"}],"version-history":[{"count":0,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/824\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/media?parent=824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/categories?post=824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/tags?post=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}