forked from mrhdias/perl6-Imlib2
/
export_image.p6
executable file
·41 lines (33 loc) · 1.01 KB
/
export_image.p6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use Imlib2;
if @*ARGS.elems != 2 {
say "Error: wrong number of arguments!";
say "Usage: ./export_image.p6 image format\n";
say "Format is a string and may be \"jpeg\", \"tiff\", \"png\", etc.";
say "The exact number of formats supported depends on how you built imlib2.\n";
say "Ex: ./export_image.p6 images/camelia-logo.jpg png";
exit();
}
my $im = Imlib2.new();
# load an image
my $loadedimage = $im.load_image(@*ARGS[0]);
unless $loadedimage {
say "Error: Problem loading file...";
exit();
}
# Sets the current image Imlib2 will be using with its function calls.
$loadedimage.context_set();
@*ARGS[0] ~~ s/\.\w+$/\.@*ARGS[1]/;
$im.image_set_format(@*ARGS[1]);
if @*ARGS[0].IO ~~ :e {
my $option = prompt("The " ~ @*ARGS[0] ~ " file already exists - do you want to replace the existing file? [y/n]: ");
if $option eq "y" {
unlink(@*ARGS[0])
} else {
$im.free_image();
exit();
}
}
$im.save_image(@*ARGS[0]);
# Frees the image that is set as the current image in Imlib2's context.
$im.free_image();
exit();