Reformating images with App::BlurFill

You might know that I publish books about Perl at Perl School. What you might now know is that I also publish more general technical books at Clapham Technical Press. If you scroll down to the bottom of that page, you’ll see a list of the books that I’ve published. You’ll also see evidence of the problem I’ve been solving this morning. Books tend to have covers that are in a portrait aspect ratio. But the template I’m using to display them requires images in a landscape aspect ratio. This is a common enough problem. And, of course, we’ve developed a common way of getting around it. You’ll see it on that page. We create a larger version of the image (large enough to fill the width of where the image is displayed), apply some level of Gaussian blur to the image and insert a new copy of the image over that. So we get our original image with a tastefully blurred background which echoes the colour of the image. ChatGPT tells me this is called a “Blurred Fill”. So that’s all good. But as I’m publishing more books, I need to create these images on a pretty regular basis. And, of course, if I do something more than three or four times, I will want to automate. A while ago, I wrote a simple program called “blur” that used Imager to apply the correct transformations to an image. But this morning, I decided I should really make that program a bit more useful. And release it to CPAN. So that’s what I’ve been doing. The Problem Adjusting images to fit various aspect ratios without losing essential content or introducing unsightly borders is a frequent challenge. Manually creating a blurred background for each image is time-consuming and inefficient, especially when dealing with multiple images or integrating into automated workflows. The Solution: App::BlurFill App::BlurFill is a Perl module and CLI tool designed to streamline the process of creating images with blurred backgrounds. It takes an input image and generates a new image where the original is centred over a blurred version of itself, adjusted to the specified dimensions. How It Works Input : Provide the source image along with the desired width and height. Processing: The tool creates a blurred version of the original image to serve as the background. It then overlays the original image, centred, onto this background. Output: A new image file with the specified dimensions, combining the original image and its blurred background. Installation and Usage Install via CPAN: cpanm App::BlurFill Then to use the CLI tool: blurfill --width=800 --height=600 input.jpg This command will generate input_blur.jpg with the specified dimensions. Web API App::BlurFill also includes a web interface built with Dancer2. You can start the web server and send POST requests with an image file to receive the processed image in response. Example using curl: curl -OJ -X POST http://localhost:5000/blur -F "image=@input.jpg" The response will be the new image file, ready for use. Under the Hood App::BlurFill is written in Perl 5.40, using the new perlclass feature. It makes use of the Imager module for image processing tasks. Currently, it supports JPG, PNG and GIF. What’s Next? Future enhancements may include: Support for modern image formats like WebP. More customisation options. A Docker container to make it easier to set up and use. Maybe a hosted version. Maybe it’s even a business idea. App::Blurred aims to simplify the task of creating visually consistent images across various platforms and devices. Feedback and contributions are welcome to help improve its functionality and usability. Please let me know if you find it useful or if there are extra features you would find useful. Oh, and why not buy some Clapham Technical Press books! The post Reformating images with App::BlurFill first appeared on Perl Hacks.

May 11, 2025 - 13:56
 0
Reformating images with App::BlurFill

You might know that I publish books about Perl at Perl School. What you might now know is that I also publish more general technical books at Clapham Technical Press. If you scroll down to the bottom of that page, you’ll see a list of the books that I’ve published. You’ll also see evidence of the problem I’ve been solving this morning.

Books tend to have covers that are in a portrait aspect ratio. But the template I’m using to display them requires images in a landscape aspect ratio. This is a common enough problem. And, of course, we’ve developed a common way of getting around it. You’ll see it on that page. We create a larger version of the image (large enough to fill the width of where the image is displayed), apply some level of Gaussian blur to the image and insert a new copy of the image over that. So we get our original image with a tastefully blurred background which echoes the colour of the image. ChatGPT tells me this is called a “Blurred Fill”.

So that’s all good. But as I’m publishing more books, I need to create these images on a pretty regular basis. And, of course, if I do something more than three or four times, I will want to automate.

A while ago, I wrote a simple program called “blur” that used Imager to apply the correct transformations to an image. But this morning, I decided I should really make that program a bit more useful. And release it to CPAN. So that’s what I’ve been doing.

The Problem

Adjusting images to fit various aspect ratios without losing essential content or introducing unsightly borders is a frequent challenge. Manually creating a blurred background for each image is time-consuming and inefficient, especially when dealing with multiple images or integrating into automated workflows.

The Solution: App::BlurFill

App::BlurFill is a Perl module and CLI tool designed to streamline the process of creating images with blurred backgrounds. It takes an input image and generates a new image where the original is centred over a blurred version of itself, adjusted to the specified dimensions.

How It Works

  1. Input : Provide the source image along with the desired width and height.
  2. Processing:
    • The tool creates a blurred version of the original image to serve as the background.
    • It then overlays the original image, centred, onto this background.
  3. Output: A new image file with the specified dimensions, combining the original image and its blurred background.

Installation and Usage

Install via CPAN:

cpanm App::BlurFill

Then to use the CLI tool:

blurfill --width=800 --height=600 input.jpg

This command will generate input_blur.jpg with the specified dimensions.

Web API

App::BlurFill also includes a web interface built with Dancer2. You can start the web server and send POST requests with an image file to receive the processed image in response.

Example using curl:

curl -OJ -X POST http://localhost:5000/blur -F "image=@input.jpg"

The response will be the new image file, ready for use.

Under the Hood

App::BlurFill is written in Perl 5.40, using the new perlclass feature. It makes use of the Imager module for image processing tasks. Currently, it supports JPG, PNG and GIF.

What’s Next?

Future enhancements may include:

  • Support for modern image formats like WebP.
  • More customisation options.
  • A Docker container to make it easier to set up and use.
  • Maybe a hosted version. Maybe it’s even a business idea.

App::Blurred aims to simplify the task of creating visually consistent images across various platforms and devices. Feedback and contributions are welcome to help improve its functionality and usability.

Please let me know if you find it useful or if there are extra features you would find useful.

Oh, and why not buy some Clapham Technical Press books!

The post Reformating images with App::BlurFill first appeared on Perl Hacks.