Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best filter for downsampling text?

I'm using ImageMagick to downsample text. I realize that there's no such thing as the best filter for every situation, but I'm thinking there must be a generally-accepted standard when it comes to downsampling text specifically (I could be wrong). Here's the list of filters available in ImageMagick:

  • Bartlett
  • Blackman
  • Bohman
  • Box
  • Catrom
  • Cubic
  • Gaussian
  • Hamming
  • Hanning
  • Hermite
  • Jinc
  • Kaiser
  • Lagrange
  • Lanczos
  • LanczosSharp
  • Lanczos2
  • Lanczos2Sharp
  • Mitchell
  • Parzen
  • Point
  • Quadratic
  • Robidoux
  • Sinc
  • SincFast
  • Triangle
  • Welsh
like image 242
David Jones Avatar asked Nov 05 '12 17:11

David Jones


2 Answers

There is no generally-accepted downsampling filter; simply because, there's no generally-accepted type-face for text. You'll need to identify the font(s) your working with, and apply the best-matching filter for said task.

  • Processing emails from 2005? Tahoma font => Hermite filter
  • Scanned faxes? Mixed high contrast => Point filter

Visual comparision of each filter's stangth & weakness can be found here & here.

I like to remind myself which filter will meet a task by generating a visual cheat-sheet.

Downsampling example

Here's a quick bash script previewing "DejaVu Sans Condensed"

#!/bin/bash

# Adjust this to type-face you'd like to preview
FONT_TO_PREVIEW="DejaVu-Sans-Condensed-Bold"

# Create a temp directory to work with
mkdir filter_tmp
cd filter_tmp

while read filter
do
    # Generate base file
    convert \
        -gravity center -font $FONT_TO_PREVIEW -background white -size 126x \
        -fill black -pointsize 12 label:$filter $filter.org.png
    # Resample
    convert $filter.org.png -filter $filter -resize 400% $filter.filter.png
    # Crop center
    mogrify -gravity center -crop 126x+0+0 +repage $filter.filter.png
    # Tile filtered image below original
    montage $filter.org.png $filter.filter.png -tile 1x2 -geometry +1+1 +label +set label $filter.png
    # Clean house
    rm $filter.filter.png $filter.org.png
# Generate list of all installed filters
done < <(identify -list filter)
montage -geometry +4+4 -tile 4x *.png ../filter_preview.png

# Clean house & display generated filter preview
cd ..
rm -rf filter_tmp
display -display :0 filter_preview.png
like image 162
emcconville Avatar answered Nov 06 '22 07:11

emcconville


Your cheat sheet is not using some of the filters as recommended: Some of them are designed for use with "-distort Resize" instead of "-resize" (see here).

An additional recommendation: The choice of color space can really improve (or worsen) results. The ImageMagick Forums post "Enlarge with sRGB, RGB, LAB, LUV, XYZ, sigmoidal...?" (which I can't link here because, as a new user, I can only put two links per answer) illustrates this. With text, I recommend "sigmoidization" with a high contrast value (above 11). See here.

like image 31
Nicolas Robidoux Avatar answered Nov 06 '22 05:11

Nicolas Robidoux