Skip to content

Instantly share code, notes, and snippets.

@T3sT3ro
Last active January 31, 2024 20:37
Show Gist options
  • Save T3sT3ro/fe94b6bcb295c0af191a2a3dbb1fb8f0 to your computer and use it in GitHub Desktop.
Save T3sT3ro/fe94b6bcb295c0af191a2a3dbb1fb8f0 to your computer and use it in GitHub Desktop.
thumbnailer setup for displaying pictures + pixelart correctly (not blurred due to filtering) on gnome, ubuntu
#!/bin/bash
# put this in /usr/local/bin/
# must be executable
IMG_URI="$1" # %i
THUMB_SIZE="$2" # %s
THUMB_URI="$3" # %o
IMAGE_SIZE=$(identify -format "%w %h" "$1")
PIX_SIZE=128 # Size to classify as pixel art. Picture must fit inside this box.
WIDTH=$(echo $IMAGE_SIZE | cut -d ' ' -f 1)
HEIGHT=$(echo $IMAGE_SIZE | cut -d ' ' -f 2)
# https://www.imagemagick.org/script/command-line-processing.php#geometry
if [ "$WIDTH" -le $PIX_SIZE ] && [ "$HEIGHT" -le $PIX_SIZE ]; then
# Magnify the thumbnail, apply point filtering to avoid smooth, blurred previews
/usr/bin/convert\
-colorspace sRGB "$IMG_URI"[0]\
-background transparent\
-flatten\
-filter Point\
-thumbnail "${PIX_SIZE}"\
-gravity center\
-extent "${PIX_SIZE}"\
png32:"$THUMB_URI"
else # this is the same thing as in /usr/share/thumbnailers/gdk-pixbuf-thumbnailer.thumbnailer
/usr/bin/gdk-pixbuf-thumbnailer -s $THUMB_SIZE "$IMG_URI" "$THUMB_URI"
fi
# put this is /usr/share/thumbnailers/
# this file MUST supersede /usr/share/thumbnailers/gdk-pixbuf-thumbnailer.thumbnailer to work
# either delete or comment the content of ^that file. The MIME types are superset of that.
# possibly renaming it to be alphabetically earlier than gdk-... would also work, but I didn't test it and wouldn't rely on that
[Thumbnailer Entry]
TryExec=/usr/local/bin/pixelart-thumbnailer
Exec=pixelart-thumbnailer %i %s %o
MimeType=image/png;image/bmp;image/x-bmp;image/x-MS-bmp;image/gif;image/x-icon;image/x-ico;image/x-win-bitmap;image/vnd.microsoft.icon;application/ico;image/ico;image/icon;text/ico;application/x-navi-animation;image/jpeg;image/x-portable-anymap;image/x-portable-bitmap;image/x-portable-graymap;image/x-portable-pixmap;image/tiff;image/x-xpixmap;image/x-xbitmap;image/x-tga;image/x-icns;image/jp2;image/jpeg2000;image/jpx;image/x-quicktime;image/qtif;image/vnd.adobe.photoshop;image/x-photoshop;image/x-psd;application/psd;application/x-psd;image/webp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment