我正在编写一个使用一些 ImageMagick 命令的 perl 脚本,特别是识别。相关代码在这里:
my $height = system("identify -format %h $curPic");
my $width = system("identify -format %w $curPic");
当我运行整个脚本时,它会挂在这些行上,这是输出:
identify: unable to open image 'if0W211.jpg': No such file or directory @ error/blob.c/OpenBlob/3323
identify: unable to open image 'if0W211.jpg': No such file or directory @ error/blob.c/OpenBlob/3323
起初,问题与 ImageMagick 没有正确的格式代表来处理 jpg 图像有关,但在修复该问题后,我仍然收到此错误。我找不到任何与“error/blob.c/OpenBlob/3323”相关的错误文档。在编写了一些测试代码以查看问题可能是什么之后,我想我已经确定它与 perl 将参数传递给系统命令的方式有关,因为当我identify -format %h xxxx.jpg
在终端中编写该系统命令时,它工作得很好. 我还注意到,当 I 时print "$curPic\n
,在打印过程中文件名前面会附加一个 256。我不知道为什么会这样。
作为参考,这是我收集文件名的方式:
opendir DIR, $folder or die "Cannot open directory: $!";
my @files = readdir(DIR);
closedir(DIR);
这是完整的脚本:
#!/usr/bin/perl -w
use strict;
use diagnostics;
use File::Copy;
my $folder = "/media/sf_Pictures_from_Reddit";
my $oriFolder = "/media/sf_WrongOrientation";
my $resFolder = "/media/sf_LowRes";
#Collect all the files
opendir DIR, $folder or die "Cannot open directory: $!";
my @files = readdir(DIR);
closedir(DIR);
#Iterate through each file and check its orientation and resolution
foreach my $curPic (@files) {
my $height = system("identify -format %h $curPic");
my $width = system("identify -format %w $curPic");
#move those that are vertically oriented to a different folder
if ($height >= ($width*0.8)) {
move($curPic, $oriFolder//$curPic) or die "The ori move operation failed for image $curPic: $!";
print "$curPic was not approved because of its orientation.";
next;
}
#move those that are low res to a third folder
elsif (($height < 1080) | ($width < 1920)) {
move($curPic, $resFolder//$curPic) or die "The res move operation failed for image $curPic: $!";
print "$curPic was not approved because of its resolution.";
next;
}
print "$curPic is approved as a desktop background";
}
编辑:我正在切换到推荐的 Image::Size 库,所以这是我更新的脚本。它工作了一段时间,给了我想要的输出,但突然中断并说变量未初始化。“使用未初始化的变量...” $height 和 $width 有一个错误,但它们再次发生在大约 20 次成功迭代之后。如果我多次背靠背运行脚本,它似乎会有所不同。
#!/usr/bin/perl -w
use strict;
use diagnostics;
use File::Copy;
use Image::Size;
my $folder = "/media/sf_Pictures_from_Reddit";
my $oriFolder = "/media/sf_WrongOrientation";
my $resFolder = "/media/sf_LowRes";
my $height = 0;
my $width = 0;
#Collect all the files
opendir DIR, $folder or die "Cannot open directory: $!";
my @files = readdir(DIR);
closedir(DIR);
#Iterate through each file and check its orientation and resolution
foreach my $curPic (@files) {
($width, $height) = imgsize("$folder/$curPic");
#move those that are vertically oriented to a different folder
if ($height >= ($width*0.8)) {
move("$folder/$curPic", "$oriFolder/$curPic") or die "The ori move operation failed for image $curPic: $!";
print "$curPic was not approved because of its orientation.\n";
next;
}
#move those that are low res to a third folder
elsif (($height < 1080) | ($width < 1920)) {
move("$folder/$curPic", "$resFolder/$curPic") or die "The res move operation failed for image $curPic: $!";
print "$curPic was not approved because of its resolution.\n";
next;
}
print "$curPic is approved as a desktop background.\n";
}