ulis, 2002-09-11. Deux procédures pour retourner une image (sans tenir compte de la transparence). La première suivant un axe vertical (comme une porte). La deuxième suivant un axe horizontal (comme un abattant).
Attention en langage anglo-saxon la première correspond à 'horizontal flip' et la seconde à 'vertical flip'.
Pourquoi
Voir Tourner une image.
Comment
Chaque nouveau point est l'image d'un certain nombre d'anciens points. Sa couleur est la couleur moyenne des anciens points.
A la nouvelle dimension correspond l'ancienne dimension. Donc à un nouveau point correspond une largeur de (ancienne dimension / nouvelle dimension) points.
L'algorithme utilisé ne tient pas compte des fractions de point. Par contre il construit une image et l'image symétrique.
La première procédure
# vertical flip proc proc horizontalFlip {in out1 out2 coef} \ { set width [image width $in] set height [image height $in] set ww [expr {round($width * $coef)}] set $out1 [image create photo -width $ww -height $height] set $out2 [image create photo -width $ww -height $height] set dW [expr {1.0 / double($coef)}] set DW [expr {int($dW)}] for {set y 0} {$y < $height} {incr y} \ { for {set x 0} {$x < $ww} {incr x} \ { set x0 [expr {round($x * $dW)}] set y0 $y foreach c {r g b} { set $c 0 } set cnt 0 for {set j 0} {$j < $DW} {incr j} \ { foreach {r1 g1 b1} [$in get $x0 $y0] break foreach c {r g b} { incr $c [set ${c}1] } incr x0 incr cnt if {$x0 >= $width} { break } } foreach c {r g b} { set $c [expr [set $c] / $cnt] } set col [format #%2.2x%2.2x%2.2x $r $g $b] [set $out1] put $col -to $x $y [set $out2] put $col -to [expr {$ww - $x - 1}] $y } } }
La seconde procédure
# vertical flip proc proc verticalFlip {in out1 out2 coef} \ { set width [image width $in] set height [image height $in] set hh [expr {round($height * $coef)}] set $out1 [image create photo -width $width -height $hh] set $out2 [image create photo -width $width -height $hh] set dH [expr {1.0 / double($coef)}] set DH [expr {int($dH)}] for {set x 0} {$x < $width} {incr x} \ { for {set y 0} {$y < $hh} {incr y} \ { set x0 $x set y0 [expr {round($y * $dH)}] foreach c {r g b} { set $c 0 } set cnt 0 for {set j 0} {$j < $DH} {incr j} \ { foreach {r1 g1 b1} [$in get $x0 $y0] break foreach c {r g b} { incr $c [set ${c}1] } incr y0 incr cnt if {$y0 >= $height} { break } } foreach c {r g b} { set $c [expr [set $c] / $cnt] } set col [format #%2.2x%2.2x%2.2x $r $g $b] [set $out1] put $col -to $x $y [set $out2] put $col -to $x [expr {$hh - $y - 1}] } } }
Le test de la première procédure
# horizontal flip of color_image.gif # (download here: http://www.images.com/color_image.gif) set image color_image.gif set n 12 package require Tk catch { package require Img } image create photo _img_ -file $image set width [image width _img_] set height [image height _img_] set w2 [expr {$width / 2}] set h2 [expr {$height / 2}] canvas .c -width $width -height $height pack .c .c create image $w2 $h2 -image _img_ update raise . focus -force . set dc [expr {1.0 / $n}] set coef 1.0 for {set i $n} {$i > 0} {incr i -1} \ { horizontalFlip _img_ ::img($i) ::img(-$i) $coef .c itemconfig all -image $img($i) update set coef [expr {$coef - $dc}] } set img(0) [image create photo -width 0 -height $height] set delay [expr {1000 / $n}] set dn -1 proc step {step} \ { .c itemconfig all -image $::img($step) update if {$step == -$::n} { set ::dn 1 } if {$step == $::n} { set ::dn -1 } incr step $::dn after $::delay step $step } step 0
Le test de la seconde procédure
# vertical flip of color_image.gif # (download here: http://www.images.com/color_image.gif) set image color_image.gif set n 12 package require Tk package require Img image create photo _img_ -file $image set width [image width _img_] set height [image height _img_] set w2 [expr {$width / 2}] set h2 [expr {$height / 2}] canvas .c -width $width -height $height pack .c .c create image $w2 $h2 -image _img_ update raise . focus -force . set dc [expr {1.0 / $n}] set coef 1.0 for {set i $n} {$i > 0} {incr i -1} \ { verticalFlip _img_ ::img($i) ::img(-$i) $coef .c itemconfig all -image $img($i) update set coef [expr {$coef - $dc}] } set img(0) [image create photo -width 0 -height $height] set delay [expr {1000 / $n}] set dn -1 proc step {step} \ { .c itemconfig all -image $::img($step) update if {$step == -$::n} { set ::dn 1 } if {$step == $::n} { set ::dn -1 } incr step $::dn after $::delay step $step } step 0
Voir aussi
(à compléter)
Discussion
Catégorie Exemple | Catégorie Traitement d'image