Why the output image will be rotated?

前端 未结 1 527
花落未央
花落未央 2020-12-12 02:51

I build a watermark creator by OpenCV. The program could create watermark in the 4 corner of a source image. I haven\'t change the source image direction and the output imag

相关标签:
1条回答
  • 2020-12-12 03:18

    As @MarkRansom says, some cameras store an EXIF "Orientation" field in the headers of images.

    You have a few options, using exiftool, jhead or ImageMagick all of which are installed on many Linux distros and are available for macOS and Windows.

    So, to see the field with exiftool:

    exiftool -Orientation image.jpg
       Orientation                     : Horizontal (normal)
    

    Or with jhead:

    jhead -v image.jpg | grep -i orien
    Orientation = 1
    

    Or with ImageMagick:

    identify -verbose image.jpg | grep -i orient
       Orientation: TopLeft
       exif:Orientation: 1
    

    So, you could do that, and correct for it with OpenCV using a rotate(). Or you could correct it with ImageMagick outside of OpenCV:

    mogrify -auto-orient image.jpg             # correct orientation of single image
    convert image.jpg -auto-orient result.jpg  # alternative, more verbose but same as above  
    mogrify -auto-orient *.jpg                 # correct orientation of all images
    

    Beware that mogrify will change all your images irreversibly, so be sure that you make a backup before testing.


    Note that jhead is probably the easiest to install, lightest weight option of the three in Windows. In Linux or macOS, exiftool is the lightest weight as it is just a Perl script. ImageMagick is vastly more powerful, and heavier weight and somewhat harder to install - IMHO.


    Useful links

    Tutorial

    Wikipedia

    0 讨论(0)
提交回复
热议问题