Urgent.News

What's breaking now, across thousands of outlets.

Tech

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.

Read the original at dev.to →

More in Tech

Stored Procedures in practice: implementing business rules with PL/SQL

Que é possível criar métodos e funções para manipular um banco de dados no back-end, isso você já sabe. Mas e se eu te contar que também é possível criar lógica e armazená-la dentro do próprio banco…

  • Stored Procedures store code in database for reuse
  • PL/SQL syntax includes name, parameters, header, and block
  • Procedures encapsulate business rules and reduce network traffic

Building a Real-Time Quant Trading Terminal in the Browser (Without React Frame Drops)

The Problem with High-Frequency Data in React If you have ever hooked a raw WebSocket stream into standard React state (useState and useEffect), you already know the nightmare.

  • React state updates cause performance degradation in high-frequency trading data
  • Lumen OS terminal runs entirely in browser without performance issues
  • Direct edge WebSockets stream feeds from exchange endpoints

Building an Ethiopian Calendar in Flutter

Most date libraries pretend the world runs on one calendar. Building the Zemen App — my Ethiopian calendar and date converter on the Play Store — taught me otherwise.

  • Ethiopian calendar presents unique challenge for developers
  • Zemen App built self-contained calendar engine
  • Dart-based engine handles conversions offline

Website Migration: Build a Cutover Plan Before Changing DNS

A website migration succeeds when data, traffic, and business functions arrive together. Copying files to a new server is only one part of that process.

  • Successful migration requires careful planning and execution beyond copying files.
  • Create a duplicate of the existing setup in a restricted environment for testing.
  • Gradually transition traffic by updating DNS records with overlap duration.

More from Saturday 17 October →