{"id":15118,"date":"2024-03-15T10:35:35","date_gmt":"2024-03-15T10:35:35","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/"},"modified":"2025-08-06T15:54:13","modified_gmt":"2025-08-06T15:54:13","slug":"how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/","title":{"rendered":"How can Python be used to batch process and standardize&#8230;"},"content":{"rendered":"<p>To batch unify image formats and sizes, you can use the PIL library (a fork of the Pillow library) in Python. Here is an example code that you can modify and expand according to your needs:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">from<\/span> PIL <span class=\"hljs-keyword\">import<\/span> Image\r\n<span class=\"hljs-keyword\">import<\/span> os\r\n\r\n<span class=\"hljs-comment\"># \u8bbe\u7f6e\u76ee\u6807\u683c\u5f0f\u548c\u5c3a\u5bf8<\/span>\r\ntarget_format = <span class=\"hljs-string\">\"JPEG\"<\/span>\r\ntarget_size = (<span class=\"hljs-number\">500<\/span>, <span class=\"hljs-number\">500<\/span>)\r\n\r\n<span class=\"hljs-comment\"># \u904d\u5386\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u6587\u4ef6<\/span>\r\ninput_folder = <span class=\"hljs-string\">\"\/path\/to\/input\/folder\"<\/span>\r\noutput_folder = <span class=\"hljs-string\">\"\/path\/to\/output\/folder\"<\/span>\r\n<span class=\"hljs-keyword\">for<\/span> filename <span class=\"hljs-keyword\">in<\/span> os.listdir(input_folder):\r\n    <span class=\"hljs-keyword\">if<\/span> filename.endswith((<span class=\"hljs-string\">\".jpg\"<\/span>, <span class=\"hljs-string\">\".jpeg\"<\/span>, <span class=\"hljs-string\">\".png\"<\/span>)):\r\n        <span class=\"hljs-comment\"># \u6253\u5f00\u56fe\u7247\u6587\u4ef6<\/span>\r\n        image_path = os.path.join(input_folder, filename)\r\n        image = Image.<span class=\"hljs-built_in\">open<\/span>(image_path)\r\n\r\n        <span class=\"hljs-comment\"># \u7edf\u4e00\u56fe\u7247\u683c\u5f0f<\/span>\r\n        <span class=\"hljs-keyword\">if<\/span> image.<span class=\"hljs-built_in\">format<\/span> != target_format:\r\n            output_path = os.path.join(output_folder, <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{os.path.splitext(filename)[<span class=\"hljs-number\">0<\/span>]}<\/span>.<span class=\"hljs-subst\">{target_format.lower()}<\/span>\"<\/span>)\r\n            image.save(output_path, <span class=\"hljs-built_in\">format<\/span>=target_format)\r\n        <span class=\"hljs-keyword\">else<\/span>:\r\n            output_path = os.path.join(output_folder, filename)\r\n\r\n        <span class=\"hljs-comment\"># \u7edf\u4e00\u56fe\u7247\u5c3a\u5bf8<\/span>\r\n        <span class=\"hljs-keyword\">if<\/span> image.size != target_size:\r\n            resized_image = image.resize(target_size)\r\n            resized_image.save(output_path)\r\n        <span class=\"hljs-keyword\">else<\/span>:\r\n            image.save(output_path)\r\n<\/code><\/pre>\n<p>In the code above, you will need to set input_folder as the directory path containing all the images to be processed, and output_folder as the directory path where the processed images will be saved. The code will then go through all image files in input_folder and open each image.<\/p>\n<p>The code will first check if the image format is the desired one, if not, it will save the image in the desired format. Then, it will check if the image size matches the target size, if not, it will adjust the image to the target size. Finally, the code will save the processed image to the output_folder while keeping the original file name unchanged.<\/p>\n<p>Before running the code, please make sure to install the Pillow library (pip install Pillow) and specify the correct folder path.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To batch unify image formats and sizes, you can use the PIL library (a fork of the Pillow library) in Python. Here is an example code that you can modify and expand according to your needs: from PIL import Image import os # \u8bbe\u7f6e\u76ee\u6807\u683c\u5f0f\u548c\u5c3a\u5bf8 target_format = &#8220;JPEG&#8221; target_size = (500, 500) # \u904d\u5386\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u6587\u4ef6 input_folder = [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[453,1402,299,1404,1403],"class_list":["post-15118","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-development","tag-guide","tag-programming","tag-technology","tag-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How can Python be used to batch process and standardize... - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn about how can python be used to batch process and standardize image formats and sizes?. Comprehensive guide with examples and best practices.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How can Python be used to batch process and standardize...\" \/>\n<meta property=\"og:description\" content=\"Learn about how can python be used to batch process and standardize image formats and sizes?. Comprehensive guide with examples and best practices.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T10:35:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-06T15:54:13+00:00\" \/>\n<meta name=\"author\" content=\"William Carter\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"William Carter\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/\"},\"author\":{\"name\":\"William Carter\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0\"},\"headline\":\"How can Python be used to batch process and standardize&#8230;\",\"datePublished\":\"2024-03-15T10:35:35+00:00\",\"dateModified\":\"2025-08-06T15:54:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/\"},\"wordCount\":185,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"Development\",\"guide\",\"programming\",\"technology\",\"tutorial\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/\",\"name\":\"How can Python be used to batch process and standardize... - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T10:35:35+00:00\",\"dateModified\":\"2025-08-06T15:54:13+00:00\",\"description\":\"Learn about how can python be used to batch process and standardize image formats and sizes?. Comprehensive guide with examples and best practices.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How can Python be used to batch process and standardize&#8230;\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0\",\"name\":\"William Carter\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g\",\"caption\":\"William Carter\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/williamcarter\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How can Python be used to batch process and standardize... - Blog - Silicon Cloud","description":"Learn about how can python be used to batch process and standardize image formats and sizes?. Comprehensive guide with examples and best practices.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/","og_locale":"en_US","og_type":"article","og_title":"How can Python be used to batch process and standardize...","og_description":"Learn about how can python be used to batch process and standardize image formats and sizes?. Comprehensive guide with examples and best practices.","og_url":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T10:35:35+00:00","article_modified_time":"2025-08-06T15:54:13+00:00","author":"William Carter","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"William Carter","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/"},"author":{"name":"William Carter","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0"},"headline":"How can Python be used to batch process and standardize&#8230;","datePublished":"2024-03-15T10:35:35+00:00","dateModified":"2025-08-06T15:54:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/"},"wordCount":185,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["Development","guide","programming","technology","tutorial"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/","url":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/","name":"How can Python be used to batch process and standardize... - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T10:35:35+00:00","dateModified":"2025-08-06T15:54:13+00:00","description":"Learn about how can python be used to batch process and standardize image formats and sizes?. Comprehensive guide with examples and best practices.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-can-python-be-used-to-batch-process-and-standardize-image-formats-and-sizes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How can Python be used to batch process and standardize&#8230;"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0","name":"William Carter","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g","caption":"William Carter"},"url":"https:\/\/www.silicloud.com\/blog\/author\/williamcarter\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/15118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=15118"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/15118\/revisions"}],"predecessor-version":[{"id":158909,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/15118\/revisions\/158909"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=15118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=15118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=15118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}