#!/usr/bin/perl =head1 DESCRIPTION View random images from the supplied folder (supports . and ~). Every time you close an image a new random image will open, until the program is stopped. Inspired by "Re^2: Tk show image from web" by IB2017 https://www.perlmonks.org/index.pl?node_id=11108799 =cut use strict; use warnings; use autodie; use Encode; use File::Spec; use MIME::Base64; use Image::Info; use Imager; use Tk; use Tk::Photo; use Tk::JPEG; use Tk::PNG; my $dir = shift || die "Usage: $0 /path/to/images"; die 'Folder not found :-(' unless -d $dir; my $img = shift; # an initial image, for debugging opendir my $dh, glob $dir; my @img = grep /\.(jpe?g|gif|png|bmp)/i, readdir $dh; closedir $dh; die 'No images found, try again!' unless @img; while () { my $file = $img || $img[rand@img]; my $path = File::Spec->catfile($dir,$file); my $info = Image::Info::image_info($path); undef $img if $img; print "$path \n"; if ($info->{color_type} eq 'CMYK') { my $tmpdir = File::Spec->tmpdir; my $imager = Imager->new; $imager->read(file=>$path) or die $imager->errstr; $path = File::Spec->catfile($tmpdir,$file); # Converts CMYK to RGB $imager->write(file=>$path) or die $imager->errstr; } open my $fh, '<', $path; my $data = join '', <$fh>; close $fh; $data = MIME::Base64::encode_base64($data); my $mw = MainWindow->new; $mw->configure(-title => Encode::decode_utf8($file)); my $top = $mw->Frame()->pack(); my $image = $mw->Photo(-data => $data); my $label = $top->Label(-image => $image)->pack(); print $image->width, 'x', $image->height, "\n\n"; # From Tk::Image perldoc: # # CAVEATS # # It's necessary to use the "delete" method to delete an image # object and free memory associated with it. Just using a lexical # variable for storing the image object and letting the variable # to go out of scope or setting to undef is not sufficient. # # $image->delete # # Deletes the image $image and returns an empty string. If there # are instances of the image displayed in widgets, the image won't # actually be deleted until all of the instances are released. # However, the association between the instances and the image # manager will be dropped. Existing instances will retain their # sizes but redisplay as empty areas. If a deleted image is # recreated (with the same name) the existing instances will # use the new image. # $image->delete; # Image area blank MainLoop; # $image->delete; # Segmentation fault: 11 }