Img

Tcl8.3からImgパッケージを使うのにコアへのパッチがいらなくなりました。
Imgパッケージを使うと、標準で使えるGIF,PPM,PGM形式以外にBMP,XPM,XBM,PNG,JPEG,TIFF,PostScript形式等が使えるようになります。
GIFについてはUNISYSがLZW圧縮法の特許を持っているので、miGIF圧縮法で回避しているようです。

package require Img
pack [canvas .can -width 100 -height 70]
image create photo foo -file hana.jpg
.can create image 0 0 -image foo -anchor nw

コマンド形式

pixmapイメージの場合は以下のコマンド形式を使います。
dataはXPM形式で指定します。
image create pixmap -file filename
image create pixmap -data data

photoイメージの場合は以下のコマンド形式を使います。
dataはBASE64エンコード形式またはバイナリ形式(Tcl8.3〜)で指定します。
image create photo -file filename
image create photo -data data

Photo形式

Photoイメージには以下の形式とオプションがあります。
いずれも-formatオプションで指定します。

read時の形式とオプションです。
  "bmp"
  "gif -index <n>"
  "jpeg -fast -grayscale"
  "png"
  "tiff"
  "xbm"
  "xpm"
  "postscript -index <n> -zoom <x> <y>" (-index not yet implemented)
  "window" (works only with "-data", not "-file")

write時の形式とオプションです。
  "bmp"
  "gif -interlaced <bool>" (-interlaced not yet implemented)
  "jpeg -quality <n> -smooth <n> -grayscale -optimize -progressive"
  "png Author <name> Title <title> Description ....."
Each pair of arguments will add a named text chunk to the file.
  "tiff -compression <compression> -byteorder <byteorder>"
  "xbm"
  "xpm"

オプションの意味は以下の通りです。
  -background C: use color C as background color for transparent parts of the image.
  -byteorder: Byteorder for TIFF file. Should be one of bigendian, littleendian, network, smallendian or {}. Default: {}
  -compression: Compression for TIFF file. Should be one of none, jpeg, packbits or deflate. Default: none.
  -fast: Fast, low-quality processing.
  -grayscale: Force incoming image to grayscale/ Create monochrome file.
  -index N: Select one of the sub-images (GIF and postscript only, not yet implemented for postscript). Default value: 0
  -interlaced N: N=1: interlaced. N=0: non-interlaced (not yet implemented).
  -optimize: Optimize Huffman table.
  -progressive: Create progressive file (JPEG only).
  -quality N: Compression quality (0..100; 5-95 is useful range). Default value: 75
  -smooth N: Perform smoothing (10-30 is enough for most GIF's). Default value: 0
  -zoom X Y: Multiply image size by given scale factors. If Y is missing, the default is the same as X. X and Y are allowed to be in floating point format, but they are rounded to the nearest practically possible value. For postscript this means the zoom factors should be multiples of 1/72.

例題

カレントフォルダのすべてのJPEG画像のサムネイルを作成するスクリプトです。
Imgには画像サイズを指定してリサイズする機能がないので、BLTを使っています。
package require Img
package require BLT
namespace import blt::*

image create photo foo
image create photo bar -width 128 -height 96
foreach photo [glob *.jpg] {
    if {[string first {$} $photo] != -1} {
	continue
    }
    set out [file rootname $photo]\$.jpg
    if [file exists $out] {
	continue
    }
    foo read $photo
    winop resample foo bar box
    bar write $out -format "jpeg -quality 91 -optimize -smooth 30"
}

注意

GIF形式は256色までしか扱えませんが、JPEG形式をGIF形式に変換しようとしても、
減色する機能がないので、GIF形式で保存する時にエラーになります。ちょっと残念です。


Img