[英]Using Image::Magick to split an image into smaller chunks and get relative position of each chunk
我需要將大圖片(最大16384x16384px)拆分為較小的塊,通常為512x512。 為此,我使用以下簡單的perl腳本:
#!/usr/bin/perl
use warnings;
use strict;
my $size = '512x512';
unless ($ARGV[0]) { die "Missing filename as arg" }
unless (-e $ARGV[0]) { die "$ARGV[0] not found.\n" }
my ($newfile, undef) = split(/\./,$ARGV[0]);
system("convert $ARGV[0] -crop $size $newfile" . "_%03d.png");
現在,我需要構建一個較小的索引,其中包括原始圖像中的位置偏移,例如在1024x1024源圖像的情況下:
image_000.png 0,0
image_001.png 512,0
image_002.png 0,512
image_003.png 512,512
...或類似的東西。 基本上,只是為了跟蹤每個特定塊在原始圖像中的位置。
自從將convert
命令傳遞給system()
以來,我一直在考慮對文件進行全局處理並對其進行排序,因為一些快速實驗表明,較小的塊按從左到右,從上到下(直觀)的順序編號並進行構建基於此的索引,但是我認為必須有一些更簡單的方法,包括Perls自己的ImageMagick接口
所以我的問題是:
1.什么是Image::Magick
等效於convert $ARGV[0] -crop $size $newfile" . "_%03d.png
?
2.一旦設法避免了system()調用,是否可以基於$ image對象中的數據構建索引?
PS:
$size
提取出來……實際上,我根本不知道為什么我選擇使用該變量。 identify
命令的第三列? 我用我知道的唯一方法解決了它:擰上perlmagic,然后通過命令行代替。 以下腳本適用於矩形和正方形圖像。 索引打印到屏幕上。
use warnings;
use strict;
my $size = '512x512';
my $offset = 512;
unless ($ARGV[0]) { die "Missing filename as arg" }
unless (-e $ARGV[0]) { die "$ARGV[0] not found.\n" }
my ($newfile, undef) = split(/\./,$ARGV[0]);
system("convert $ARGV[0] -crop $size $newfile" . "_%03d.png");
my @files = glob($newfile . "_*");
@files = sort(@files);
my (undef, undef, $origsize) = split(/\s+/,`identify $ARGV[0]`);
my ($maxX, $maxY) = split(/x/,$origsize);
$maxX /= $offset;
$maxY /= $offset;
my $x = 0;
my $y = 0;
foreach my $file (@files)
{
print "$file\t" . $x * $offset . "\t" . $y * $offset . "\n";
$x++;
if ($x >= $maxX)
{
$x = 0;
$y++;
}
}
__END__
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.