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.

Revisions

  1. T3sT3ro revised this gist Jan 31, 2024. No changes.
  2. T3sT3ro created this gist Jan 31, 2024.
    29 changes: 29 additions & 0 deletions pixelart-thumbnailer
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/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
    8 changes: 8 additions & 0 deletions pixelart.thumbnailer
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # 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;