【问题标题】:Adding data to IPTC field using Perl使用 Perl 将数据添加到 IPTC 字段
【发布时间】:2014-11-17 20:48:18
【问题描述】:

我想在 Perl 的 IPTC 字段“特殊说明”中设置自定义文本。

如何在不使用模块的情况下做到这一点?

【问题讨论】:

  • 你可以用 ImageMagick 或 exiv2 来做,如果你不介意向他们掏钱 - 但我猜你不是这个意思?
  • 不,不需要炮击。使用 ImageMagick perl API 很好(因为这是我们默认安装的 cpan 包)
  • @asp: thx,但我无法安装除我安装的那些之外的其他 perl 模块。我的问题是我不想使用 perl 模块

标签: image perl image-processing iptc perlmagick


【解决方案1】:

再次更新

好的,鉴于您对实际读取、修改和重写 IPTC 信息的新要求,您可以使用以下方式读取 IPTC 信息,直到我们找到更好的内容:

print $image->Identify();

这给出了这个:

stuff ..
...
Profiles:
  Profile-8bim: 44 bytes
  Profile-iptc: 32 bytes
    Special Instructions[2,40]: Handle with care.
    Credit[2,110]: Mark
...
...

嗯...看来该信息已写入stdout,我不知道如何捕获它。所以我进一步调查,也可以得到这样的IPTC信息:

$profile=$image->Get('IPTC');

这给出了这个:

0000000      021c    0028    4811    6e61    6c64    2065    6977    6874
         034 002   (  \0 021   H   a   n   d   l   e       w   i   t   h
0000020      6320    7261    2e65    021c    006e    4d04    7261    006b
               c   a   r   e   . 034 002   n  \0 004   M   a   r   k  \0

所以看起来各个 IPTC 字段由以下分隔:

1c - a single byte marker
byte - IPTC page
byte - IPTC field number
2 bytes - length of following field
<FIELD> - the actual data

因此,您可以像这样阅读它们并创建您的 IPTC.txt 文件:

#!/usr/bin/perl
use strict;
use warnings;
use Image::Magick;

my ($image,$x,$profile,$id);
$image=Image::Magick->new(size=>'256x128');
$image->ReadImage('out.jpg');

$profile=$image->Get('IPTC');
my @items=split /\x1c/,$profile;
shift @items; # Discard emptiness before first separator
foreach (@items) {
   my $page=ord(substr($_,0,1));
   my $field=ord(substr($_,1,1));
   my $value=substr($_,4); # rest
   print "$page#$field=\"$value\"\n";
}

通过我的测试文件,我得到以下输出:

2#110="CREDITCREDITCREDITCREDIT"
2#5="OBJECT"
2#115="SOURCE"
2#116="COPYRIGHT"
2#118="CONTACT"
2#120="CAPTION"

然后可以使用 Perl API 设置 IPTC 数据,如下使用文件IPTC.txt 再往下:

$image->Mogrify("profile",'8BIMTEXT:IPTC.txt');

以下内容本身并不是一个明智、完整的程序,但它展示了如何使用我建议的技术:

#!/usr/bin/perl
use strict;
use warnings;
use Image::Magick;

my ($image,$x,$profile);
$image=Image::Magick->new(size=>'256x128');
$image->ReadImage('out.jpg');
print $image->Identify();                          # Get IPTC info - to screen but you can put it in a variable obviously
$image->Mogrify("profile",'8BIMTEXT:IPTC.txt');    # Write IPTC info
$image->Write('out.jpg');                          # Output image with new IPTC info

更新

我取得了一些进展...我可以使用 Perl API 从图像中读取 IPTC 属性。例如,以下内容将读取 IPTC Credit:

$credit=$image->Get('IPTC:2:110');

原答案

我正在努力,但以下内容可能足以让您在我完成之前开始!

如果我创建一个这样的文件,并将其命名为IPTC.txt

2#40#Special Instructions="Handle with care."
2#110#Credit="Mark"

然后像这样使用 ImageMagick convert

convert out.jpg -profile 8BIMTEXT:IPTC.txt out.jpg

我可以插入 IPTC 信息。然后我可以使用jhead 进行测试,如下所示:

jhead out.jpg
File name    : out.jpg
File size    : 18899 bytes
File date    : 2014:09:24 11:41:23
Resolution   : 1024 x 768
Color/bw     : Black and white
JPEG Quality : 86
======= IPTC data: =======
Spec. Instr.  : Handle with care.
Credit        : Mark

我知道您不想花钱,但这有望让我们开始了解如何使用您拥有的 CPAN 模块进行操作。顺便问一下,你有哪一个?

【讨论】:

  • 感谢您到目前为止的尝试。我们安装了模块 Image::Magick (imagemagick.org/script/perl-magick.php)
  • 读取配置文件并修改文本文件后应该可以设置配置文件
  • 我不明白。我的回答已经显示了如何设置 IPTC 数据。
  • 没错,仍然需要做的是从图像文件中已经设置的值创建一个 iptc.txt 文件 - 否则它们将被删除(我们只想覆盖一个单独的 IPTC 字段)
  • 啊哈!我明白了 - 你的问题没有提到那一点。我会在这方面做更多的工作......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多