Contact us
Leave a message

Bitte benutzen:

Mein öffentlicher Kryptographie-Schlüssel


Mitglied bei

Automatismus

Realisiert wurden die Verarbeitungsketten mittels der Kommandozeilen-Bildbearbeitung ImageMagick. Hier Shellskript, das die eine bequeme Cartoonisierung einer beliebigen Anzahl Bilder erlaubt:

cartoonize

#!/bin/sh

VERSION=1.0.0

black=          # -b
colors=12       # -c
dither=+dither  # -d
format=         # -f
tmpmap=         # -m
outline=1       # -o
prefix=toon_    # -p
quality=100     # -q
resize_to=      # -r
saturation=200  # -s
threshold=80    # -t
suffix=         # -u
white=          # -w
colorspace=srgb # -y

function printUsage() {
  echo
  echo "cartoonize - Convert photos into cartoon-style images."
  echo "Version $VERSION"
  echo "-----------------------------------------------------------------------"
  echo "Usage: cartoonize [options] <file_pattern> [<file_pattern>...]"
  echo "with possible options:"
  echo "-?              : Display this message and exit."
  echo "-b              : Definitely include pure black into the color map"
  echo "                  finally applied to the image."
  echo "-c <colors>     : Use image file specified by path <colors> as palette,"
  echo "                  if exsists; else <colors> specifies the max. number"
  echo "                  of colors to be used. In the latter case, 0 means no"
  echo "                  color reduction. [$colors]"
  echo "-d              : Allow dithering."
  echo "-f <format>     : Convert cartoonized images to <format>."
  echo "-m              : Create on-the-fly and use a colormap of the original"
  echo "                  image for color reduction."
  echo "-o <outline>    : Outline width in pixels. [$outline]"
  echo "-p <prefix>     : String to be prepended to file name. [$prefix]"
  echo "-q <quality>    : Compression quality in percent. [$quality]"
  echo "-r <resize_to>  : Width/height of cartoonized image as imagemagick"
  echo "                  geometry specification [$resize_to]"
  echo "-s <saturation> : Percent by which to change color saturation. [$saturation]"
  echo "-t <threshold>  : Defines which lightness level (in percent) separates"
  echo "                  black from white; increase to emphasize outlines."
  echo "                  [$threshold]"
  echo "-u <suffix>     : String to be appended to filename, but before"
  echo "                  extension. [$suffix]"
  echo "-w              : Definitely include pure white into the color map"
  echo "                  finally applied to the image."
  echo "-y <color_space>: Color space to be used for color reduction. Good"
  echo "                  results are obtained with lab, srgb, yiq and yuv."
  echo "                  [$colorspace]"
  echo "Requires ImageMagick to be installed and in PATH."
}

while getopts ": b c: d f: m o: p: q: r: s: t: u: w y:" option; do
  case $option in
    b ) black=1;;
    c ) colors=$OPTARG;;
    d ) dither=-dither;;
    f ) format=$OPTARG;;
    m ) tmpmap=1;;
    o ) outline=$OPTARG;;
    p ) prefix=$OPTARG;;
    q ) quality=$OPTARG;;
    r ) resize_to=$OPTARG;;
    s ) saturation=$OPTARG;;
    t ) threshold=$OPTARG;;
    u ) suffix=$OPTARG;;
    w ) white=1;;
    y ) colorspace=$OPTARG;;
    \?) printUsage
        exit 1;;
    * ) printUsage
        exit 1;;
  esac
done

threshold="$threshold%"

resize=
if [ $resize_to ]; then resize="-resize ${resize_to}"; fi

sigma1=$outline
if [ $sigma1 -eq 1 ]; then sigma1=2; fi
radius1=$(($sigma1*2))

radius2=$outline
if [ $radius2 -eq 1 ]; then radius2=2; fi
sigma2=$(($radius2/2))

shift $((OPTIND-1))
OPTIND=1

if [ ! "$@" ]; then printUsage; exit 1; fi

for path in "$@"; do

  name=${path%.*}
  name=${name##*/}
  dir=${path%$name*}

  if [ $format ]; then
    ext=$format
  else
    ext=${path#*.}
  fi

  echo Cartoonizing $path \=\> $dir$prefix$name$suffix.$ext...

  if [ -f "$colors" ]; then
    convert $resize "$path" -modulate 100,$saturation,100 -blur ${radius1}x$sigma1 \
      -quantize $colorspace $dither -map "$colors" "${dir}color_$name.png"
  else
    if [ $colors -eq 0 ]; then
      convert $resize "$path" -modulate 100,$saturation,100 -blur ${radius1}x$sigma1 \
        "${dir}color_$name.png"
    else
      if [ $tmpmap ]; then
        addcols=""
        if [ $black ]; then
          addcols="( -size 1x1 xc:black ) +append"
        fi
        if [ $white ]; then
          addcols="$addcols ( -size 1x1 xc:white ) +append"
        fi
        convert "$path" -quantize $colorspace -colors $colors -unique-colors \
          -modulate 100,$saturation,100 $addcols -filter point -resize 1600% "${dir}map_$name.png"
        convert $resize "$path" -modulate 100,$saturation,100 -blur ${radius1}x$sigma1 \
          $dither -quantize $colorspace -map "${dir}map_$name.png" "${dir}color_$name.png"
        rm "${dir}map_$name.png"
      else
        convert $resize "$path" -modulate 100,$saturation,100 -blur ${radius1}x$sigma1 \
          $dither -quantize $colorspace -colors $colors "${dir}color_$name.png"
      fi
    fi
  fi

  convert $resize "$path" -negate -blur $radius2x$sigma2 -edge $outline -type Grayscale \
    -negate -normalize -sigmoidal-contrast 20x$threshold "${dir}outline_$name.png"
  composite -compose Multiply "${dir}color_$name.png" "${dir}outline_$name.png" \
    -quality $quality -depth 8 "$dir$prefix$name$suffix.$ext"

  rm "${dir}color_$name.png"
  rm "${dir}outline_$name.png"

done

Findige ImageMagicker werden natürlich ohne temporäre Dateien auskommen und alle Verarbeitungsschritte in einer einzigen Kommandozeile erledigen, die bis zum Mond reicht. ;-)