ulis, 2005-09-15. Une procédure pour fondre deux images (avec gestion de la transparence).
Pourquoi
Après la transformation des images, l'animation des images.
Comment
La couleur de chaque point résultant est la somme pondérée des couleurs des points correspondants initiaux.
Sous la condition : coef1 + coef2 == 1, R = R1 * coef1 + R2 * coef2, G = G1 * coef1 + G2 * coef2, B = B1 * coef1 + B2 * coef2.
Si un point initial est transparent, on prend la couleur de l'autre point initial. Si les deux points initiaux sont transparents, le point résultant est transparent.
La procédure
# fade proc # ----------- # returns a faded image # ----------- # in1 : first image # in2 : second image # coef1 : first image transparency coeff # coef2 : optional second image transparency coeff proc fade {in1 in2 coef1 {coef2 ""}} \ { if {$coef2 == ""} { set coef2 [expr {1 - $coef1}] } set width [image width $in1] set height [image height $in1] set out [image create photo -width $width -height $height] for {set y 0} {$y < $height} {incr y} \ { for {set x 0} {$x < $width} {incr x} \ { set t1 [$in1 transparency get $x $y] set t2 [$in2 transparency get $x $y] if {$t1 && $t2} \ { [set $out] transparency set $x $y 1 } \ else \ { set col1 [$in1 get $x $y] set col2 [$in2 get $x $y] if {$t1} { set col1 $col2 } \ elseif {$t2} { set col2 $col1 } \ else \ { set col [list 0 0 0] for {set i 0} {$i < 3} {incr i} \ { set c1 [lindex $col1 $i] set c2 [lindex $col2 $i] lset col $i [expr {int($coef1 * $c1 + $coef2 * $c2)}] } $out put [eval format #%2.2x%2.2x%2.2x $col] -to $x $y } } } } return $out }
Le test
# fading images # (download here: http://www.images.com/image2.png) # (download here: http://www.images.com/image4.png) # packages package require Tk package require Img # parameters set image1 image2.png set image2 image4.png set coef 0.5 # display interface wm title . fade image create photo ::img1 -file $image1 image create photo ::img2 -file $image2 set width [image width ::img1] set height [image height ::img1] image create photo ::img3 -width $width -height $height canvas .c -width $width -height $height .c create image 0 0 -anchor nw pack .c raise . focus -force . update # create images set n 0 for {set coef 0.0} {$coef <= 1.0} {set coef [expr {$coef + 0.04}]} \ { set ::img3($n) [fade ::img1 ::img2 $coef] .c itemconfig all -image $::img3($n) update incr n } incr n -1 # display images proc set ds 1 proc step {step} \ { .c itemconfig all -image $::img3($step) update if {$step == $::n} { set ::ds -1 } if {$step == 0} { set ::ds +1 } incr step $::ds after 40 step $step } # display images step $n
Voir aussi
(à compléter)
Discussion
Catégorie Exemple | Catégorie Traitement d'image