본문으로 바로가기

Tk의 이미지(image)

category 카테고리 없음 2025. 8. 15. 00:50

비트맵

위젯의 -bitmap 옵션에는 기본으로 제공되는 비트맵이나 XBM 형식의 비트맵 파일을 지정할 수 있습니다. 두 방식 모두 흑백이지만, -foreground와 -background 옵션을 조합하여 색상을 입힐 수 있습니다.

# 기본 제공 비트맵 사용 예시
pack [button .b -bitmap info -foreground red]

# XBM 형식의 비트맵 파일 사용 예시
pack [button .b -bitmap @foo.xbm -foreground blue]

아래는 기본 제공 비트맵입니다.


XBM 형식의 비트맵을 이미지로 취급 시 위젯의 -image 옵션을 사용합니다.  

image create bitmap foo -file data.xbm -maskfile mask.xbm  
pack [button .b -image foo]

포토

image 커맨드는 XBM 형식 외에도 GIF, PPM, PGM 형식을 사용할 수 있습니다. Tcl8.3에서는 GIF87a 및 GIF89a를 지원하지만, GIF는 UNISYS가 LZW 압축 특허를 보유하고 있어서 miGIF 압축법으로 우회하는 것 같습니다. JPEG 등 다른 형식을 사용하려면 Tcl 확장인 Img 패키지를 사용합니다. 아래는 버튼에 사진을 넣은 예입니다.

image create photo foo -file foo.gif  
pack [button .b -image foo]

포토 이미지를 캔버스 위젯 안에 붙이기

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

포토 이미지를 텍스트 위젯 안에 붙이기

pack [text .text -width 20 -height 7]  
image create photo foo -file hana.gif  
.text insert end 포토입니다.\n  
.text image create insert -image foo -name bar

 

포토에 대해서는 감마값 조정, 확대/축소, 디더링, 저장이 가능합니다.  저장이 가능하다는 것은 이미지 형식 변환도 할 수 있음을 의미합니다.

# 감마값 조정
image create photo foo -file hana.gif -gamma 1.4

# 2배 확대
image create photo foo -file hana.gif  
image create photo bar  
bar copy foo -zoom 2

# 1/2배 축소
image create photo foo -file hana.gif  
image create photo bar  
bar copy foo -subsample 2

# 디더 적용
image create photo foo -file hana.gif  
foo redither

# 저장(GIF에서 PPM으로 변환)
image create photo foo -file hana.gif  
foo write hana.ppm -format ppm

그 밖에도, 픽셀 단위 수정과 복사를 응용하면 다양한 작업이 가능합니다.

# 이미지 크기 얻기
proc getsize {image} {
    return "[image width $image] [image height $image]"
}

# 상하 반전
proc hl_Invert {src dest} {
    set size [getsize $src]
    set x [lindex $size 0]
    set y [expr [lindex $size 1] -1]
    for {set srcy $y;set desty 0} {$desty <= $y} {incr srcy -1;incr desty} {
catch {$dest copy $src -from 0 $srcy $x [expr $srcy+1] -to 0 $desty}
    }
}

# 좌우 반전
proc lr_Invert {src dest} {
    set size [getsize $src]
    set x [expr [lindex $size 0] -1]
    set y [lindex $size 1]
    for {set srcx $x;set destx 0} {$destx <= $x} {incr srcx -1;incr destx} {
catch {$dest copy $src -from $srcx 0 [expr $srcx+1] $y  -to $destx 0}
    }
}

# RGB 값 반전
proc rgb_Invert {src dest} {
    set size [getsize $src]
    set x [lindex $size 0]
    set y [lindex $size 1]
    for {set iy 0} {$iy < $y} {incr iy} {
     for {set ix 0} {$ix < $x} {incr ix} {
    set rgb [$src get $ix $iy]
    set color [format #%02X%02X%02X [lindex $rgb 2] [lindex $rgb 1] [lindex $rgb 0]]
    $dest put $color -to $ix $iy [expr $ix+1] [expr $iy+1]

커서

위젯의 커서는 -cursor 옵션으로 지정합니다. 아래는 버튼의 커서를 모래시계로 변경하는 예로 버튼 위에 마우스 커서를 올리면 커서가 모래시계로 바뀝니다.

pack [button .b -text Push -cursor watch]

커서 종류는 다음과 같습니다.  

arrow, based_arrow_down, based_arrow_up, boat, bogosity, bottom_left_corner,  
bottom_right_corner, bottom_side, bottom_tee, box_spiral, center_ptr, circle,  
clock, coffee_mug, cross, cross_reverse, crosshair, diamond_cross, dot, dotbox,  
double_arrow, draft_large, draft_small, draped_box, exchange, fleur, gobbler,  
gumby, hand1, hand2, heart, icon, iron_cross, left_ptr, left_side, left_tee,  
leftbutton, ll_angle, lr_angle, man, middlebutton, mouse, pencil, pirate, plus,  
question_arrow, right_ptr, right_side, right_tee, rightbutton, rtl_logo, sailboat,  
sb_down_arrow, sb_h_double_arrow, sb_left_arrow, sb_right_arrow, sb_up_arrow,  
sb_v_double_arrow, shuttle, sizing, spider, spraycan, star, target, tcross,  
top_left_arrow, top_left_corner, top_right_corner, top_side, top_tee, trek,  
ul_angle, umbrella, ur_angle, watch, xterm

각각의 커서 이미지는 플랫폼에 따라 다릅니다.