[英]Adding data to IPTC field using Perl
我想在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=$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
,如下所示:
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模块进行操作。 顺便问一下,您有哪一个?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.