Score:0

How to preview JP2 and J2K images on Nautilus and Nemo?

dk flag

I have a number of JPEG-2000 .jp2 and .j2k images that I want to preview on Nautilus or Nemo. How can I tell the file managers to create thumbnails for those files?

Score:0
dk flag

Some methods for previewing JPEG-2000 images that work on Nautilus, Nemo, and Caja.

Method 1: Use opj_decompress


  1. Install libopenjp2-tools

    sudo apt install libopenjp2-tools
    

    This package comes with the tools and codecs needed to convert JP2 and J2K images to PNG thumbnails, namely opj_decompress, opj_dump, and libopenjp2-7.

    Notes:

    • libopenjp2-tools is not available on Ubuntu 14.10 and earlier.
    • If you are on Ubuntu 18.04 or later, you may have to install libgdk-pixbuf2.0-bin (which is usually pre-installed):
      sudo apt install libgdk-pixbuf2.0-bin
      
      This package provides gdk-pixbuf-thumbnailer which will be used to resize the PNGs generated by opj_decompress to a more suitable size for thumbnails.
  2. Get the mime-types of JP2 and J2K images

    • Right-click a JP2 or J2K image, select Properties.
    • On the Basic tab, take note of what is in the parentheses for the Type field. In my case (Ubuntu 20.04), it's image/jp2 for JP2 images and image/x-jp2-codestream for J2K and J2C images.

    Alternatively, you can use xdg-mime to get these mime-types. For example, if you have example.jp2 and example.j2k in ~/Pictures, issue these commands:

    xdg-mime query filetype ~/Pictures/example.jp2
    xdg-mime query filetype ~/Pictures/example.j2k
    
  3. Create a thumbnailer script for JP2 and J2K images

    • Create a file named jp2-thumbnailer-bin in /usr/local/bin:
      sudo nano /usr/local/bin/jp2-thumbnailer-bin
      
    • Copy the following script (based on the method from this) into the file (use Ctrl+C to copy and Ctrl+Shift+V to paste into nano window):
      #!/bin/bash
      
      sInFile="$1"
      nThumbSize="$2"
      sOutFile="$3"
      
      # Get the dimensions of the input image
      sInfo="$(opj_dump -i "$sInFile")"
      sSize="$(echo "$sInfo" | grep x1 | sed -r 's|.*x1=(.*), y1=(.*)|\1\t\2|')"
      nWidth="$(echo "$sSize" | cut --fields=1)"
      nHeight="$(echo "$sSize" | cut --fields=2)"
      
      # Get the large dimension of the input image
      if((nWidth>nHeight)); then
          nLargeSize="$nWidth"
      else
          nLargeSize="$nHeight"
      fi
      
      # Generate output thumbnail;
      # Images larger than the default thumbnail size (256x256 or
      # 128x128 pixels) are scaled down so their large dimension
      # is at most 256 or 128 pixels. This significantly reduces
      # thumbnail generation time.
      if((nLargeSize<=nThumbSize)); then
          /usr/bin/opj_decompress -i "$sInFile" -o "$sOutFile".png
      else
          nResolutions="$(echo "$sInfo" | grep numresolutions | cut --delimiter='=' --fields=2 | sort --general-numeric-sort | sed 1q)"
          nReduce="$(echo "f=l($nLargeSize/$nThumbSize)/l(2);scale=0;f/1" | bc -l)"
          if((nReduce<nResolutions)); then
              nReduceFactor="$nReduce"
          else
              nReduceFactor="$((nResolutions-1))"
          fi
          /usr/bin/opj_decompress -i "$sInFile" -r "$nReduceFactor" -o "$sOutFile".png
      fi
      if [[ -e /usr/bin/gdk-pixbuf-thumbnailer ]]; then
          /usr/bin/gdk-pixbuf-thumbnailer -s "$nThumbSize" "$sOutFile".png "$sOutFile"
          rm "$sOutFile".png
      else
          mv "$sOutFile".png "$sOutFile"
      fi
      
    • Press Ctrl+O and Enter to save the file, and Ctrl+X to exit nano and return to the terminal.
    • Next, make the file executable with:
      sudo chmod +x /usr/local/bin/jp2-thumbnailer-bin
      

    Note: If you use Nemo or Caja, you can actually put the script somewhere in your home directory (e.g. ~/.local/bin) and run commands like the above without sudo. If you use Nautilus, however, you can only do so if libgnome-desktop being used by Nautilus is older than 3.28.2. To get the version number of libgnome-desktop, issue this command:

    apt list --installed | grep libgnome-desktop | cut -d ' ' -f 2 | cut -d '-' -f 1
    
  4. Create a thumbnailer entry for JP2 and J2K images

    • First, create a folder named thumbnailers in ~/.local/share.
      mkdir -p ~/.local/share/thumbnailers
      
    • Create a file named jp2.thumbnailer in that folder.
      nano ~/.local/share/thumbnailers/jp2.thumbnailer
      
    • Copy these lines into the file (use Ctrl+C to copy, Ctrl+Shift+V to paste into nano window):
      [Thumbnailer Entry]
      Exec=/usr/local/bin/jp2-thumbnailer-bin %i %s %o
      MimeType=image/jp2;image/x-jp2-codestream;
      
    • Press Ctrl+O and Enter to save the file, and Ctrl+X to exit nano.

    Notes:

    • Put the mime-types found earlier in the third line listed above (the MimeType key); separate each mime-type with a semicolon (;) and optionally end the line with a semicolon.
    • If you want thumbnails for JP2 and J2K images to be available to all users, place this thumbnailer entry in /usr/share/thumbnailers instead of ~/.local/share/thumbnailers:
      sudo nano /usr/share/thumbnailers/jp2.thumbnailer
      
    • A GUI text editor like gedit or xed can also be used to create and edit the thumbnailer entry. However, if you plan to place it in /usr/share/thumbnailers, using nano is highly recommended.
  5. Enable thumbnailing for JP2 images

    The file manager may disable thumbnailing for JP2 images. To check whether this is the case, issue one of these commands, for Nautilus, Nemo, and Caja respectively:

    gsettings get org.gnome.desktop.thumbnailers disable
    gsettings get org.cinnamon.desktop.thumbnailers disable
    gsettings get org.mate.thumbnailers disable
    

    If the result is [] or [''], then the file manager does not disable thumbnailing for any file types (and you can go straight to step 6). However, if the result is ['image/jp2'], then thumbnailing for JP2 images is disabled. To re-enable it, issue one of these commands (for Nautilus, Nemo, and Caja respectively):

    gsettings set org.gnome.desktop.thumbnailers disable "[]"
    gsettings set org.cinnamon.desktop.thumbnailers disable "[]"
    gsettings set org.mate.thumbnailers disable "[]"
    

    Alternatively, you can use dconf Editor:

    • First, install dconf-editor:
      sudo apt install dconf-editor
      
    • Open dconf Editor, then go to one of these places (for Nautilus, Nemo, and Caja respectively):
      • orggnomedesktopthumbnailersdisable.
      • orgcinnamondesktopthumbnailersdisable.
      • orgmatedesktopthumbnailersdisable.
    • Turn off Use default value.
    • Change the value in the Custom value box from ['image/jp2'] to either [] or [''].
    • Click Apply in the bottom-right corner and close dconf Editor.
  6. Clear old cached thumbnails and restart the file manager

    • First, fully close the file manager with one of these commands:
      nautilus -q
      nemo -q
      caja -q
      
    • Next, delete cached failed thumbnails:
      rm -r ~/.cache/thumbnails/fail
      
    • Optionally, delete all cached thumbnails (if you previously used unoptimized thumbnailer entries or scripts that created large thumbnails):
      rm -r ~/.cache/thumbnails/*
      
    • Finally, reopen the file manager. JP2 and J2K images should have their thumbnails now.

Method 2: Use convert


convert can be used to create thumbnails for JP2 and J2K images on Ubuntu 14.04 or Ubuntu 20.10 and later.

  1. Install imagemagick which provides the convert tool

    sudo apt install imagemagick
    
  2. Get the mime-types of JP2 and J2K images (see Use opj_decompress, step 2)

  3. Create a thumbnailer entry for JP2 and J2K images (see Use opj_decompress, step 4 for details)

    The contents of ~/.local/share/thumbnailers/jp2.thumbnailer with convert as the thumbnailer program:

    [Thumbnailer Entry]
    Exec=/usr/bin/convert %i -thumbnail %sx%s png:%o
    MimeType=image/jp2;image/x-jp2-codestream;
    
  4. Enable thumbnailing for JP2 images (see Use opj_decompress, step 5)

  5. Clear old cached thumbnails and restart the file manager (see Use opj_decompress, step 6)

Method 3: Use gm


gm supports JPEG-2000 images on Ubuntu 14.04 and 16.04.

  1. Install graphicsmagick which provides the gm tool

    sudo apt install graphicsmagick
    
  2. Get the mime-types of JP2 and J2K images (see Use opj_decompress, step 2)

  3. Create a thumbnailer entry for JP2 and J2K images (see Use opj_decompress, step 4 for details)

    The contents of ~/.local/share/thumbnailers/jp2.thumbnailer with gm as the thumbnailer program:

    [Thumbnailer Entry]
    Exec=/usr/bin/gm convert %i -thumbnail %sx%s png:%o
    MimeType=image/jp2;image/x-jp2-codestream;
    
  4. Enable thumbnailing for JP2 images (see Use opj_decompress, step 5)

  5. Clear old cached thumbnails and restart the file manager (see Use opj_decompress, step 6)

Method 4: use totem-video-thumbnailer


totem-video-thumbnailer can thumbnail JP2 and J2K images on Ubuntu 18.04 and later.

  1. Install totem and gstreamer1.0-plugins-bad

    sudo apt install totem gstreamer1.0-plugins-bad
    

    totem provides totem-video-thumbnailer, while gstreamer1.0-plugins-bad comes with the codecs needed by totem-video-thumbnailer to handle JP2 and J2K images.

    Note: totem is the default video player on GNOME desktops so it is pre-installed on Ubuntu. gstreamer1.0-plugins-bad is not pre-installed, however, probably because it's only a suggested package for totem.

  2. Get the mime-types of JP2 and J2K images (see Use opj_decompress, step 2)

  3. Create a thumbnailer entry for JP2 and J2K images (see Use opj_decompress, step 4 for details)

    The contents of ~/.local/share/thumbnailers/jp2.thumbnailer with totem-video-thumbnailer as the thumbnailer program:

    [Thumbnailer Entry]
    Exec=/usr/bin/totem-video-thumbnailer -s %s %u %o
    MimeType=image/jp2;image/x-jp2-codestream;
    
  4. Enable thumbnailing for JP2 images (see Use opj_decompress, step 5)

  5. Clear old cached thumbnails and restart the file manager (see Use opj_decompress, step 6)

Method 5: Use ffmpeg


ffmpeg supports JP2 and J2K images on Ubuntu 16.04 and later.

  1. Install ffmpeg

    sudo apt install ffmpeg
    
  2. Get the mime-types of JP2 and J2K images (see Use opj_decompress, step 2)

  3. Create a thumbnailer entry for JP2 and J2K images (see Use opj_decompress, step 4 for details)

    The contents of ~/.local/share/thumbnailers/jp2.thumbnailer with ffmpeg as the thumbnailer program:

    [Thumbnailer Entry]
    Exec=/usr/bin/ffmpeg -y -i %i -filter scale=%s:%s:force_original_aspect_ratio=1 -f apng %o
    MimeType=image/jp2;image/x-jp2-codestream;
    
  4. Enable thumbnailing for JP2 images (see Use opj_decompress, step 5)

  5. Clear old cached thumbnails and restart the file manager (see Use opj_decompress, step 6)

Summary


Tested on → Ubuntu 14.04 Ubuntu 16.04 Ubuntu 18.04, 20.04; Linux Mint 20 Cinnamon; Ubuntu MATE 20.04 Ubuntu 20.10, 21.04
opj_decompress N/A ✔️ ✔️ ✔️
convert ✔️ ❌️ ❌️ ✔️
gm ✔️ ✔️ ❌️ ❌️
totem-video-thumbnailer ❌️ ❌️ ✔️ ✔️
ffmpeg N/A ✔️ ✔️ ✔️

Note: Using opj_decompress or totem-video-thumbnailer is recommended as they offer significantly faster decompression speed and produce smaller thumbnails.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.