Custom thumbnails for unknown formats in Gnome

Radu Zaharia
5 min readNov 9, 2023

--

Photo by Jess Bailey on Unsplash

I just found out something cool today about file thumbnails in Linux. They of course follow a standard! It is defined by freedesktop and it bears the self-descriptive name Thumbnail Managing Standard. It establishes paths for thumbnails, thumbnail creation definitions and so on. Most Linux desktop environments follow this standard except KDE, at least the fifth version, so we get to edit and configure the thumbnails as we wish, in a portable manner.

The standard specifies that the thumbnails should be placed in the home folder, in the cache location, in the thumbnails subfolder. Sure enough, here they are:

Finding the generated thumbnails in the standard folder

If we want to generate them again, we simply delete this folder, and all thumbnails will get recreated. And talking about recreated, the standard also defines how thumbnails are made. Sure enough, there is a folder for these definitions too, /usr/share/thumbnailers:

The thumbnail generators definitions folder

If we explore one such definition, for example librsvg.thumbnailer, we see the process that converts a SVG file to a thumbnail:

Uncovering the thumbnail generator process

It’s a very small definition file, specifying the mime type, which in this case is image/svg+xml, and the command needed to create the thumbnail, in this case gdk-pixbuf-thumbnailer with a few preconfigured parameters. Let’s check another:

Looking at another thumbnail generator configuration

Again, the exact same thing: a mime type, in this case collecting all kinds of font files, and a command that when executed will produce the thumbnail form the input. This time we are using the gnome-thumbnail-font program, again with its preconfigured parameters.

The parameters are easy to guess: %i is the input file, %o is the output file, %s is the height in pixels, and %u is the input URI. Note how both definitions prefer the input URI instead of the input path, as it’s way more versatile.

Creating custom thumbnail generators

Photo by Henk Van der Westhuizen on Unsplash

Now, Gnome has thumbnails for almost everything by default, which means it has you covered no matter what kind of picture you throw at it. But what if we want thumbnails for EPUB files? This is how EPUB files look by default in Gnome Files:

Showing some thumbnails in the Pictures folder in Gnome Files

In the screenshot we can see some SVG files with their thumbnails, and an EPUB file with a generic thumbnail. This is exactly the case for a custom thumbnail generator definition. We also need a program able to scan an EPUB file and give us a thumbnail, but these are usually easy to find. I used a python script found on GitHub by Mariano Simeone. We simply need to give it an input EPUB and an output location and we are done. And yes, it even comes with the thumbnail generation definition file:

The epub-thumbnailer python script with the epub.thumbnailer generator

It also comes with an installer to make everything extra convenient, which creates a standalone executable from the python script and copies the thumbnail generator file to the standard /usr/share/thumbnailers location for us. We just run sudo python3 install.py install from the root folder and restart Gnome Files:

The new and improved thumbnail for EPUB files in Gnome Files

There are many such custom thumbnail generators, just search for “Gnome additional thumbnailers” online and you will find generators for many file formats, including EXE, ICN and more. If you cannot find one, you can write your own using FFMPEG for videos for example, or ImageMagick for pictures.

Writing a custom thumbnailer with ImageMagick

Photo by Soragrit Wongsa on Unsplash

As Gnome already has thumbnail generators for many picture formats, I had doubts I would find something that needs a generator from scratch. So, I decided to write something a bit different: a thumbnail generator that replaces the default thumbnail with a grayscale variant. Basically, I would turn all Gnome Files thumbnails black and white. This is easy to do with the open-source ImageMagick tool. First, we can resize an image using the -resize parameter to get a thumbnail:

#magick input.jpg -resize 120x120 thumbnail.png

And to make it black and white, we just add some more image magic:

#magick input.jpg -resize 120x120 -set colorspace Gray -separate -evaluate-sequence Mean thumbnail.png

Ok, we have the command. Let’s add it to a thumbnail generator definition. The general image thumbnail generator is gdk-pixbuf-thumbnailer, found at /usr/share/thumbnailers, so we will edit it with our new command:

[Thumbnailer Entry]
TryExec=magick
Exec=magick %u -resize %s -set colorspace Gray -separate -evaluate-sequence Mean %o
MimeType=image/png;image/jpeg;image/bmp;image/x-bmp;image/x-MS-bmp;image/gif;image/x-icon;image/x-ico;image/x-win-bitmap>

This is how it looks like in nano:

This is how the thumbnail generator should look

All we need to do is save, clear the thumbnail cache, and restart Gnome Files, bringing this:

Original thumbnail for the JPG file

To this:

Black and white thumbnails for JPG files

Note how the SVG files were not affected. This is normal because as we saw they are handled by a different generator.

That is all for today. I didn’t even know about the thumbnail freedesktop standard and wasn’t even thinking that I could intervene in the thumbnail generator process. But here we are, Gnome allows you to do so and create whatever thumbnails you like. Thank you for reading and see you next time!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response