Search code examples
bashexifexiv2

How to acces exiv2 ImageSize Property?


I create images from blender, and they lack Exif metadata, which I add using the bash command exiv2. (Because the software these images are passed to will use the metadata.)

For example, I can set the Image Width to 960 using exiv2 -M"set Exif.Image.ImageWidth 960" image.jpg and then read it out using exiv2 -g Exif.Image.ImageWidth -Pv image.jpg.

For a quick summary, I can do exiv2 image.jpg to get a list of set Exif metadata. This includes

$ exiv2 image.jpg
File name       : image.jpg
File size       : 32975 Bytes
MIME type       : image/jpeg
Image size      : 960 x 540

How can I use this Image size to set Exif.Image.ImageWidth and Exif.Image.ImageLength in bash?

The standard Exif tags don't list ImageSize.


Solution

  • As ghoti suggested, parsing the output works:

    exiv2 image.jpg  | grep  "Image size" | sed -n "s/^.*Image size\s*:\s\([0-9]*\)\sx\s\([0-9]*\).*$/\1/p"
    

    gives the width, and

    exiv2 lowerCircle_0100.jpg  | grep  "Image size" | sed -n "s/^.*Image size\s*:\s\([0-9]*\)\sx\s\([0-9]*\).*$/\2/p"
    

    the height.

    I'm still hoping for a cleaner answer though.

    Explanation of the sed command:

     sed -n "s/^.*Image size\s*:\s\([0-9].*\)\sx\s\([0-9]*\).*$/\1/p"
    
     -n            suppress printing
     "s/           substitute match
     ^.*           everything from the start
     Image size    until "Image size" is encountered
     \s*:\s        whitespace, colon, a single space
     ([0-9]*\)    any number of digits. store that in \1
     \sx\s         space x space
     ([0-9]*\)    another group of digits. store that in \2
     .*$           everything until the end of line
     /\1           substitute all that with the first group
     /p"           and print the substituted result