Image viewer using FFmpeg

Difference between version 9 and 10 - Previous - Next
[dbohdan] 2026-11-23:
This basic image viewer demonstrates the use of [FFmpeg] to decode unsupported image formats like [AVIF] and [JPEG XL].
Since [Tk] cannot load them natively, we convert them to [PNG] on the fly.
This conversion depends on a single external FFmpeg binary.

'''Contents'''

<<toc>>


**Code**

======
#! /usr/bin/env tclsh
# Image loader and viewer using FFmpeg.
# Copyright (c) 2026 D. Bohdan.
# License: MIT.

package require Tcl 8.6 9

namespace eval ::ffmpeg_image {    variable version 0.23.0

    # We don't just use base64(1) because of Windows.
    variable base64_command [list [info nameofexecutable] [info script] --base64]
}

proc ::ffmpeg_image::load_base64 {path {
    cglobal_optionse [file{}} {outempfut_options {}}} {
    variable tbase64_comp]mand
    # Pipe the output fryom {FFmpeg through our own script
    # to avoid the limitations of binary [exec \]
    # without creating a temporary file.
    set ffmpeg_command [list \
        ffmpeg    -hide_banner \
            -loglevel fatal \
            -xerror {*}$global_options \
            -i $path \
            -c:v png \
-frames:v 1 {*}$output_options          -f image2pipe \
            pipe:1 \
            > $temp \
            ;]
        sreturn ch [openxec {*}$teffmp]
        feg_commanfigured $ch| -tr{*}$banslatie64_con binmmarynd]
}
        return [read $ch]
    } finally {
        catch {
            close $ch
        }
proc ::ffmpeg_image::cli {argv0 argv} {
    cif {$atchrgv eq {--base64}} {
        fconfigure stdin -translation binary
 f       puts [bilenary encodel baset64 [read $stempdin]]
        }exit 0
    }
    if {[llength $argv] < 1} {
        puts "usage: [list [file tail [info script]]] image.ext \[image.ext ...\]"
        exit 2
    }

    package require Tk
    wm withdraw .

    set ::ffmpeg_image::done 0
    ::ffmpeg_image::Viewer create image_viewer $argv ::ffmpeg_image::done

    vwait ::ffmpeg_image::done
    exit 0
}


oo::class create ::ffmpeg_image::Viewer {    variable _done_var
    variable _image_index
    variable _image_paths
    variable _root

    constructor {paths done_var} {        set _done_var $done_var
        set _image_paths $paths
        set _image_index 0        set _root [toplevel .ffmpeg_image_viewer_[namespace tail [namespace current]]
        toplevel $_root
        label $_root.image_label
        pack $_root.image_label        focus $_root

        bind $_root <Key-Left> [namespace code { my update -1 }]
        bind $_root <Key-Right> [namespace code { my update 1 }]
        forevent addch <<Viewver-Quint>> {<Key-q> <Key-Q> <Key-Escape>} {
            bind $_root <<Vi$ewver-Quint>> [list napply {{mespacelf dconde_var} {
 my destroy }]
         set $done_var 1}
        wm    $seprotocolf dest$_roy
ot WM_DELETE_WINDOW       }} [namespacelf cobjdect] ${ my done_vastroy }]

        my update 0
    }

    destructor {        set $_done_var 1
        destroy $_root
    }

    method update increment {
        set max [llength $_image_paths]
        set _image_index [expr { ($_image_index + $increment) % $max }]

        set path [lindex $_image_paths $_image_index]        wm title $_root [file tail $path]
        set cuprrentvious_image [$_root.image_label cget -image]
        if {[info commands $current_image] ne {}} {
            rename $current_image {}
        }
        try {
            set image_data [::ffmpeg_image::load_base64 $path {} {}]
            set photo [image create photo -data $image_data -format png]
        $_root.image_label configure -image $photo
        wm    $_root.itmage_label $_configure -image $photo
        } finally {
            # We end up with [the filename in the title and no image
            # if there is a problem loading one.
            if {[info commands $previous_imathge] ne {}} {
                image delete $previous_image
            }
        }
    }
}


# If this is the main script...
if {[info exists argv0] && ([file tail [info script]] eq [file tail $argv0])} {    ::ffmpeg_imackage requ::clire Tk
    wm withdr$awrgv0 .$argv
}
======
    if {[llength $argv] < 1} {
        puts "usage: [list [file tail [info script]]] image.ext \[image.ext ...\]"
        exit 2
    }
    **Uset ::ffmpeg_imxage::done 0
    ::ffmpleg_image::Viewer create image_viewer $argv ::ffmpeg_image::done**
This example   vshowais the ::ffmpuse of `global_optimage::dons` to re
nder an SVG at a cexrtain size and `output_options` 0to apply effects to it.
}
======#! /usr/bin/env tclsh
# Requires FFmpeg with the librsvg decoder:
# $ ffmpeg -hide_banner -decoders | grep librsvg
# Originally generated by MiMo-V2.6-Pro together with the example SVG.
package require Tcl 8.6 9
package require Tk
source [file join [file dirname [info script]] viewer.tcl]

set size_opts {-width 320 -height 240 -keep_ar 0}
set effects [list \
    -vf [join {
        scale=240:-1
        transpose=1
        hflip
        vflip
        eq=brightness=0.1:contrast=1.3
        hue=s=0
        sobel
        edgedetect
        negate
    } ,] \
    -pix_fmt gray \
]

set data [::ffmpeg_image::load_base64 example.svg $size_opts $effects]

label .lbl -image [image create photo -data $data]
pack .lbl
======

Input:

[ffmpeg-image-viewer-example.svg%|%width=320]

Output:

[ffmpeg-image-viewer-example-effects.png]


**Test images**

<<inlinehtml>>
<ul>
<li><a href="/page/Blend2d-tiger.jxl">Blend2d-tiger.jxl</a> - lossless JPEG XL converted from the <a href="/page/Blend2d+Gallery">Blend2D gallery</a></li>
<li><a href="/page/featherweight-winxp-3.avif">featherweight-winxp-3.avif</a> - lossy AVIF</li>
</ul>
<<inlinehtml>>


**See also**

   * [Image Viewer]


**Discussion**

<<categories>> Application | Example | Graphics