Batch-Converting 500 PNGs to JPG: I Timed My Pillow Script Against the Alternatives
Every few months I get the same folder: a few hundred PNGs exported from a design tool, all needing to be JPG before they can go into a CMS that has a hard 25 MB per-image cap. PNGs of screenshots and photos are heavy, and nobody on the content team is going to open an image editor 500 times. So I have a script for this. But the last time I posted it, someone asked the obvious question: "is that…
In a scenario where a folder containing a few hundred PNGs exported from a design tool arrives every few months, the PNG files need to be converted to JPG due to a CMS with a strict 25 MB per-image limit. The script in question is used for this conversion process. However, a question arose regarding whether using an online converter would be faster.
To find out, a test was conducted involving 500 PNG files, each with dimensions of 800×600. Approximately 20% of these files had an alpha channel, which was primarily composed of logos and icons, while the rest were opaque. The target format was JPG with a quality setting of 85 and a white background where the alpha channel was flattened. It was crucial to avoid modifying the original source files.
The script begins by importing the necessary modules and setting the source and destination directories, ensuring they exist. It then iterates through the sorted list of files in the source directory, processing only the PNG files. For each PNG image, the script checks its mode. If the mode is RGBA, LA, or P, the image is converted to RGBA, and a white background is created.
This background is then pasted onto the image, using the alpha band as a mask to maintain transparency. For other modes, the image is converted to RGB. The script then saves the processed image in the destination directory, using the JPEG format with a quality setting of 85 and optimizing the output for slightly smaller file sizes.
During the test, key metrics were recorded. The wall time taken for the conversion process was 6.7 seconds, resulting in a throughput of approximately 75 images per second. The total input size was 744.7 MB, while the output size was 171.7 MB, representing a 23% reduction in file size compared to the input. Notably, the worst-case input scenario involved noise images, which PNG compresses poorly.
In contrast, real screenshots and photos typically yield a compression ratio closer to 60-70%, as PNG is less effective on photographic content than on noise. Throughout the process, one core of the CPU was utilized, with the rest remaining idle.
While the script was capable of converting 500 images in 6.7 seconds, it was noted that parallel processing using a ProcessPoolExecutor could potentially increase the throughput to 4-6 times faster. However, the script's performance of 6.7 seconds for 500 images was deemed sufficient, as the task did not require further optimization.
The script is designed to be run on the user's own machine, offering several advantages such as being faster, free, and offline, with complete control over the quality settings. In contrast, for one-off tasks, foreign machines, or situations involving non-technical teammates, utilizing an online tool may be more appropriate. The script's simplicity and reliability make it a dependable solution for recurring batch conversions when executed on the user's own machine.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.