-
Notifications
You must be signed in to change notification settings - Fork 3
/
doconversion.pl
executable file
·88 lines (69 loc) · 2.19 KB
/
doconversion.pl
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/perl
use strict;
use warnings;
use Image::Magick;
# called as
# ./doconversion.pl ppm jpg
my ($src, $dest) = @ARGV;
#
open(my $file, '<', "annotation.csv");
while(my $line = <$file>){
chomp $line;
next if !$line;
my ($id, $name, $mask, $cat, $sizes, $default_size, $overlay) = split /,/, $line;
# convert file
my $infile;
if($id =~ m/^\d\d\d$/){
$infile = "$src/marker-$id.ppm";
}else{
$infile = "$src/$id.png";
$id =~ s/ /_/g;
}
# Skip files that are not in this directory
if(-f $infile){
my @label = ("marker", $id);
push @label, $overlay if $overlay;
my $label = join("-", @label);
my $outfile = "$dest/$label.$dest";
my $thumbfile = "thumb/$label.$dest";
my $img = new Image::Magick;
$img->Set(size => '450x450');
my $status = $img->Read($infile);
$img->Set(density => "150");
$img->Set(units => "PixelsPerInch");
$img->Set(background => "white");
$img = $img->Flatten();
$img->Set(alpha => 'Off');
# apply mask
if($mask){
print "Masking $id based on $mask\n";
my $maskfile = "$src/marker-$mask.ppm";
my $mask = new Image::Magick;
$mask->Read($maskfile);
my $base = $mask->Clone();
$base->Negate();
$base->Composite(image => $img, mask => $mask);
$img = $base;
}
# trim whitespace
$img->Set(fuzz => "5%");
$img->Trim();
my ($xres, $yres) = $img->Get("columns", "rows");
# apply overlay
if($overlay){
print "Overlaying $label with $overlay\n";
my $overlayfile = "overlay/$overlay.png";
my $overlay = new Image::Magick;
$overlay->Read($overlayfile);
$overlay->Resize(width => $xres, height => $yres);
$img->Composite(image => $overlay);
}
# write out the image
$img->Write($outfile);
# generate thumbnail
my $width = 30;
my $height = $yres * $width / $xres;
$img->Resize(width => $width, height => $height);
$img->Write($thumbfile);
}
}