简体   繁体   English

使用Perl将数据添加到IPTC字段

[英]Adding data to IPTC field using Perl

I want to set custom text to the IPTC field "Special instructions" in Perl. 我想在Perl中将自定义文本设置为IPTC字段“特殊说明”。

How can this be done without the usage of a modul? 不使用模块怎么办?

Updated Again 再次更新

Ok, in the light of your new requirement to actually read, modify and then re-write the IPTC information, you could use the following to read the IPTC information until we find something better: 好的,根据您实际读取,修改然后重新写入IPTC信息的新要求,您可以使用以下内容读取IPTC信息,直到发现更好的东西为止:

print $image->Identify();

which gives this: 这给出了:

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

Mmm... it appears that that information gets written to stdout , and I don't know how to capture it. 嗯...似乎信息已写入stdout ,我不知道如何捕获它。 So I have investigated further and can get the IPTC information like this too: 因此,我进行了进一步调查,并可以得到如下的IPTC信息:

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

which gives this: 这给出了:

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

So it looks like individual IPTC fields are separated by: 因此,看来各个IPTC字段由以下分隔:

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

So, you can read them and create your IPTC.txt file like this: 因此,您可以阅读它们并创建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";
}

With my test file I get the following output from this: 使用我的测试文件,我从中得到以下输出:

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

Then you can set IPTC data using the Perl API, as follows using the file IPTC.txt further down: 然后,您可以使用Perl API设置IPTC数据,如下所示,使用文件IPTC.txt进一步向下:

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

The following is not a sensible, complete program in itself but it shows how to use the techniques I am suggesting: 以下内容本身并不是一个明智的完整程序,但它显示了如何使用我建议的技术:

#!/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

Updated 更新

I have made a little progress... I can read the IPTC attributes from an image using the Perl API. 我取得了一些进展...我可以使用Perl API从图像中读取IPTC属性。 For example, the following will read the IPTC Credit: 例如,以下内容将读取IPTC信用:

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

Original Answer 原始答案

I am working on this, but the following may be enough to get you started anyway before I finish! 我正在努力,但是以下内容可能足以让您在我完成之前开始!

If I create a file like this, and call it IPTC.txt 如果我创建这样的文件,并将其IPTC.txt

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

and then use ImageMagick convert like this: 然后像这样使用ImageMagick convert

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

I can insert the IPTC information. 我可以插入IPTC信息。 I can then test this using jhead as follows: 然后,我可以使用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

I know you don't want to shell out, but this will hopefully get us started on how to do it using the CPAN module you have. 我知道您不想花钱,但这有望使我们开始使用如何使用CPAN模块进行操作。 Which one do you have, by the way? 顺便问一下,您有哪一个?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM