Search code examples
imageimage-processingimagemagickimagemagick-convert

Imagemagick adding and reading comment


I am trying to add a comment to an image using imagemagick and read it back For adding a comment i can do

convert -comment "processed" original.jpg converted.jpg

Now the output of identify -verbose converted.jpeg

Properties:
comment: processed
date:create: 2017-05-17T12:22:36+06:00
date:modify: 2017-05-17T12:22:36+06:00
exif:ColorSpace: 1
exif:DateTime: 2017:02:04 12:21:36
exif:ExifImageLength: 1273
exif:ExifImageWidth: 900
exif:ExifOffset: 164
exif:Orientation: 1
exif:ResolutionUnit: 2
exif:Software: Adobe Photoshop CS5 Windows
exif:thumbnail:Compression: 6
exif:thumbnail:JPEGInterchangeFormat: 302
exif:thumbnail:JPEGInterchangeFormatLength: 3805
exif:thumbnail:ResolutionUnit: 2
exif:thumbnail:XResolution: 300/1
exif:thumbnail:YResolution: 300/1

Is there a cleaner way to read back the comments other than

identify -verbose converted.jpg | grep 'comment'

Solution

  • You can set the comment like this:

    convert YourImage.jpg -set comment "fred" YourImage.jpg
    

    or, it's probably better to use mogrify to avoid repeating yourself in this instance:

    mogrify -set comment "bill" YourImage.jpg
    

    And that technique allows you to do multiple images at once:

    mogrify -set comment "bill" *.jpg
    

    You can extract just the comment like this:

    identify -format %c YourImage.jpg 
    fred
    

    im4java - I have no knowledge of im4java, but can only guess (based on this) that you would do:

     Info imageInfo = new Info(filename,true);
     System.out.println("Format: " + imageInfo.getImageComment());
    

    Alternatively, maybe you can build your own ConvertCmdknowing that the following convert works at the command line if you don't know how to make an identify command in im4java:

    convert -format %c YourImage.jpg info: