Drawing on an image 
Author Message
 Drawing on an image
Hello,

I would like to create a window which will load in a gif image, and then allow
the user to draw a rectangle over the image by clicking button-1 and dragging it
to create a rectangle (to be used for zooming purposes).

How can one do that using Tcl/Tk?  Is it possible?  My first thought was to create
an image, and then create an invisible canvas on top of the image.  Is that right?

Any help or suggestions would be appreciated.

Thanks,
Brian

--
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"                                                         "
" Name:            Brian Yarrington                          "
" Address: Department of Mathematics                 "
"          The State University of New York          "
"          Stony Brook, NY 11794-3651                "
"                                                         "

"                                                         "
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""



Fri, 17 Jul 1998 03:00:00 GMT  
 Drawing on an image
   I would like to create a window which will load in a gif image, and then allow
   the user to draw a rectangle over the image by clicking button-1 and dragging it
   to create a rectangle (to be used for zooming purposes).

   How can one do that using Tcl/Tk?  Is it possible?  My first thought was to create
   an image, and then create an invisible canvas on top of the image.  Is that right?

Here is an example where you draw a polygon on a gif image. It shouldn't be
too hard to replace the polygon by a rectangle.
Enjoy
Pierre

#! /wish

proc mkONode { x y } {
    global ONodeX ONodeY index init_index

    if { $index == $init_index } {
        set ONodeX($index) $x
        set ONodeY($index) $y
        .c create line $ONodeX($index) $ONodeY($index) $x $y -fill green \
                -tag newline$index
        set index [expr $index+1]
        set ONodeX($index) $x
        set ONodeY($index) $y

    } else {
        set ONodeX($index) $x
        set ONodeY($index) $y
        .c create line $ONodeX($index) $ONodeY($index) $x $y -fill green \
                -tag newline$index
        set index [expr $index+1]

    }

Quote:
}

proc create_poly {canvas} {
    global index init_index

    bind .c <Button-1> {
        mkONode %x %y
    }

    bind .c <Motion> {
        grab .c
        set newx %x
        set newy %y
        .c coords horlin 0 $newy $wid $newy
        .c coords verlin $newx 0 $newx $len
        if { $index > $init_index } {
            set oldindex [expr $index-1]
            .c coords newline$oldindex $ONodeX($oldindex) \
                    $ONodeY($oldindex) $newx $newy
        }
    }
    bind .c <B2-Motion> {
        set newx %x
        set newy %y
        .c coords horlin 0 $newy $wid $newy
        .c coords verlin $newx 0 $newx $len
        if { $index > $init_index } {
            set oldindex [expr $index-1]
            .c coords newline$oldindex $ONodeX($oldindex) \
                    $ONodeY($oldindex) $newx $newy
        }
    }
    bind .c <Button-3> {
        if { $index > $init_index } {
            set index [expr $index-1]
            .c delete newline$index
            .c create line $ONodeX($init_index) $ONodeY($init_index) $ONodeX($index) \
                    $ONodeY($index) -fill green
        }
        set init_index $index
        grab release .c
        grab .
        undo_binds .c
    }

Quote:
}

proc undo_binds {$canvas} {
    bind .c <Button-1> {}
    bind .c <Motion> {}
    bind .c <B2-Motion> {}
    bind .c <Button-3> {}

Quote:
}

set index 0
set init_index 0
set ONodeX(0) 0
set ONodeY(0) 0
set img1 [image create photo -file "/rb/bossart/images/mickey.gif"]

frame .f
button .f.b -text "create polygon" -command "create_poly .c"
pack .f.b -side top
pack .f -side top -expand true -fill both

canvas .c -width [image width $img1] -height [image height $img1]
.c create image 0 0 -anchor nw -image $img1
pack .c -side top -expand 1 -fill both -in .
set wid [image width $img1]
set len [image height $img1]
.c create line 0 0 $wid 0 -tags horlin -fill red
.c create line 0 0 0 $len -tags verlin -fill red

--
Lawrence Livermore National Laboratory
Non-Destructive Evaluation Section
L-416, 7000 East Avenue, Livermore, CA 94550



Sun, 19 Jul 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Draw/Print and image

2. Tkinter - Drawing on an image

3. how to draw an xpm image

4. faster way to draw lines on image

5. Drawing geometrics over Images?

6. Draw a line in an image

7. Drawing scale on an image

8. Problem with draw-image and HD 1.2

9. Drawing images with Dylan

10. please help: drawing items on photo images

11. Drawing lines to Photo images?

12. Drawing lines on top of a photo image?

 

 
Powered by phpBB® Forum Software