向PNG图片添加和检索一些元数据

3
我需要向许多图像添加一些元数据。例如,我需要将右眼和左眼的位置添加到元数据中。 就像这样:righteye(291,493), lefteye(453,491)。我现在正在处理png文件。下面是使用gimp的示例。 enter image description here 有没有好的方法将这些信息添加到图像元数据中?我看过png文件中的关键字和字符串。那是解决我的问题的方法吗?另外,我需要一个工具来编辑元数据。我有很多人脸图像,我需要一个工具来为每个图像添加这些元数据。而且我还需要以编程方式从每个图像中检索这些数据。请建议一个适当的方法来解决所有这些任务。

你应该能够使用 ImageMagickexiv2 读取和写入 PNG 元数据。这些参考资料可能会有所帮助... http://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_PNG_files 和 http://blog.client9.com/2007/08/19/editing-png-metadata-from-command-line.html - Mark Setchell
关闭?为什么?请先阅读http://stackoverflow.com/help/on-topic...这是程序员常用的软件工具;并且是一个实际可回答的问题,与软件开发有关。 - Neeraj
工具建议不在话题范围之内。此外,除此之外,你的问题过于宽泛。 - Jongware
2个回答

6

看起来你可以使用exiv2或者ImageMagick两种工具在PNG图片中设置注释,这两个工具都有命令行版本和C++库绑定。

因此,如果你运行:

# Set image description using "exiv2"
exiv2 -M"add Exif.Image.ImageDescription Ascii 'left eye (200,201) right eye(202,203)'" image.png

# Set image comment usimg "ImageMagick"
convert -comment "left eye(76,77) right eye(78,79)" image.png image.png

你现在可以使用 exiftool 查看结果。

exiftool image.png

ExifTool Version Number         : 10.00
File Name                       : image.png
Directory                       : .
File Size                       : 496 bytes
File Modification Date/Time     : 2015:09:20 20:45:29+01:00
File Access Date/Time           : 2015:09:20 20:49:22+01:00
File Inode Change Date/Time     : 2015:09:20 20:45:29+01:00
File Permissions                : rw-r--r--
File Type                       : PNG
File Type Extension             : png
MIME Type                       : image/png
Image Width                     : 1
Image Height                    : 1
Bit Depth                       : 1
Color Type                      : Grayscale
Compression                     : Deflate/Inflate
Filter                          : Adaptive
Interlace                       : Noninterlaced
White Point X                   : 0.3127
White Point Y                   : 0.329
Red X                           : 0.64
Red Y                           : 0.33
Green X                         : 0.3
Green Y                         : 0.6
Blue X                          : 0.15
Blue Y                          : 0.06
Background Color                : 1
Modify Date                     : 2015:09:20 20:45:29
Exif Byte Order                 : Little-endian (Intel, II)
Image Description               : left eye (200,201) right eye(202,203)    <--- HERE
Comment                         : left eye(76,77) right eye(78,79)         <--- HERE
Datecreate                      : 2015-09-20T20:45:29+01:00
Datemodify                      : 2015-09-20T20:45:29+01:00
Exif Image Description          : left eye (200,201) right eye(202,203)
Image Size                      : 1x1
Megapixels                      : 0.000001

或者使用ImageMagick的identify命令查看结果:
identify -verbose image.png

  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 1x1+0+0
  Units: Undefined
  Type: Bilevel
  Base type: Bilevel
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8/1-bit
  Channel depth:
    gray: 1-bit
  Channel statistics:
    Pixels: 1
    Gray:
      min: 0 (0)
      max: 0 (0)
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
      entropy: nan
  Colors: 1
  Histogram:
         1: (  0,  0,  0) #000000 gray(0)
  Colormap entries: 2
  Colormap:
         0: (  0,  0,  0) #000000 gray(0)
         1: (255,255,255) #FFFFFF gray(255)
  Rendering intent: Undefined
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: gray(255)
  Border color: gray(223)
  Matte color: gray(189)
  Transparent color: gray(0)
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 1x1+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    comment: left eye(76,77) right eye(78,79)                      <--- HERE
    date:create: 2015-09-20T20:45:29+01:00
    date:modify: 2015-09-20T20:45:29+01:00
    exif:ImageDescription: left eye (200,201) right eye(202,203)   <--- HERE
    png:bKGD: chunk was found (see Background color, above)
    png:cHRM: chunk was found (see Chromaticity, above)
    png:IHDR.bit-depth-orig: 1
    png:IHDR.bit_depth: 1
    png:IHDR.color-type-orig: 0
    png:IHDR.color_type: 0 (Grayscale)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 1, 1
    png:text: 5 tEXt/zTXt/iTXt chunks were found
    png:text-encoded profiles: 1 were found
    png:tIME: 2015-09-20T20:45:29Z
    signature: 709e80c88487a2411e1ee4dfb9f22a861492d20c4765150c0c794abd70f8147c
  Profiles:
    Profile-exif: 70 bytes
  Artifacts:
    filename: image.png
    verbose: true
  Tainted: False
  Filesize: 496B
  Number pixels: 1
  Pixels per second: 1PB
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.9.1-10 Q16 x86_64 2015-09-08 http://www.imagemagick.org

或者使用:

exiftool -s -Comment  image.png
Comment                         : left eye(76,77) right eye(78,79)

或者使用exiftool来设置注释,然后使用ImageMagick以以下两种方式之一读取:

exiftool -comment="Crazy Comment"  image.png

identify -verbose image.png | grep Crazy
comment: Crazy Comment

identify -format "%[c]" image.png
Crazy Comment

2

虽然这是一个老问题,但对于有同样需求的人来说,现在有一个 LibPNG++ 的分支可以轻松地向 PNG 图像添加文本元数据功能:https://github.com/Armchair-Software/pngpp

  png::image<png::rgba_pixel> output_png(image_size.x, image_size.y);
  
  // construct your PNG as normal here, this is just a basic example:
  for(png::uint_32 y = 0; y != 255; ++y) {
    for(png::uint_32 x = 0; x != 255; ++x) {
      output_png[y][x] = png::rgba_pixel(x, 0, y, 96);
    }
  }
  
  auto png_comments{output_png.get_text()};                
  std::initializer_list<std::pair<std::string, std::string>> comments {
    {"Title", "My beautiful PNG"},
    {"Author", "Me"},
    {"Copyright", "Copyright Me 2023"},
  };
  for(auto const &[key, text] : comments) {
    png_comments.emplace_back(key.c_str(), text.c_str());
  }
  output_png.set_text(png_comments);
  
  output_png.write("my_filename.png")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接