0

我正在编写一个使用一些 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";
}
4

3 回答 3

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
于 2017-12-18T21:55:40.813 回答
1

就目前而言,您正在为每个图像创建两个新进程(一个用于识别宽度,另一个用于识别高度),因此如果您有大量图像,这可能会给您的系统带来相当大的负载。

作为替代方案,您可以只调用一个identify进程并将所有图像名称传递给它,如下所示:

identify -format "%w:%h:%f\n" *jpg

样本输出

2000:1200:ok.jpg
1200:2000:toolow2.jpg
1000:500:vert.jpg

然后,您可以使用bashor解析它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
于 2017-12-20T12:58:27.710 回答
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";
    }
}
于 2017-12-21T01:04:17.060 回答