[英]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.