Aneurin Price wrote: > Hello, > > I'd like to be able to generate images dynamically for a Django-based > website, and I'm wondering how best to go about it. > > The idea is to do certain things to existing images, such as > superimpose them on a background. The set of base images will be > more-or-less static; it's just the operation performed on it which > might change, so I don't really want to create an image model and have > to instantiate it for each image. Rather I'd like to be able to simply > put them in a directory, so I can make a request for > 'imagename-operation.png', and have my application check to see if it > exists in a cache, and if not, look for 'imagename.png' in the base > images directory and apply the given operation to it. I'm fine with > the image operations, but I can't decide where I should be doing this > work. > > I'm tempted to do it all in a view, so 'imagename' and 'operation' are > parsed from the request name and passed to the appropriate function, > which returns the image. Doing it in the view however doesn't really > feel right, and I wondered if perhaps I would be better with some > custom middleware, or something else entirely. > A view is the right place to do this. A view takes a request, parses it, and returns the response. This process holds true for html, an image, pdf, or office document. You'll likely want to use PIL for the actual manipulation/operation, but as long as you can get the binary content of the image into Python, you're fine. Simply set the mime type and content of the response, and you're good to go. There are examples in the Django documentation on how to do this with a PDF, but the response object instance creation will be basically the same for an image as well.
Take care! Jeff Anderson
signature.asc
Description: OpenPGP digital signature

