{"id":11189,"date":"2021-12-14T10:00:00","date_gmt":"2021-12-14T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=11189"},"modified":"2021-12-14T10:18:15","modified_gmt":"2021-12-14T01:18:15","slug":"converting-images-into-a-pdf-in-python","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/11189","title":{"rendered":"Converting images into a pdf in python"},"content":{"rendered":"\n<p>Last weekend, my friend asked me to help him something about rearranging images and converting them into a pdf with only portrait view and sorting by modified date . But the images were not sorted by that and some in landscape. They were in chaos. Actually there are many online tools to do that. But most of them were not compatible with all what I wanted. Then I decided to do with a python program and wrote the following small code block. Let&#8217;s take a look.<\/p>\n\n\n\n<p>I&#8217;ve used the image library called <strong>Pillow(PIL)<\/strong> and built-in module named <strong>os<\/strong>. <\/p>\n\n\n\n<p>Overall program flow is as follow. <\/p>\n\n\n\n<ol><li>Request user inputs for a pdf filename with path and image folder path to be converted.<\/li><li>With <strong>os <\/strong>module, image files are sorted by modified date.<\/li><li>Looping the sorted files, rotate the images which are in landscape, to be in portrait with the help of<strong> Pillow(PIL)<\/strong> and push the images into an empty list named img_list[ ].<\/li><li>Finally convert the images list to a pdf.<\/li><\/ol>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<pre class=\"wp-block-code\"><code>from PIL import Image\nimport os\n\n# Function to sort by modified dates\ndef getfiles(dirpath):\n    a = &#091;s for s in os.listdir(dirpath)\n         if os.path.isfile(os.path.join(dirpath, s))]\n    a.sort(key=lambda s: os.path.getmtime(os.path.join(dirpath, s)))\n    return a\n\n# declare an empty list \nimg_list = &#091;]\n\n# Request user input for pdf filename and image folder path\npdf_filename = input(\"Enter the filename of pdfincluding path to be exported : \")\nfolder = input(\"Enter the path of images folder : \")\n\nfiles = getfiles(folder) # get the files sorted by modified dates\nprint('Processing....')\n\nfor count, filename in enumerate(files):\n    image = Image.open(folder+'\/'+filename) #open each file\n    \n    # To change as portrait layout for landscape images\n    width, height = image.size \n    if(width&gt;height):\n        image = image.transpose(Image.ROTATE_90)\n    # Append each processed image in img_list&#091;]    \n    img_list.append(image)\n\n# All images in img_list&#091;] are converted to a pdf\nimg_list&#091;0].save(pdf_filename, \"PDF\" ,resolution=100.0, save_all=True, append_images=img_list&#091;1:])\nprint(\"Done!\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"683\" height=\"162\" src=\"https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/12\/blog1.png\" alt=\"\" class=\"wp-image-11190\" \/><figcaption><font><\/figcaption><\/figure>\n\n\n\n<p>This program is very simple but it can be modified to be more useful for other cases. I hope you enjoy that.<\/p>\n\n\n\n<p><font>B<\/font><font>y<\/font><font> <\/font><font>A<\/font><font>s<\/font><font>a<\/font><font>h<\/font>i<font><\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11189\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/11189\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Converting images into a pdf in python\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11189\" data-text=\"Converting images into a pdf in python\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11189\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11189\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Last weekend, my friend asked me to help him something about rearranging images and converting them into a pdf [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11189"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=11189"}],"version-history":[{"count":6,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11189\/revisions"}],"predecessor-version":[{"id":11198,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11189\/revisions\/11198"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=11189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=11189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=11189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}