본문으로 바로가기

이미지 재주꾼 Img

category 카테고리 없음 2024. 6. 21. 16:12

http://members.chello.nl/~j.nijtmans/img.html

 

Img 패키지의 등장으로 Tcl8.3부터 Tcl/Tk 내부를 수정할 필요가 없어졌습니다. Img 패키지를 사용하면, 표준으로 사용할 수 있는 GIF, PPM, PGM 포맷 이외에도, BMP, XPM, XBM, PNG, JPEG, TIFF, PostScript 포맷의 파일을 사용할 수 있습니다. Img패키지는 ActiveTcl에 기본으로 포함되어 있습니다.

package require Img
pack [canvas .can -width 275 -height 360]

image create photo foo -file import101.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 인코딩(encoding) 형식의 바이너리 형식(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 포맷의 파일들을 thumnail 형식으로 출력해주는 코드입니다. 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 포맷으로 변경시 색이 감소가 되는 문제 있습니다.