Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

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:
http://lkubaski.wordpress.com/2013/01/18/rotating-photos-from-mobile-devices-uploaded-to-google-app-engine/

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)
img.resize(140)
save = img.execute_transforms(output_encoding=images.PNG, parse_source_metadata=True)
meta_data = img.get_original_metadata()
  if meta_data != None:
      logging.info('debug: meta data nach image %s', meta_data)
      if meta_data.get("Orientation"):
          logging.info('debug: meta data Orientation found %s', meta_data["Orientation"])
          if meta_data.get("Orientation") == 6:
              logging.info('debug: rotate image by 90')
              img.rotate(90)
              save = img.execute_transforms(output_encoding=images.PNG, parse_source_metadata=True)
      else:
          logging.info('debug: no orientation')
obj.pict = db.Blob(save)
obj.put()
For blogstore objects using the image get_serving_url() you could do rotation by adding -r90 as of an undocumented feature: https://code.google.com/p/googleappengine/issues/detail?id=4200

Thursday, January 2, 2014

Google Appengine

Very mature application web server engine. I made some examples using python with functionalities like excel export, pdf creation, openid authentication, RESTful service, database design (big table based). Overall an excellent approach to develop web services very fast and being prepared for mass usage if you like.

Unfortunately the authentication seams to be still very beta. It supports only OpenID but OpenID Connect would be much better as this supports oauth1/2 and OpenID.

The google cloud console is still very early but shows the direction it will go to.

Tuesday, June 12, 2012

Django Python

Well I watched django the western movies but this is certainly something else. Django is a framework based on python, mainly build to serve web pages or services but has some other nice features. I used Aptana Studio which provides pydev and django plugins which make it very easy to setup project and develop with Eclipse. Using python is similar to perl, without modules you can't do real applications, however this is as well the nasty bit as to get the right modules on your OS can be quite tricky. I tried some distributions but I decided to use ActiveState Python as it provides easy access to modules free of charge on Windows. On Unix it is a bit easier but ActiveState is cerntainly a good choice as well. On Windows you have to use the 32bit distribution as the modules for the 64bit are restricted to a valid subscription at ActiveState. Key modules are the database modules and of course Django. After installing them you can move on with Aptana and development. The nice bit is the administration framework and the ORM feature of Django. It is easy to reverse engineer an existing database (like the yafra database) and start off immediately.

A simple python standalone and Django application is available.

https://www.djangoproject.com/
http://www.python.org/