我想在我的Tcl/Tk应用程序中展示一些动画GIF(主要用于“每日提示”屏幕)。在https://stackoverflow.com/a/28797669/1169096之后,我创建了这个小脚本来在循环中播放一个动画GIF:
proc nextFrame {image {time 500} {index 0}} {
if { [ catch {
$image configure -format "gif -index $index"
} stderr ] } {
set index -1
}
set nextIndex [expr $index + 1]
after $time nextFrame $image $time $nextIndex
}
set img [image create photo -file "animated.gif"]
label .w -image $img
pack .w
nextFrame $img 100这基本上是可行的,但不幸的是,它在正确呈现GIF方面存在问题。
下面是我用peek创建的一个动物化的GIF
例如。

用我的Tcl/Tk脚本来回放它,我得到了以下内容:

如您所见,存在严重的渲染问题:颜色显示错误,部分帧丢失/左灰色,.)
我认为问题可能与peek使用一些简单的帧间压缩存储动画序列的方式有关(通过使用子图像来更新只部分改变的帧--我猜,这在应用程序的屏幕上是相当典型的)。
$ identify animated.gif
animated.gif[0] GIF 895x718 895x718+0+0 8-bit sRGB 256c 0.000u 0:00.001
animated.gif[1] GIF 1x1 895x718+894+717 8-bit sRGB 256c 0.010u 0:00.002
animated.gif[2] GIF 1x1 895x718+894+717 8-bit sRGB 256c 0.010u 0:00.002
animated.gif[3] GIF 1x1 895x718+894+717 8-bit sRGB 256c 0.010u 0:00.002
animated.gif[4] GIF 45x31 895x718+87+25 8-bit sRGB 256c 0.010u 0:00.002
animated.gif[5] GIF 21x27 895x718+94+29 8-bit sRGB 256c 0.010u 0:00.002
animated.gif[6] GIF 18x23 895x718+99+35 8-bit sRGB 256c 0.010u 0:00.002
animated.gif[7] GIF 188x160 895x718+87+28 8-bit sRGB 256c 0.010u 0:00.002
animated.gif[8] GIF 186x20 895x718+88+72 8-bit sRGB 256c 0.010u 0:00.002
animated.gif[9] GIF 1x1 895x718+894+717 8-bit sRGB 256c 0.010u 0:00.002
animated.gif[10] GIF 1x1 895x718+894+717 8-bit sRGB 256c 0.010u 0:00.002如果我通过将序列转储到PNG (convert -coalesce animate.gif)来重新创建动画GIF,然后将其转换回GIF (convert *.png),它将正确地回放--但是现在动画GIF已经增长了16倍(2.5MB而不是150 16),这是我想避免的。
有什么想法,如何正确地播放一个动画GIF,有子帧更新?
我目前正在Debian/sid上运行Tcl/Tk-8.6.12 (但我想用Tcl/Tk>=8.6.10在macOS/Windows/Linux上显示我的GIF。
发布于 2022-06-03 11:21:27
多部分GIF图像中的图像有一点指示了处理方法.此位指示新层是否应替换现有图像,还是与其合并。
您的映像包含应该与现有映像相结合的层,但是您的代码只是切换到新层。
要组合这些层,您需要两个图像,并重复将不同的层从GIF复制到目标图像上:
set dir [file dirname [file normalize [info script]]]
proc nextFrame {image {time 500} {index 0}} {
if {[catch {tmpimg configure -format "gif -index $index"} stderr]} {
set nextIndex 0
} else {
set nextIndex [expr {$index + 1}]
if {$index == 0} {
$image copy tmpimg -compositingrule set
} else {
$image copy tmpimg -compositingrule overlay
}
}
after $time nextFrame $image $time $nextIndex
}
set img [image create photo -file [file join $dir animated.gif]]
# Create a helper image
image create photo tmpimg -file [$img cget -file]
label .w -image $img
pack .w
nextFrame $img 100由于某些原因,一些颜色似乎已经消失,但这更接近它应该是什么。
发布于 2022-06-09 16:24:44
相关的Tk票据现在有了一个实现,用于获取Tk8.7元数据框架中最终丢失的信息。我希望你能给我一份评论。遗憾的是,它将无助于TK8.6.10,因为这可能包含在TK8.7b1中。
谢谢,保重,哈拉尔德
https://stackoverflow.com/questions/72486189
复制相似问题