Is it possible to use the google app engine python imaging library, from a python script, to load an image file from the local filesystem? Or must images be loaded from the datastore? I can successfully load a text file and display it’s contents.
I am using google app engine with Python 2.7 and using the PIL imaging library. I am using PyCharm and Google App Engine Launcher on Mac OS X 10.8.2. I am using version 1.1.7 of PIL which I built from source.
I want to perform some manipulation upon an image (or images) before returning them in the response output of my python script.
When I use the Image.image( filename=<filename> ) function, the error I get informs me that the filename must start with “/gs”. Stepping into the __init()__ method of the Image object shows me that blobstore.create_gs_key() is used to create a blobkey if I supply a filename to the Image constructor.
Because I have a fixed set of images that I wish to manipulate, I was hoping to avoid having to manually upload them into the datastore for retrieval.
My trivial application handler currently looks like this;
class ChallengeImageHandler( webapp2.RequestHandler ):
def get(self):
olifant = images.Image( filename='gs/challenge/C017.png' )
logging.debug( "olifant.width=%d" % olifant.width );
logging.debug( "olifant.height=%d" % olifant.height );
self.response.headers[ 'Content-Type' ] = 'image/png'
self.response.out.write( olifant )
return
My app.yaml looks like this;
application: hello_world
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /images
static_dir: images
- url: /.*
script: main.application
- url: /software_development.html
script: main.application
libraries:
- name: jinja2
version: latest
- name: PIL
version: latest
I understand that I could achieve the same result by serving the image statically, but once I have managed to successfully load the image, I wish to manipulate it before serving it up. I have deliberately placed the images that I wish to load from my application and not have served as static content by the google app engine web server in a separate directory than /images. They are located in a subdirectory called gs/challenge.
Any assistance, suggestions or solutions much appreciated. Perhaps I am going about this entirely the wrong way?
You can load files from the local filesystem if you want just by uploading them along with your application.
As long as you realise that they will never change and you can’t change them except when you deploy your application, and that there is a limit on the amount of space a deployed application can take up you should be OK.
For example, I upload a .csv file along with my application and when it starts up for the first time it reads it and creates a record in the datastore related to it’s contents. I then never use that file again.
Where you place your images is not really relevant, as you are manipulating them before you serve them to the user they are not “static content” as such and won’t be served to the user directly (as a .css file would be for example). As long as your application can load them that’s the right place.