我正在编写一个使用一些ImageMagick命令的perl脚本,特别是识别。相关代码在这里:
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
当我运行整个脚本时,它会挂在这些行上,这就是输出:
identify -format %h xxxx.jpg
首先,问题与ImageMagick没有正确的格式委托来处理jpg图像有关,但在修复之后,我仍然遇到此错误。我无法找到与" error / blob.c / OpenBlob / 3323"相关的任何错误文档。在编写了一些测试代码以查看问题可能是什么之后,我想我已经确定它与perl将参数传递给系统命令的方式有关,因为当我写入系统命令print "$curPic\n
时终端,它工作得很好。我还注意到,当我opendir DIR, $folder or die "Cannot open directory: $!";
my @files = readdir(DIR);
closedir(DIR);
时,在打印期间会在文件名前添加256。我不知道为什么会这样。
供参考,以下是我收集文件名的方法:
#!/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";
}
以下是完整的脚本:
#!/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";
}
编辑: 我切换到推荐的Image :: Size库,所以这是我更新的脚本。它工作了一段时间,给了我想要的输出,但突然中断并说变量是未初始化的。 &#34;使用未初始化的变量......&#34; $ height和$ width有一个错误,但是它们都是在大约20次成功迭代后发生的。如果我连续多次运行脚本,它似乎会有所不同。
@Configuration
@EnableContextInstanceData
public static class ApplicationConfiguration {
}
答案 0 :(得分:3)
如消息所示,您将路径传递给不存在的文件。而不是传递
if0W211.jpg
你应该通过
/media/sf_Pictures_from_Reddit/if0W211.jpg
这仍然会给您带来以下问题:
所有这些都可以通过使用Image::Size来修复。
但如果你坚持使用identify
,
use IPC::System::Simple qw( capturex );
my $dimensions = eval { capturex("identify", "-format", "%h,%w", "--", "$folder/$curPic") }
or do {
warn("Can't determine the dimensions of \"$folder/$curPic\": $@");
next;
};
my ($height, $width) = $dimensions =~ /^(\d+),(\d+)$/
or do {
warn("Can't determine the dimensions of \"$folder/$curPic\": Unexpected output from \"identify\": $dimensions\n");
next;
};
如果您的identify
不支持--
(或者即使它支持),您可以替换
"--", "$folder/$curPic"
与
"$folder/$curPic" =~ s{^-}{./-}r
答案 1 :(得分:1)
目前,您正在为每个图像创建两个新进程(一个用于标识宽度,另一个用于标识高度),因此如果您有大量图像,则可能会对系统造成相当大的负担。
作为替代方案,您只需调用一个identify
进程并将其传递给所有图像名称,如下所示:
identify -format "%w:%h:%f\n" *jpg
示例输出
2000:1200:ok.jpg
1200:2000:toolow2.jpg
1000:500:vert.jpg
然后,您可以使用bash
或Perl
#!/bin/bash
VERT="/tmp"
TOOLOW="/tmp"
identify -format "%w:%h:%f\n" *jpg |
while IFS=: read w h name; do
echo "DEBUG: $name, $w, $h"
[[ $h -gt $(( (8*w)/10 )) ]] && { mv "$name" "$VERT"; >&2 echo "Too tall: $name"; continue; }
[[ ($h -lt 1080) || ($w -lt 1920) ]] && { mv "$name" "$TOOLOW"; >&2 echo "Low res: $name"; continue; }
echo "$name approved"
done
答案 2 :(得分:0)
这是适用于我的目的的最终脚本。我添加了定义检查和非零检查,以确保变量在前进之前接收到正确的输入。
#!/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") or die "Couldn't get the image dimensions for $curPic: $!";
if($curPic eq "." || $curPic eq ".."){ next;}
if((defined $height) & (defined $width)){
if(($height != 0) & ($width != 0)) {
print "File: $curPic\nHeight: $height\nWidth: $width\n";
#sleep(0.5);
#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 $curPic: $!";
print "$curPic was not approved because of its orientation.\n\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 $curPic: $!";
print "$curPic was not approved because of its resolution.\n\n";
next;
}
print "$curPic is approved as a desktop background.\n\n";
$height = 0;
$width = 0;
}
else{
print "Variables failed to initialize.\n\n";
}
}
else{
print "Variables are undefined.\n\n";
}
}