Sunday, February 2, 2014

Photo orientation handling in google appengine with python

After realising that not all photos uploaded are correctly handled in regard to orientation the search started. After some hours I got the solution. First of all this happens only on PNG pictures uploaded by phonegap camera plugin on iOS. A work around would be parsing the meta data of an image uploaded to google appengine. If you use phonegap camera plugin with encoding JPEG there is no issue (and even much faster).

It occurred with my iPhone with PNG as phonegap camera encoding type and Apple uses the Orientation meta data tag, it seems the following values give guidance on how to rotate your uploaded image before you save.

This applies only to images saved in the datastore db.Blob, NOT the blobstore ! In blobstore you have no influence during the upload and the blobstore handles the orientation correctly.

The following post supported my solution:

This posts lists the iOS orientation values used:

1: image is Normal-> that’s the orientation value you get when the iphone home button is on the right
2: image is flipped horizontally
3: image is rotated 180° -> that’s the orientation value you get when the iphone home button is on the left
4: image is flipped vertically
5: image is rotated 90° CCW and flipped vertically
6: image is rotated 90° CW 
7: image is rotated 90° CW and flipped vertically
8: image is rotated 90° CCW 

How to rotate the image
orientation = 1 -> don’t rotate
orientation = 3 -> rotate 180° clockwise
orientation = 6 -> rotate 90° clockwise
orientation = 8 -> rotate -90° clockwise (or 90° counter clockwise if you prefer)

The sample code (python google appengine):

obj = YourDBObj()
img = images.Image(raw_file)
save = img.execute_transforms(output_encoding=images.PNG, parse_source_metadata=True)
meta_data = img.get_original_metadata()
  if meta_data != None:'debug: meta data nach image %s', meta_data)
      if meta_data.get("Orientation"):
'debug: meta data Orientation found %s', meta_data["Orientation"])
          if meta_data.get("Orientation") == 6:
    'debug: rotate image by 90')
              save = img.execute_transforms(output_encoding=images.PNG, parse_source_metadata=True)
'debug: no orientation')
obj.pict = db.Blob(save)
For blogstore objects using the image get_serving_url() you could do rotation by adding -r90 as of an undocumented feature:


  1. Hi friends, This is Victoria from Chennai. I am a technology freak. I have read your blog. It was really a wonderful article and I was really impressed by reading this blog. Thanks for sharing this informative blog. I did HTML Training in Chennai at FITA academy, Its really useful for me to make a bright future.

  2. Digital cameras photography follows a lot of the similar techniques that produce successful film photographs. Nevertheless, digital cameras are different in some important ways. gopro helmet mount

  3. This comment has been removed by the author.

  4. I found your blog on Google and read a few of your other posts too, it is very informative and also influencing me to read more of yours. You have done an exceptional job by adding your hardwork and dedication to it. I am a big fan of art and craft. I just added you to my bookmarks and recommending your blog to my social profiles too. Just like the other budding bloggers i have started my blog, You can support me by visiting my site for more paper airplane related information and knowledge, Keep up the great work Look forward to reading more from you in the future. Thanks in anticipation.
    how to make a paper airplane that flies far and straight | classic dart paper airplane | how to make a good paper | nakamura lock paper airplane instructions | best paper airplane design for distance and accuracy | best paper airplane design for distance and speed | Boomerang paper airplane instructions | Zazoom internet | Eagle paper airplane | how to make a paper airplane that flies far and straight step by step | 4 aerodynamic forces | Paper airplane templates for distance | the eagle paper airplane | best paper plane in the world | How to make a successful paper airplane