FE 'tkGooie' Utilities

'IMAGEtools' group

Show Image Files named
in a Playlist File

(a 'front end' for reading a 'playlist' file
of image filenames and showing the images
with a user-selected image viewer program)
(FE = Freedom Environment)

FE Home Page > FE Downloads Page >

FE 'tkGooies' Description Page >

FE 'tkGooies' 'IMAGEtools' Page >

This
'Show Images in a Playlist File'
tkGooie Page

INTRODUCTION to Tcl-Tk script
'tkShowImageFilesInPlaylist'

On 2013dec10, I contributed code to the Tclers' Wiki at wiki.tcl.tk in a page titled

'tkImageViewer - for Mask-Selecting Images
from a Directory Hierarchy'

--- to provide a Tk GUI 'front end' to a combination of the 'find' command and a user-selectable image viewer command/program.

The GUI was designed to offer the user the ability to

  • 'automagically' select image files within a DIRECTORY HIERARCHY --- according to criteria such as filename-mask, file-size, file-age, and file-type

    and

  • show the image files in a sequence --- with a choice from several image viewer programs --- such as 'eog' (Eye of Gnome) or ImageMagick 'display' or 'ffplay'.

This 'batch-find-images-and-view' utility was implemented as a Tk script and 2 shell scripts.

As such, it should be implementable with very few changes --- in various Linux/BSD/Mac/Unix operating system environments.

    (By editing the scripts involved, it is quite easy to switch out any of these viewer programs/commands to replace them with another image viewer that is available in a user's operating environment.)

Soon I was thinking about another 'batch viewing' utility that I would like to add to my list of 'done' Tk scripts.

Soon after submitting (and updating/enhancing) the 'find-image-files-and-view' 'FrontEnd' utility, I realized that, with relatively few changes, the Tk script and 2 shell scripts could be altered to provide a way to 'find' movie (and/or audio) files and play them with any of the many 'media-player' programs that are available on Linux and other such systems.

Some of the 'media-player' programs that I was aware of were:

  • mplayer
  • gmplayer
  • smplayer
  • gnome-mplayer
  • totem
  • ffplay
  • VLC

Several of these are GUI front-ends to the 'mplayer' program --- or they use media library routines that were used to implement the 'mplayer' program.

At least one uses the 'Gstreamer' media library.

To make a long story short, that 'batch media-file viewer' utility was made available on 2013dec15 at the Tclers' Wiki on a page titled

'tkMediaPlayer - to Batch-Select-and-Play Movie & Audio files in a Directory Hierarchy'.


A NEXT STEP IN FILE VIEWING

An intended use of the 'batch-image-viewer' and 'batch-media-player' utilities was to allow for quickly surveying image or media (video/audio) files that may be scattered throughout a directory hierarchy

--- such as sub-directories of a web site --- OR sub-directories of a home directory --- OR sub-directories of a removable storage device mounted on a computer.

However, two drawbacks are that

  • the files are viewed in a fixed order
    (according to a filename sort)

    and

  • the user may encounter many files that they are no so interested in seeing.

On the other hand, the 'batch-image-viewer' and 'batch-media-player' utilities have a 'show-selected-files' button-option that lists the files selected via the search criteria (filename-mask, file-size, file-age, and/or file-type).

The list is shown in a popup window, in a Tk 'text' widget.

So the list of files could be pasted in a text-editor window --- and the user could change the ORDER of the files and ELIMINATE files that the user does not want to view/play, for this particular group of files.

In other words, the user could rather easily make a 'playlist' of image files to be viewed with an 'image-viewer' program --- or a playlist of movie/audio files to be played with a 'media-player' program.

If I were to prepare a 'playlist' file of images, I just needed a utility to show the files --- preferably a low-cost (like free) utility that is easily enhanced/modified/fixed.

I decided to start with a 'show-image-files-in-a-playlist' Tk-script utility.

That is the subject of this page.

A 'play-media-files-in-a-playlist' utility will come later.

---

VIEWERS FOR JPEG, GIF, PNG FILES

I would like to have made my own image-file viewer, using Tcl-Tk and the Tk 'canvas' widget, say.

I could have tried to make a Tk-viewer that has the best features of viewers like 'display', 'ffplay', and 'eog', mentioned above --- while providing features that they lack.

Unfortunately, Tcl-Tk does not 'natively' support reading JPEG files yet --- and, perhaps, never.

And it requires installing Tcl-Tk 8.6 (or higher) to show PNG files. (I am currently using 8.5.)

I wanted a Tk-image-viewer that would NOT require adding a Tcl-Tk 'extension' --- which most potential users (like me) are not going to want to do.

So it seemed the most desirable way to proceed (in my view) was to use some 'external, light-weight' image-viewers --- like 'eog' and 'display' and 'ffplay' --- on the playlist --- and build a 'front-end' using Tcl-Tk to read the playlist file and display the image-files listed in the file.

---

THE GOALS

My goals for the Tcl-Tk script were:

  • provide a GUI for selecting a 'playlist' of image files.

  • allow the user to select an image-viewer program from among multiple choices.

  • allow the user to select a 'fullscreen'-size or 'default'-size mode of display.

  • provide a 'Launch' button to start showing the images, in the order of the filenames within the playlist file.

  • provide a 'Stop' button for 'interrupting' the display of a (long) sequence of images.

  • like on the 'batch-find-images-and-view' and 'batch-find-media-and-play' GUI utilities, provide 'Count' and 'List' buttons on the GUI to allow for quickly counting and displaying the filenames of the image files in the playlist file.

---

THE GUI LAYOUT

Like for the 'batch-image-viewer' and 'batch-media-player' utilities, I made a 'text-sketch' for the GUI for this 'show-image-files-in-a-playlist' utility:



  ---------------------------------------------------------------------------
  Show the *Image* Files of a *Playlist* ...  with a choice of image-viewers
  [window title]
  ---------------------------------------------------------------------------
  
  {Exit} {Help} {LaunchViewerJob} {Stop (no more images)}  {CountImages} {ShowImgFilenames}
  
  Playlist Filename: ___________________________________________________  {Browse...}
  
  Viewer Program:  O 1 - ImageMagick 'display'    O 2 - 'ffplay'   O 3 - 'eog'
  
  Display Size: O FULLSCREEN (no toolbars)  O DEFAULT (image size, up to fullscreen) 

  --------------------------------------------------------

where

   Square brackets indicate a comment (not to be placed on the GUI).
   Braces indicate a Tk 'button' widget.
   Underscores indicate a Tk 'entry' widget.
   A colon indicates that the text before the colon is on a 'label' widget.
   Capital-O indicates a Tk 'radiobutton' widget.
   Capital-X indicates a Tk 'checkbutton' widget (if any).


GUI Components

From the GUI 'sketch' above, it is seen that the GUI consists of about

  • 7 button widgets
  • 3 label widgets
  • 1 entry widget
  • 5 radiobutton widgets in 2 groups
  • 0 checkbutton widgets
  • 0 scale widgets
  • 0 listbox widgets
  • 0 canvas widgets
  • 0 text widgets

All but the 'label' widgets provide operating parameters/options in this utility.

Hence there are about 7 + 1 + 5 = 13 user-specifiable options via the GUI of this utility.


THE PLAYLIST FILE FORMAT

The intent of this utility is to allow the user to easily specify files to be viewed ... by providing a simple 'playlist'.

The playlist file for this utility contains three types of lines:



  1 - Comment lines may be indicated in the file by a # sign
      in column 1.

  2 - Lines that contain a fully-qualified directory name
      or a 'dot' directory name.
      Examples: /data/images/vacation2014 or
                $env(HOME)/vacation2014  or
                /home/fred/vacation2014  or
                .                        or
                ./camping
      These lines must start with either slash(/) or $ or dot(.).

      The dot can be used to represent the directory in which
      the playlist file lies. This useful when putting the playlist
      file in a directory of images and using the dot to
      represent the directory of the images. The directory of
      images may have sub-directories of images, as indicated
      by the last example. The dot allows for moving/copying the
      images (with the playlist file) to different directories
      without having to change the internals of the playlist file.


  3 - Lines that contain a relative filename (relative to the
      previous directory name)
      Example: MountShasta_800x600.jpg
      These lines DO NOT start with either slash (/) or $ or
      dot or #.


The first non-comment line of the playlist file should be a directory name.

There can be more than one directory name in the file.

Each directory-name-line is followed by names of image files that are in the specified directory.

A SAMPLE 'images' playlist file follows.



 # A description of this playlist file could go here.
 #
 # First directory:
 $env(HOME)/IMAGE_CAPTURE
 BernieAndSusan_atJonesWedding2007_1_264x351.jpg
 BernieAndSusan_atJonesWedding2007_1_264x351_PIXELATED.jpg
 #
 # Second directory:
 $env(HOME)/IMAGE_CAPTURE/tiles
 tile_ripple_gray_320x200.jpg


FRONT END FOR OPTIONS FOR THE VIEWERS

This 'Front End' Tk script is essentially a 'front end' for the image-viewer programs that are offered via radiobuttons on the GUI.

A second set of radiobuttons allow for running the several image-viewers in 'fullscreen' mode, instead of a 'default' mode.

The 'default' mode may vary, depending on the viewer chosen, but it is typically show-the-image-at-its-original-size- unless-it-is-too-large-for-the-screen.

In the future, if there are some other options (of at least one of the image-viewers) that would seem nice to implement, additional widgets could be added to the GUI to support those choices.

---

Beautification of the GUI

I should point out here that I was not especially interested in coming up with a 'beautiful utility'.

I just wanted a utility that would make viewing images that are specified in a playlist file (with a very simple format) --- and with several different choices of viewer --- an easy process (a small set of mouse clicks).

I am certainly interested in making pretty GUI's --- as I have indicated in some experiments that I originally posted at the Tclers' Wiki in the pages titled

and

But at this time, I am satisfied to implement the 'functionality', and let the 'beauty' go for a later date (when I have more beauty tools/code at hand).


SCREENSHOT OF THE GUI

On the basis of the GUI-layout sketch above, I ended up with the GUI seen in the following image.

Note that there are several radiobuttons that allow you to choose the image-viewer to use --- and a couple of radiobuttons to choose fullscreen-mode, or not.

---

TYPICAL SEQUENCE OF OPERATIONS WITH THE GUI

You can use the 'Browse...' button to retrieve a full filename to the filename entry field.

Before clicking on the 'LaunchViewerJob' button, you can also change radiobutton settings as needed --- for

  • a viewer program

and

  • display size
    ('fullscreen' or the 'default-size' used by the viewer).

*NOTE*:
After the image viewing starts, closing the viewer window causes the next image to be shown.

---

Optionally, if you want to check the number or names of the image files specified in the 'playlist' file, after selecting a playlist file, click on the 'CountImages' or 'ShowImageFilenames' button.

---

BREADTH AND FLEXIBILITY FEATURES OF THIS UTILITY

Note that this utility has the flexibility of a 'playlist' approach. Namely . . .

The user is given an opportunity to SELECT the files to be shown and is given the opportunity to choose the ORDER in which the selected image files are shown.

NOTE that most 'simple' image-viewers, like 'eog', and even more complex image viewers, are oriented toward 'playing' through the image files in a SINGLE directory --- usually in filename order.

This utility is oriented toward showing image files which may be scattered throughout various PARENT DIRECTORIES and THEIR SUB-DIRECTORIES --- in a play-back ORDER DETERMINED BY THE USER (that is, determined by the order of directories and files in the playlist file).


DESCRIPTION OF THE CODE

Below, I provide the Tk script code for this 'show-image-files-in-a-playlist' utility.

I follow my usual 'canonical' structure for Tk code for this Tk script:



  0) Set general window & widget parms (win-name, win-position,
     win-color-scheme, fonts-for-widgets, widget-geometry-parms,
     text-array-for-labels-etc, win-size-control).

  1a) Define ALL frames (and sub-frames, if any).
  1b) Pack   ALL frames and sub-frames.

  2) Define & pack all widgets in the frames, frame by frame.
              Within each frame, define ALL the widgets.
              Then pack the widgets.

  3) Define keyboard and mouse/touchpad/touch-sensitive-screen action
     BINDINGS, if needed.

  4) Define PROCS, if needed.

  5) Additional GUI initialization (typically with one or more of
     the procs), if needed.


This Tk coding structure is discussed in more detail on the page A Canonical Structure for Tk Code --- and variations.

This structure makes it easy for me to find code sections --- while generating and testing a Tk script, and when looking for code snippets to include in other scripts (code re-use).

I call your attention to step-zero.

One thing that I started doing in 2013 is use of a text-array --- named 'aRtext' --- for text in labels, buttons, and other widgets in the GUI.

This can make it easier for people to internationalize my scripts.

I will be using a text-array like this in most of my scripts in the future.


Experimenting with the GUI

As in all my scripts that use the 'pack' geometry manager (which is all of my 100-plus scripts, so far), I provide the four main 'pack' parameters --- '-side', '-anchor', '-fill', '-expand' --- on all of the 'pack' commands for the frames and widgets.

That helps me when I am initially testing the behavior of a GUI (the various widgets within it) as I resize the main window.

I think that I have used a pretty nice choice of the 'pack' parameters.

The label and button and radiobutton widgets stay fixed in size and relative-location if the window is re-sized --- while the entry widget expands/contracts horizontally whenever the window is re-sized horizontally.

You can experiment with the '-side', '-anchor', '-fill', and '-expand' parameters on the 'pack' commands for the various frames and widgets --- to get the widget behavior that you want.

---

Additional experimentation:

You might want to change the fonts used for the various GUI widgets.

For example, you could change '-weight' from 'bold' to 'normal' --- or '-slant' from 'roman' to 'italic'.

Or change font families.

In fact, you may NEED to change the font families, because the families I used may not be available on your computer --- and the default font that the 'wish' interpreter chooses may not be very pleasing.

I use variables to set geometry parameters of widgets --- parameters such as border-widths and padding.

And I have included the '-relief' parameter on the definitions of frames and widgets.

Feel free to experiment with those 'appearance' parameters as well.

If you find the gray 'palette' of the GUI is not to your liking, you can change the value of the RGB parameter supplied to the 'tk_setPalette' command near the top of the code.


Some features in the code

There are plenty of comments in the code to describe what most of the code-sections are doing.

You can look at the top of the PROCS section of the code to see a list of the procs used in this script, along with brief descriptions of how they are called and what they do.

The main procs are :



  'get_playlist_filename'   - called by the 'Browse...' button
                              next to the filename entry field

  'count_images'            - called by the 'CountImages' button.

  'show_image_filenames'    - called by the 'ShowImgFilenames' button.

  'show_images'             - called by the 'LaunchViewerJob' button.

  'popup_msgVarWithScroll' - called by 'Help' button to show HELPtext var.
                                    Also called via the 'CountImages' and
                                    and 'ShowImgFilenames' buttons


Like with the 'batch-find-images-and-view' and 'batch-find-media-files-and-play' Tk GUI utilities, I used the following statement to allow the GUI to be expanded in the x-direction, but NOT the y-direction.

wm resizable . 1 0

---

One of the trickiest things about this GUI involved finding a way to get the 'Stop' button to work.

At first I used the Tcl 'exec' command to start the viewer(s) in a 'foreground' mode.

But this locked up the Tk GUI while an image was being shown by the 'external' viewer.

I eventually settled on using the 'exec' command to start the viewer in 'background' mode.

Example:

catch {eval exec $VIEWERcommand "$IMGfilename" &} ViewerPID

Thus I had a PID (process ID) for the viewer process.

Then I used a 'while' loop, including an 'after 500' statement to occasionally check, with the 'ps' show-processes command, whether the process was still running --- thanks to a code snippet seen at

http://stackoverflow.com/questions/ 16012040/ how-do-i-check-for-spawn-id-thats-alive-tcl.

From that example, I devised the following simple loop



   while {1} {
      update
      after 500
      if {[catch {exec ps $ViewerPID} CatchMsg] != 0} {break} 
   }


The 'update' command is to allow for checking whether the 'Stop' button has been pressed --- as the 'show_images' proc is proceeding to show the next image.

    (The Stop button sets a stop-variable to 1 rather than 0. Checking that variable determines whether to stop.)

The catch-exec-ps line is used to determine whether the user has closed the image viewer window and it is now time to

  • check whether the Stop button had been pressed, and

  • if not, show the next image.


A fervent hope

It is my hope that the copious comments in the code will help Tcl-Tk coding 'newbies' get started in making GUI's like this.

Without the comments, potential young Tcler's might be tempted to return to their iPhones and iPads and iPods --- to watch videos of 'liquid mud' flowing down the hillsides of Southern California during the recent (2014mar02) rainstorms pounding a drought-ravaged California.


The Tcl-Tk CODE

Here is a link to CODE for the script

'showImgFilesInPlaylist_FrontEnd.tk'.


INSTALLING THE SCRIPT:

This 'set of just ONE script' could be put in a sub-directory of the user's home directory, such as

$HOME/apps/tkShowImgsInPlaylist

    (I used two auxiliary shell scripts, in addition to a Tk script, to implement the 'batch-find-images-and-view' and 'batch-find-media-files-and-play' utilities.

    But I found it acceptable to avoid using external shell scripts in this 'show-playlist' utility.)

The user can use their desktop system (such as MATE or Gnome or KDE) to set up the Tk script as an icon on the desktop.

Then the user can click on the icon to startup the 'front end'.

Alternatively, install the FE 'tkGooies' system and choose to run this utility from the 'IMAGEtools' toolchest of the 'tkGooies' menu system.


SOME POSSIBLE ENHANCEMENTS

This Tk script adds another utility to my list of 'done' Tk scripts --- in the 'Front Ends' category.

I plan to work on a similar script to 'play-media-files-in-a-playlist', for movie and audio files --- offering a choice of about 5 different media players.

Also I plan to work on 'front ends' for 'mplayer', 'xrandr', 'ffmpeg', and several other programs.

But I may return to this 'show-images-in-a-playlist' Tk script to provide some enhancements.

Additional options may be added to the GUI, via adding a few widgets, to allow for

  • user specification of the location of the upper-left-corner of the view-window on the 'desktop'

  • auto-image-advance (with a 'fixed' user-specified wait time) --- to cause the image display to proceed, rather than wait for the user to close the image-viewer window

  • 'stepping back' to previously shown images

    (There are many ways that this could be implemented --- some using more computer memory than others.)

---

THE BIG ENHANCEMENT - A 'NATIVE' TK IMAGE VIEWER

As I mentioned above, I would actually like to make an image viewer using Tcl-Tk and the Tk 'canvas' --- when there is a 'wish' interpreter that includes JPEG-file 'read' support.

    (I don't foresee a need for JPEG 'write' capability.)

Then I could make a viewer that combines many of the best features of image viewers like 'display' and 'ffplay' and 'eog' --- and add features that are missing from those viewers.

Until that day comes, I guess I will have to live with some of the 'quirks' of the 'display' and 'ffplay' and 'eog' viewers --- some of which are mentioned below.

---

FEATURES and IDIOSYNCRACIES OF
THE 'display' VIEWER

Although the ImageMagick 'display' command has many parameter options, it does not seem to have the capability to do a nice 'fullscreen' display of a set of images --- as revealed at

http://www.imagemagick.org/ discourse-server/ viewtopic.php?f=1&t=23110.

'display' always wants to 'auto-shrink' the window around each image.

To get 'display' windows that approach fullscreen-size, I used the '-resize' parameter to scale up the images to near the top-bottom or left-right borders of the screen.

However, for small images, the scale-up may result in crude-looking images.

---

FEATURES and IDIOSYNCRACIES OF
THE 'ffplay' VIEWER

Although the 'ffplay' command does NOT have nearly as many parameter options as the 'display' command, the 'ffplay' command may be much nicer to use for 'fullscreen' mode.

In a 'near-fullscreen-mode', the 'ffplay' command covers almost the entire screen with a black background.

'ffplay' automatically expands the images to fit in the 'ffplay' window when it is 'fullscreen'.

This can be nice for medium to large sized images.

However, for small images, the scale-up may result in crude-looking images.

---

FEATURES and IDIOSYNCRACIES OF
THE 'eog' VIEWER

In 'fullscreen' mode, the user may find it easier to use Ctrl-w to close each 'eog' window.

Otherwise, it takes one click to reduce the 'eog' window from fullscreen size, and another click to close the down-sized 'eog' window.

For small images, 'eog' has a tool icon (a magnifying-glass with a '+' in it) that can be used to enlarge an image to fit the 'eog' window when it is 'fullscreen'.


IN CONCLUSION

As I have said on several other code-donation pages on this wiki ...

There's a lot to like about a utility that is 'free freedom' --- that is, no-cost and open-source so that you can modify/enhance/fix it without having to wait for someone else to do it for you (which may be never).

A BIG THANK YOU to Ousterhout for starting Tcl-Tk, and a BIG THANK YOU to the Tcl-Tk developers and maintainers who have kept the simply MAH-velous 'wish' interpreter going.


UPDATE 2014apr22

In using this utility, I found a few changes were advisable :

  • File exists check

    Added a 'file exists' check to proc 'show_images' --- to handle cases when there may be non-existent filenames or filenames with syntax errors in the playlist file.

  • Image viewer return code check

    Added a 'RETcode' (return code) check to proc 'show_images' --- to handle cases where the display program cannot handle a specified image file.

  • 'Relative' directory names

    Added the capability to use a dot (.) at the start of any directory name in the playlist file --- to represent the directory in which the playlist file lies.

    This useful when putting the playlist file in a directory of images and we want to use the dot to represent the directory of the images.

    The dot allows for moving/copying the images (with the playlist file) to different directories without having to change the internals of the playlist file.

  • Changed a Key binding

    Changed <KeyPress> to <KeyRelease> in a binding on the playlist filename entry field.

The code above has been replaced with the updated code.

Bottom of the page for
Show Image Files named
in a Playlist File

--- a utility in the FE 'tkGooies' system,
in the 'IMAGEtools' group.

To return to a previously visited web page location, click on the Back button of your web browser a sufficient number of times. OR, use the History-list option of your web browser.
OR ...

< Go to Top of Page, above. >

Page history:

The code was created in 2014 --- and posted 2014 Mar 02 at http://wiki.tcl.tk/39462.

This FE web page was created 2014 May 13.
(as a backup and alternative to the wiki.tcl.tk page)

Page was changed 2019 Feb 25.
(Added css and javascript to try to handle text-size for smartphones, esp. in portrait orientation.)

Page was changed 2019 Jun 25.
(Specified image widths in percents to size the images according to width of the browser window.)


NOTE:
The code here MAY BECOME more 'up-to-date' than the code posted on the Tcler's Wiki ---
wiki.tcl-lang.org --- formerly wiki.tcl.tk.