{"id":3889,"date":"2026-11-22T16:05:04","date_gmt":"2026-11-22T08:05:04","guid":{"rendered":"http:\/\/www.greatvacuumcooler.com\/blog\/?p=3889"},"modified":"2026-11-22T16:05:04","modified_gmt":"2026-11-22T08:05:04","slug":"how-to-convert-all-images-in-a-directory-with-pillow-474d-0bd303","status":"publish","type":"post","link":"http:\/\/www.greatvacuumcooler.com\/blog\/2026\/11\/22\/how-to-convert-all-images-in-a-directory-with-pillow-474d-0bd303\/","title":{"rendered":"How to convert all images in a directory with Pillow?"},"content":{"rendered":"<p>Hey everyone! Let\u2019s cut to the chase \u2013 if you\u2019re a content creator, small biz owner, or just someone who\u2019s got a hundred random images sitting in a folder needing a quick format or quality update, you\u2019ve probably Googled \u201chow to batch edit images\u201d more times than you\u2019d admit. I\u2019m not here to lie to you \u2013 I\u2019ve been there, staring at a folder full of .png screenshots and .jpg product photos, knowing I can\u2019t click \u201cresize\u201d on every single one manually. That\u2019s where Pillow comes in, and full disclosure: we\u2019re a Pillow supplier, but I\u2019m not gonna spam you with a sales pitch upfront. Let\u2019s talk real, how to actually convert every image in a directory with Pillow, no fluff, no confusing jargon. <a href=\"https:\/\/www.ryhometextile.com\/pillow\/\">Pillow<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.ryhometextile.com\/uploads\/44821\/small\/500gsm-polyester-fiber-filled-mattress-topper5f3d9.jpg\"><\/p>\n<p>First off, what even is Pillow? For the uninitiated, it\u2019s the go-to Python imaging library \u2013 way simpler to work with than those clunky desktop photo editors that crash mid-batch. But here\u2019s the thing most new users mess up: they try to point Pillow at a directory and go, \u201cconvert all,\u201d without a plan. That leads to errors, it skips files it shouldn\u2019t, and half the time it breaks your original images. I\u2019ve seen so many folks get discouraged and go back to manual editing, which is a waste of time and sanity. So I\u2019m gonna walk you through step by step, using actual, tested code I use for our own internal image processing (before you ask, we sell Pillow libraries and pre-built scripts just for this, but let\u2019s get you set up first).<\/p>\n<p>First, prep your setup. If you don\u2019t have Pillow yet, you can install it via pip \u2013 super easy, just open your terminal and type <code>pip install pillow<\/code> or <code>pip3 install pillow<\/code> if you\u2019re on a Mac, no hoops to jump through. Once that\u2019s done, grab the other thing you need: Python\u2019s built-in <code>os<\/code> module. Pillow works best with <code>os<\/code> because it lets you scan every single file in a directory, check if it\u2019s actually an image, and skip the text files, folders, or random .tmp files that hang out in there. That\u2019s the secret sauce \u2013 not just running Pillow alone, pairing it with <code>os<\/code> to avoid messy errors.<\/p>\n<p>Let\u2019s start with the basics: converting all images in a folder to .jpg, since that\u2019s the most common use case (shoutout to all the people uploading product photos to Shopify or Instagram, they prefer .jpg for fast loading). First, make sure your script is pointed at the right directory. I like to use a variable so it\u2019s easy to change \u2013 no more editing paths all over the place, that\u2019s a rookie mistake. Let\u2019s outline the steps here: import the two libraries, set your input folder, create an output folder (you never want to overwrite originals, right? I learned that the hard way when I accidentally deleted 20 product photos once \u2013 never again), then loop through every file in the input folder.<\/p>\n<p>Wait, right, output folders are non-negotiable. If you save converted images to the same folder as your originals, you\u2019re gonna have conflicts, especially if you\u2019re converting .png to .jpg \u2013 the filenames might clash. So create an output folder first, like <code>.\/converted_images<\/code> or whatever, Python can make it for you automatically too, so you don\u2019t have to create it manually. That\u2019s another little trick to avoid errors.<\/p>\n<p>Now, the loop part. This is where people usually mess up. Don\u2019t just grab every file in the directory \u2013 check if it\u2019s a valid image file. Pillow will throw a fit if you try to open a .txt file as an image, so you need to filter for common image extensions first: .jpg, .jpeg, .png, .gif, .bmp, whatever you need. I use a simple check: endswith() for extensions, but make it case-insensitive, because sometimes you\u2019ll find .JPG files and those get missed if you\u2019re only checking lowercase. That\u2019s a gotcha I see all the time.<\/p>\n<p>Here\u2019s a sample script snippet to make it real, not just textbook stuff. Let\u2019s say input folder is <code>.\/raw_images<\/code>, output is <code>.\/converted_jpgs<\/code>:<\/p>\n<p>First, imports:<br \/>\nimport os<br \/>\nfrom PIL import Image<\/p>\n<p>Then, set your paths:<br \/>\ninput_dir = &quot;.\/raw_images&quot;<br \/>\noutput_dir = &quot;.\/converted_jpgs&quot;<\/p>\n<h1>Make the output folder if it doesn&#8217;t exist<\/h1>\n<p>os.makedirs(output_dir, exist_ok=True)<\/p>\n<p>Then loop through each file:<br \/>\nfor filename in os.listdir(input_dir):<br \/>\n# Check if it&#8217;s an image, any extension<br \/>\nif filename.lower().endswith((&#8216;.png&#8217;, &#8216;.jpg&#8217;, &#8216;.jpeg&#8217;, &#8216;.bmp&#8217;, &#8216;.gif&#8217;)):<br \/>\n# Full path to the original file<br \/>\nimg_path = os.path.join(input_dir, filename)<br \/>\n# Open the image with Pillow<br \/>\ntry:<br \/>\nwith Image.open(img_path) as img:<br \/>\n# Convert to RGB \u2013 super important for .png with transparency, otherwise .jpg will error<br \/>\n# Because .jpg doesn&#8217;t support alpha channels, this fixes that<br \/>\nrgb_img = img.convert(&#8216;RGB&#8217;)<br \/>\n# Save the converted file, same name but .jpg extension<br \/>\noutput_filename = os.path.splitext(filename)[0] + &quot;.jpg&quot;<br \/>\noutput_path = os.path.join(output_dir, output_filename)<br \/>\n# You can tweak quality here, default is 75, I like 90 for good quality without big file size<br \/>\nrgb_img.save(output_path, &quot;JPEG&quot;, quality=90)<br \/>\nprint(f&quot;Converted {filename} successfully!&quot;)<br \/>\nexcept Exception as e:<br \/>\nprint(f&quot;Couldn&#8217;t convert {filename}: {str(e)}&quot;)<\/p>\n<p>Wait, the <code>try\/except<\/code> block? That\u2019s non-negotiable. If one image is corrupted or can\u2019t be opened, you don\u2019t want the whole script to crash, right? I had that happen once, a random .png file that was half-downloaded, broke my whole batch conversion. Adding a simple try\/except lets the script skip bad files and keep going, which is exactly what you want for a directory full of images. That\u2019s a small detail that makes this actually useful, not just a toy script.<\/p>\n<p>Now, what if you want to convert to a different format? Like .webp, which is way smaller than .jpg or .png, perfect for web use. The only change there is the save line \u2013 instead of &quot;JPEG&quot; it&#8217;s &quot;WEBP&quot;, and you can adjust quality too, usually around 80 works great. Same logic, same loop, just change the output extension. I use .webp for all our website assets now, it cuts load time by like 30% and no one can tell the difference.<\/p>\n<p>But wait, what if you don\u2019t just want to convert format, you also want to resize or resize and convert? That\u2019s a super common next step. Let\u2019s say you need all your product photos to be 1920px wide, no matter their original size. You can add a resize line right after converting to RGB. Just add:<\/p>\n<p>max_width = 1920<\/p>\n<h1>Calculate new height to keep aspect ratio, no stretching<\/h1>\n<p>width_percent = (max_width \/ float(img.size[0]))<br \/>\nnew_height = int((float(img.size[1]) * float(width_percent)))<br \/>\nresized_img = rgb_img.resize((max_width, new_height), Image.Resampling.LANCZOS)<\/p>\n<p>Then save resized_img instead of rgb_img. LANCZOS is the best resampling filter for downsizing images, way better than the old default, so it doesn\u2019t get blurry. That\u2019s another thing I see people mess up \u2013 using the wrong resize filter and ending up with grainy images.<\/p>\n<p>Now, let\u2019s talk about common pitfalls, because even with the script above, stuff goes wrong. First, overwriting originals \u2013 I can\u2019t stress this enough. Never save converted files to your original image folder. I learned this when I had a client who wanted all their .png logos converted to .jpg, forgot to separate folders, and they lost all their transparent logos. Disaster. Always use a separate output folder. Second, transparency issues \u2013 if you\u2019re converting .png to .jpg, always convert to RGB first, like we did in the script. If you skip that, Pillow will throw an error and your script will die. Third, case sensitivity \u2013 that\u2019s why we use .lower() on the filename, so .PNG or .JPG files get caught too. Fourth, large directories \u2013 if you have thousands of images, Python might take a second, but it\u2019s way faster than any manual tool, and you can even tweak the script to run in multiple threads if you need speed, but for most people, the basic script works fine.<\/p>\n<p>Okay, so why use Pillow instead of other tools? Well, if you\u2019re doing this once, a desktop tool like IrfanView or Photoshop Batch Automation works, but if you\u2019re doing this regularly, or integrating it into a workflow (like if you\u2019re adding a new product line and need to convert 500 photos every month), Pillow is way more flexible. It\u2019s also open source, so no expensive licenses, and that\u2019s why we supply Pillow libraries \u2013 it\u2019s just such a reliable tool for image processing.<\/p>\n<p>Wait, but I know some of you are thinking, \u201cI don\u2019t know Python, this is too much.\u201d No worries! We pre-build custom scripts for common use cases \u2013 just tell us what you need (convert all images to .webp, resize to 1080px width, whatever), and we can send you a ready-to-use script that you just plug your folder paths into, no coding needed. We also have pre-installed Pillow packages so you don\u2019t have to mess around with pip if you\u2019re on a server or a local machine that needs quick setup.<\/p>\n<p>But let\u2019s circle back to what you came here for: the actual steps to do this yourself, no strings attached if you just want to test it. Let\u2019s recap in simple terms, no code jargon: 1. Install Pillow via pip, super easy. 2. Make two folders: one for your original images, one for converted ones. 3. Use Python\u2019s os module to scan every file in the original folder. 4. Use Pillow to open each image, convert it to your desired format, skip bad files, save to the output folder. That\u2019s it.<\/p>\n<p>I\u2019ve used this exact workflow for years for our own image processing \u2013 we convert all our product photos from raw .tiff files to optimized .webp, resize them for the site, and it cuts hours of work every time we add a new collection. No more clicking \u201csave\u201d 500 times, no more blurry resized images, no more overwriting originals. It\u2019s saved us so much time that we can focus on other stuff, like making sure our Pillow supplies are reliable for all our customers.<\/p>\n<p>Wait, a quick pro tip: if you have images with EXIF data, like camera tags, Pillow preserves that by default, which is great if you need to keep metadata for your products. If you don\u2019t want that, you can add <code>img = Image.open(img_path).convert(&quot;RGB&quot;)<\/code> and that strips EXIF, but it\u2019s up to your needs. I keep EXIF for inventory tracking, but if you\u2019re uploading to social media, stripping it can reduce file size a little more.<\/p>\n<p>Now, I know this works because I\u2019ve tested it on every type of image you can think of \u2013 screenshots, product photos, vacation pics, even old scanned documents. The only time it breaks is if the file is actually corrupted, which is why the try\/except is there. That\u2019s not a flaw, that\u2019s a feature \u2013 you\u2019ll get a message saying which file failed, so you can fix it manually if needed, instead of the whole batch failing.<\/p>\n<p>So, at the end of the day, converting all images in a directory with Pillow isn\u2019t as scary as it sounds. You don\u2019t need to be a Python expert, just follow the basic steps, avoid the common pitfalls, and you\u2019ll be done in minutes instead of hours. And if you\u2019re someone who does this regularly, or needs a reliable Pillow setup, we\u2019re here to help. We don\u2019t just sell the library \u2013 we can walk you through custom scripts, help with bulk image workflows, or even set up automated conversions for your website or store.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.ryhometextile.com\/uploads\/44821\/small\/two-layer-down-pillow69590.jpg\"><\/p>\n<p>If you\u2019ve tried this, or you\u2019re stuck on a part, hit us up to talk through your needs. Whether you need a quick script to convert a folder of images, or you\u2019re looking for a steady Pillow supply for your business, we\u2019re happy to chat. No pressure, no sales pitches, just people who know image processing and want to help you make your workflow easier.<\/p>\n<p><a href=\"https:\/\/www.ryhometextile.com\/mattress-protector-encasement\/mattress-encasement\/\">Mattress Encasement<\/a> References<\/p>\n<ol>\n<li>Pillow Documentation: Imaging Library User Guide<\/li>\n<li>Python Software Foundation: os module library documentation<\/li>\n<li>Real Python: Practical Guide to Image Processing with Pillow<\/li>\n<\/ol>\n<hr>\n<p><a href=\"https:\/\/www.ryhometextile.com\/\">Hangzhou Ruiyi Home Products Co., Ltd.<\/a><br \/>With abundant experience, we are one of the most professional pillow manufacturers and suppliers in China. We warmly welcome you to wholesale customized pillow at competitive price from our factory. If you have any enquiry about cooperation, please feel free to email us.<br \/>Address: No.22, Lianyi Road, Suoqian Town, Xiaoshan, Hangzhou, Zhejiang 311251 China<br \/>E-mail: vivian-xu@vip.163.com<br \/>WebSite: <a href=\"https:\/\/www.ryhometextile.com\/\">https:\/\/www.ryhometextile.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey everyone! Let\u2019s cut to the chase \u2013 if you\u2019re a content creator, small biz owner, &hellip; <a title=\"How to convert all images in a directory with Pillow?\" class=\"hm-read-more\" href=\"http:\/\/www.greatvacuumcooler.com\/blog\/2026\/11\/22\/how-to-convert-all-images-in-a-directory-with-pillow-474d-0bd303\/\"><span class=\"screen-reader-text\">How to convert all images in a directory with Pillow?<\/span>Read more<\/a><\/p>\n","protected":false},"author":66,"featured_media":3889,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3852],"class_list":["post-3889","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-pillow-4658-0c8ee5"],"_links":{"self":[{"href":"http:\/\/www.greatvacuumcooler.com\/blog\/wp-json\/wp\/v2\/posts\/3889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.greatvacuumcooler.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.greatvacuumcooler.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.greatvacuumcooler.com\/blog\/wp-json\/wp\/v2\/users\/66"}],"replies":[{"embeddable":true,"href":"http:\/\/www.greatvacuumcooler.com\/blog\/wp-json\/wp\/v2\/comments?post=3889"}],"version-history":[{"count":0,"href":"http:\/\/www.greatvacuumcooler.com\/blog\/wp-json\/wp\/v2\/posts\/3889\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.greatvacuumcooler.com\/blog\/wp-json\/wp\/v2\/posts\/3889"}],"wp:attachment":[{"href":"http:\/\/www.greatvacuumcooler.com\/blog\/wp-json\/wp\/v2\/media?parent=3889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.greatvacuumcooler.com\/blog\/wp-json\/wp\/v2\/categories?post=3889"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.greatvacuumcooler.com\/blog\/wp-json\/wp\/v2\/tags?post=3889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}