{"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<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"mailto:?subject=%5BShared%20Post%5D%20Identifying%20portrait%2Flandscape%20in%20a%20set%20of%20images%2C%20with%20ImageMagick%20and%20BASH&body=https%3A%2F%2Fwww.bytebot.net%2Fblog%2Farchives%2F2008%2F05%2F28%2Fidentifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash&share=email\" target=\"_blank\" title=\"Click to email a link to a friend\" data-email-share-error-title=\"Do you have email set up?\" data-email-share-error-text=\"If you&#039;re having problems sharing via email, you might not have email set up for your browser. You may need to create a new email yourself.\" data-email-share-nonce=\"1cdf998f32\" data-email-share-track-url=\"https:\/\/www.bytebot.net\/blog\/archives\/2008\/05\/28\/identifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash?share=email\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-824\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.bytebot.net\/blog\/archives\/2008\/05\/28\/identifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-824\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.bytebot.net\/blog\/archives\/2008\/05\/28\/identifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-824\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.bytebot.net\/blog\/archives\/2008\/05\/28\/identifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","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<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"mailto:?subject=%5BShared%20Post%5D%20Identifying%20portrait%2Flandscape%20in%20a%20set%20of%20images%2C%20with%20ImageMagick%20and%20BASH&body=https%3A%2F%2Fwww.bytebot.net%2Fblog%2Farchives%2F2008%2F05%2F28%2Fidentifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash&share=email\" target=\"_blank\" title=\"Click to email a link to a friend\" data-email-share-error-title=\"Do you have email set up?\" data-email-share-error-text=\"If you&#039;re having problems sharing via email, you might not have email set up for your browser. You may need to create a new email yourself.\" data-email-share-nonce=\"1cdf998f32\" data-email-share-track-url=\"https:\/\/www.bytebot.net\/blog\/archives\/2008\/05\/28\/identifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash?share=email\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-824\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.bytebot.net\/blog\/archives\/2008\/05\/28\/identifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-824\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.bytebot.net\/blog\/archives\/2008\/05\/28\/identifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-824\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.bytebot.net\/blog\/archives\/2008\/05\/28\/identifying-portraitlandscape-in-a-set-of-images-with-imagemagick-and-bash?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[1],"tags":[153,158,155,154,157,156],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4vJD-di","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2199,"url":"https:\/\/www.bytebot.net\/blog\/archives\/2011\/10\/12\/how-i-prepare-expense-reports","url_meta":{"origin":824,"position":0},"title":"How I prepare expense reports","date":"12\/10\/2011","format":false,"excerpt":"Its been traditional since the old days to stick receipts on a sheet of paper and have them represent entries into an expense report. I think the only time this differed was at Sun - there we just stuffed it all into an envelope without any order and someone presumably\u2026","rel":"","context":"In &quot;General&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1210,"url":"https:\/\/www.bytebot.net\/blog\/archives\/2008\/12\/07\/flickr-and-a-gps-enabled-camera-phone","url_meta":{"origin":824,"position":1},"title":"Flickr, and a GPS enabled camera phone","date":"7\/12\/2008","format":false,"excerpt":"I have always been excited about location based services. I've found it daft that its taken so long to get a camera integrated with a GPS chip for this amount of time, considering how cheaply available GPS chips are. Yes, its taken a while for me to go the GPS-phone\u2026","rel":"","context":"In &quot;General&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2593,"url":"https:\/\/www.bytebot.net\/blog\/archives\/2012\/11\/28\/typing-on-tablets-7-vs-10","url_meta":{"origin":824,"position":2},"title":"Typing on tablets &#8211; 7&#8243; vs 10&#8243;","date":"28\/11\/2012","format":false,"excerpt":"I wrote this post entirely using my Nexus 7 (a 7\" tablet). I found that I couldn't type well using it. I had to use it in portrait mode, and use my thumbs to type, which meant that it was utterly slow to generate a post. On my 10\" iPad,\u2026","rel":"","context":"In &quot;General&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1786,"url":"https:\/\/www.bytebot.net\/blog\/archives\/2010\/06\/13\/nokia-n900-gets-a-firmware-update-pr1-2-for-maemo-5","url_meta":{"origin":824,"position":3},"title":"Nokia N900 gets a firmware update &#8211; PR1.2 for Maemo 5","date":"13\/6\/2010","format":false,"excerpt":"I've been using the Nokia N900 since March 2010, when Nokia decided to give one to every person that attended the launch. Its mostly been my roaming phone so far, and its been places ;) Naturally, I found some problems which I very quickly wrote to Glen, who heads Product\u2026","rel":"","context":"In &quot;General&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2601,"url":"https:\/\/www.bytebot.net\/blog\/archives\/2012\/12\/01\/tablet-strategy-thoughts","url_meta":{"origin":824,"position":4},"title":"Tablet strategy thoughts","date":"1\/12\/2012","format":false,"excerpt":"In my continuing quest to see how my tablet strategy is going to be going forward, I walked into an Apple reseller yesterday, in Singapore. I typed on the retina iPad, and realised that I was using only about four-fingers. This tells you that even before in landscape mode, I\u2026","rel":"","context":"In &quot;General&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":657,"url":"https:\/\/www.bytebot.net\/blog\/archives\/2007\/11\/06\/raw-management-and-conversion-in-linux","url_meta":{"origin":824,"position":5},"title":"RAW Management and Conversion in Linux","date":"6\/11\/2007","format":false,"excerpt":"Today, I was playing around with Linux, and RAW management. Not wanting to carry a laptop just for photo editing (i.e. I plan on travel without my MacBook Pro, and beautiful Lightroom, Aperture, iView Media Pro [okay, Microsoft Expression Media now], and Photoshop CS2), I figured I should make Linux\u2026","rel":"","context":"In &quot;General&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/824"}],"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}]}}