11 GetOptions('delay|d=i', \$delay,
12 'frames|f=i', \$frames,
13 'lowpct|p=i', \$low_pct,
15 'verbose|v' => \$verbose);
17 my $back_color = Imager::Color->new($back)
18 or die "Cannot convert $back to a color: ", Imager->errstr, "\n";
20 $low_pct >= 0 && $low_pct < 100
21 or die "lowpct must be >=0 and < 100\n";
23 $delay > 0 and $delay < 255
24 or die "delay must be between 1 and 255\n";
27 or die "frames must be > 1\n";
35 my $base = Imager->new;
36 $base->read(file => $in_name)
37 or die "Cannot read image file $in_name: ", $base->errstr, "\n";
39 # convert to RGBA to simplify the convert() matrix
40 $base = $base->convert(preset => 'rgb') unless $base->getchannels >=3;
41 $base = $base->convert(preset => 'addalpha') unless $base->getchannels == 4;
43 my $width = $base->getwidth;
44 my $height = $base->getheight;
47 my $down_frames = $frames / 2;
48 my $step = (100 - $low_pct) / $down_frames;
49 my $percent = 100 - $step;
51 print "Generating frames\n" if $verbose;
52 for my $frame_no (1 .. $down_frames) {
53 print "\rFrame $frame_no/$down_frames";
55 # canvas with our background color
56 my $canvas = Imager->new(xsize => $width, ysize => $height);
57 $canvas->box(filled => 1, color => $back_color);
59 # make a version of our original with the alpha scaled
60 my $scale = $percent / 100.0;
61 my $draw = $base->convert(matrix => [ [ 1, 0, 0, 0 ],
64 [ 0, 0, 0, $scale ] ]);
66 # draw it on the canvas
67 $canvas->rubthrough(src => $draw);
72 print "\n" if $verbose;
74 # generate a sequence going from the original down to the most faded
77 # remove the most faded frame so it isn't repeated
80 push @frames, reverse @down;
82 print "Writing frames\n" if $verbose;
83 Imager->write_multi({ file => $out_name,
85 gif_loop => 0, # loop forever
87 translate => 'errdiff',
88 make_colors => 'mediancut',
91 or die "Cannot write $out_name: ", Imager->errstr, "\n";
95 Produce an animated gif that cycles an image fading into a background and
96 unfading back to the original image.
97 Usage: $0 [options] input output
98 Input can be any image supported by Imager.
99 Output should be a .gif file.
103 -d <delay> | --delay <delay>
104 Delay between frames in 1/100 sec. Default 10.
105 -p <percent> | --percent <percent>
106 Low percentage coverage. Default: 30
107 -b <color> | --back <color>
108 Color to fade towards, in some format Imager understands.
110 -f <frames> | --frames <frames>
111 Rough total number of frames to produce. Default: 20.
117 flasher.pl - produces a slowly flashing GIF based on an input image
121 perl flasher.pl [options] input output.gif
125 flasher.pl generates an animation from the given image to I<lowpct>%
126 coverage on a blank image of color I<back>.
134 -f I<frames>, --frames I<frames> - the total number of frames. This is
135 always rounded up to the next even number. Default: 20
139 -d I<delay>, --delay I<delay> - the delay in 1/100 second between
144 -p I<percent>, --lowpct I<percent> - the lowest coverage of the image.
149 -b I<color>, --back I<color> - the background color to fade to.
154 -v, --verbose - produce progress information.
162 Tony Cook <tonyc@cpan.org>