Previously on “The art of the perfect featured image”

<Barbie>Math is hard!</Barbie>

The good news is that we live in a wondrous age of automation and it’s easy to program a computer to generate a perfect featured image from an original. The bad news is that unless someone coughs up the Big Bucks you’re getting an old-fashioned command-line program instead of a GUI, web service, phone app, or WordPress plugin. Some of you may prefer to do the math.

This program implements the “cropless” technique described in the last article. It’s called “autopfi” and is available from these fine retailers:

The source code is being made available for those who want to compile their own version without the ATF/CCP/CIA/DEA/DHS/FBI/IRS/NSA/(insert your state here) spyware I was compelled to include to maintain my social credit score.[1]

The Windows version is called “autopfi.exe”. The Linux version is plain “autopfi”. If you have an image directory then I suggest you copy the program into it. Otherwise the program should go into one of the directories of your computer’s executable path.

The autopfi program reads and writes JPEG images only. If you have a “glibpics” directory then an example Windows invocation is:

C:\glibpics>autopfi original perfect

Where:

  • “C:\glibpics>” is the command prompt
  • “autopfi” invokes the program
  • “original” is the name of the original image
  • “perfect” is the name of the automatically generated perfect feature image

If, as in the example, the original filename lacks a filetype then autopfi will try to read it with the following:

  • .jpg
  • .jpeg
  • .JPG
  • .JPEG

And the generated image will be given the same filetype. So if “original.jpg” exists then “perfect.jpg” will be created.

The equivalent Linux invocation is:

richard@linuxbox:~/glibpics$ ./autopfi original perfect

Where:

  • “richard@linuxbox:~/glibpics$” is the command prompt
  • “./autopfi” invokes the program
  • “original” is the name of the original image
  • “perfect” is the name of the automatically generated perfect feature image

If:

  • The original image file is specified without a filetype but an image file of that name exists, or
  • The original image file is specified with a filetype

Then autopfi assumes that you know what you’re doing and won’t automatically append a filetype to the name of the generated perfect featured image. For example:

C:\glibpics>autopfi original.jpg perfect

Will create JPEG file “perfect” without a filetype.

There’s an optional “-q” parameter to specify the JPEG quality. The range is 1 (low quality, small file) to 100 (high quality, big file) and the default is 100. WordPress featured images are so small that trying to save space is silly when the whole point of the exercise is perfection. An example is:

C:\glibpics>autopfi -q 80 bigfoot bigfoot_featured

The Windows version of autopfi is a 32 bit program. The Linux version is 64 bit so those of you with 32 bit Linux computers and/or iMacintoshes are SOL.[2]

Examples

The original of this article’s featured image is:

It’s named “cheryl_tiegs4.jpg” and is 738 pixels wide and 1087 pixels tall. On my Windows computer the command:

F:\>autopfi cheryl_tiegs4 cheryl_tiegs4_pfi

Created file “cheryl_tiegs4_pfi.jpg”. This is the result with a border added:

Another image on my computer is “shark_girl.jpg” It’s 1024 pixels wide and 683 pixels tall:

The command:

F:\>autopfi shark_girl shark_girl_pfi

Produced “shark_girl_pfi.jpg” as follows, border added:

In order to maintain my social credit score I’m compelled to balance the previous examples with this:

It’s named “burt_renolds1.jpeg” and is 474 pixels wide and 247 pixels tall. After processing with:

F:\>autopfi burt_renolds1 burt_renolds1_pfi

The result is “burt_renolds1_pfi.jpeg” and, with a border added, looks like this:

I don’t need to know the gory details

The source code ZIP file contains a directory “autopfi” containing:

  • Three program modules
  • A subdirectory “libjpeg” containing a number of JPEG library modules
  • Batch files to build and test the program

The program is written in the old-fashioned C language. Like someone with a 75 year old lathe I use a 75 year old (in computer years) compiler when developing under Windows so the Windows batch files are unlikely to work on any computer but mine. The Linux batch files use the standard gcc compiler and should work on any computer on which gcc is installed. The program is built by invoking “build.bat” under Windows and “build” under Linux. A test image is provided for your entertainment.

The JPEG library is an old version the Independent JPEG Group’s libjpeg implementation:

https://sourceforge.net/projects/gnuwin32/files/jpeg/6b-4/

Which I’ve slightly modified to compile without the warnings issued by the both the ancient and modern compilers.

I wrote the three program modules in the autopfi directory:

  • Module “resize.c” has a function to resize images. It uses two different algorithms to enlarge or shrink images.[3]
  • Module “jpegfile.c” has two functions to read and write JPEG files using the JPEG library.
  • Module “autopfi.c” is the main program that:
    • Processes the parameters and massages the filenames
    • Reads the original JPEG file
    • Generates the perfect featured image
    • Writes the perfect JPEG file

Footnotes

[1] Not really. The autopfi program doesn’t access the networking subsystem at all.

[2] I have several 32 bit Linux computers but I can’t be arsed to haul one out and compile up another version.

[3] Both resizing algorithms will enlarge and shrink images but the enlarging algorithm makes bad shrunken images and the shrinking algorithm makes bad enlarged images.