Skip to content

Instantly share code, notes, and snippets.

@twobob
Created May 3, 2017 14:32
Show Gist options
  • Save twobob/32c7d3015c42d9ffb1328ef2f504592a to your computer and use it in GitHub Desktop.
Save twobob/32c7d3015c42d9ffb1328ef2f504592a to your computer and use it in GitHub Desktop.
pyhon snippet to check if a file is landscape or portrait with ImageMagick
import os, subprocess
subprocess.run(['Magick', './IMG_3160.jpg' ,'-identify' , '-format' ,'"%[fx:(w/h>1)?1:0]"','info:'], stdout=subprocess.PIPE).stdout.decode('utf-8')[-2]
@twobob
Copy link
Author

twobob commented May 3, 2017

0 = portrait
1= landscape

@fredmo
Copy link

fredmo commented Dec 30, 2022

This doesn't work on iPhone landscape picture, the W is higher than the H on Linux

Please find the solution :

ORIENTATION=$(identify -format '%[EXIF:*]' $FILE | grep Orientation | cut -d= -f2)
echo $1 $FILE $IS_LANSCAPE $ORIENTATION
if [ $ORIENTATION ]; then 
  #https://sirv.com/help/articles/rotate-photos-to-be-upright/
  #The 8 EXIF orientation values are numbered 1 to 8.
  #  1  = 0 degrees: the correct orientation, no adjustment is required.
  #  2  = 0 degrees, mirrored: image has been flipped back-to-front.
  #  3  = 180 degrees: image is upside down.
  #  4  = 180 degrees, mirrored: image has been flipped back-to-front and is upside down.
  #  5  = 90 degrees: image has been flipped back-to-front and is on its side.
  #  6  = 90 degrees, mirrored: image is on its side.
  #  7  = 270 degrees: image has been flipped back-to-front and is on its far side.
  #  8  = 270 degrees, mirrored: image is on its far side.
  if [ $ORIENTATION -lt 5 ]; then
    break;
  fi
else
  #in case of no ORIENTATION in exif, check W/H ratio
  IS_LANSCAPE=$(convert $FILE -format "%[fx:(w/h>1)?1:0]" info:)
  if [ $IS_LANSCAPE -eq 1 ]; then
    break;
  fi
fi


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment