{"id":576,"date":"2014-04-29T09:18:44","date_gmt":"2014-04-29T09:18:44","guid":{"rendered":"http:\/\/daniepstein.com\/daniepstein\/?p=576"},"modified":"2014-06-26T08:25:26","modified_gmt":"2014-06-26T08:25:26","slug":"convert-in-python-shell-script","status":"publish","type":"post","link":"https:\/\/daniepstein.com\/daniepstein\/convert-in-python-shell-script\/","title":{"rendered":"Convert in python shell script"},"content":{"rendered":"<p>This is really more of a reminder for me than anything else.<\/p>\n<p>Convert is a tool from the same stable as <a title=\"Mogrify \u2013 more pleasant than it sounds\" href=\"http:\/\/daniepstein.com\/daniepstein\/portfolio\">Mogrify<\/a>, and is an absolute life-saver when working with large batches of images.<\/p>\n<p>Just the other day I was using it to\u00a0crop some 2000 jpegs which\u00a0were scans of pages from a rather thick book. The odd and even pages were offset, by which I mean that the space on the outside of the page was greater than on the inside, and I wanted to crop the pages so that they looked pretty even.<\/p>\n<p>Not wanting to do this all by hand, I wrote a Python shell script to process the files.<\/p>\n<p>&nbsp;<\/p>\n<pre>#!\/usr\/bin\/env python3\r\n\r\nimport subprocess\r\nimport os\r\nimport sys\r\n\r\npath_to_jpegs = \"\/home\/dani\/big fat book\/pages\/\"\r\n\r\npath_to_output = path_to_jpegs + 'cropped\/'\r\n\r\nprint('starting...')\r\n\r\nfile_path = path_to_jpegs\r\n\r\n# loop through all the images in the specified folder\r\nfor filename in os.listdir(file_path):\r\n    if ('.jpg' in filename):    \r\n        print('@@@ processing file: ' + path_to_jpegs + filename)\r\n\r\n        # file number is needed in order to determine of the file is an odd or even page\r\n        dot_pos = filename.find('.')\r\n        file_number = int(filename[:dot_pos])\r\n        \r\n        # switch coords depending if this file is even or odd\r\n        # left page\r\n        if (file_number % 2 == 0):\r\n            #coords = '1940x2720+966+240'\r\n            coords = '1940x2720+400+238'\r\n        # right page\r\n        else:\r\n            #coords = '1940x2720+400+238'\r\n            coords = '1940x2720+966+240'\r\n        \r\n        subprocess.call(['convert', '-crop', coords, path_to_jpegs + filename, path_to_output + filename])\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is really more of a reminder for me than anything else. Convert is a tool from the same stable as Mogrify, and is an absolute life-saver when working with large batches of images. Just the other day I was &hellip; <a href=\"https:\/\/daniepstein.com\/daniepstein\/convert-in-python-shell-script\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[3],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2UnW3-9i","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/daniepstein.com\/daniepstein\/wp-json\/wp\/v2\/posts\/576"}],"collection":[{"href":"https:\/\/daniepstein.com\/daniepstein\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daniepstein.com\/daniepstein\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daniepstein.com\/daniepstein\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daniepstein.com\/daniepstein\/wp-json\/wp\/v2\/comments?post=576"}],"version-history":[{"count":7,"href":"https:\/\/daniepstein.com\/daniepstein\/wp-json\/wp\/v2\/posts\/576\/revisions"}],"predecessor-version":[{"id":583,"href":"https:\/\/daniepstein.com\/daniepstein\/wp-json\/wp\/v2\/posts\/576\/revisions\/583"}],"wp:attachment":[{"href":"https:\/\/daniepstein.com\/daniepstein\/wp-json\/wp\/v2\/media?parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daniepstein.com\/daniepstein\/wp-json\/wp\/v2\/categories?post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daniepstein.com\/daniepstein\/wp-json\/wp\/v2\/tags?post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}