我想在 Perl 中为 IPTC 字段“特殊说明”设置自定义文本。
如果不使用模块,如何做到这一点?
再次更新
好的,鉴于您对实际读取、修改和重写 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"
然后convert
像这样使用 ImageMagick:
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 模块进行操作。顺便问一下,你有哪一个?