Some methods for previewing JPEG-2000 images that work on Nautilus, Nemo, and Caja.
Method 1: Use opj_decompress
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:
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
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
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:
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:
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.
Install imagemagick
which provides the convert
tool
sudo apt install imagemagick
Get the mime-types of JP2 and J2K images (see Use opj_decompress, step 2)
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;
Enable thumbnailing for JP2 images (see Use opj_decompress, step 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.
Install graphicsmagick
which provides the gm
tool
sudo apt install graphicsmagick
Get the mime-types of JP2 and J2K images (see Use opj_decompress, step 2)
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;
Enable thumbnailing for JP2 images (see Use opj_decompress, step 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.
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
.
Get the mime-types of JP2 and J2K images (see Use opj_decompress, step 2)
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;
Enable thumbnailing for JP2 images (see Use opj_decompress, step 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.
Install ffmpeg
sudo apt install ffmpeg
Get the mime-types of JP2 and J2K images (see Use opj_decompress, step 2)
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;
Enable thumbnailing for JP2 images (see Use opj_decompress, step 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.