Easy way to generate image thumbnails for your models. Works with any Storage Backend.
From: [http://code.google.com/p/django-thumbs/](http://code.google.com/p/django-thumbs/)
**Usage example:**
==============
photo = ImageWithThumbsField(upload_to='images', sizes=((125,125),(300,200),)
To retrieve image URL, exactly the same way as with ImageField:
my_object.photo.url
To retrieve thumbnails URL's just add the size to it:
my_object.photo.url_125x125
my_object.photo.url_300x200
Note: The 'sizes' attribute is not required. If you don't provide it,
ImageWithThumbsField will act as a normal ImageField
**How it works:**
=============
For each size in the 'sizes' atribute of the field it generates a
thumbnail with that size and stores it following this format:
available_filename.[width]x[height].extension
Where 'available_filename' is the available filename returned by the storage
backend for saving the original file.
Following the usage example above: For storing a file called "photo.jpg" it saves:
photo.jpg (original file)
photo.125x125.jpg (first thumbnail)
photo.300x200.jpg (second thumbnail)
With the default storage backend if photo.jpg already exists it will use these filenames:
photo_.jpg
photo_.125x125.jpg
photo_.300x200.jpg
**Note:** It assumes that if filename "any_filename.jpg" is available
filenames with this format "any_filename.[widht]x[height].jpg" will be available, too.
Couldn't get the original to work, and wanted more functionality (scale on x or y coordinates)
<img src="{{ object.image.url }}" alt="original image">
<img src="{{ object.image|thumbnail:"250w" }}" alt="image resized to 250w x (calculated/scaled)h ">
<img src="{{ object.image|thumbnail:"250h" }}" alt="image resized to (calculated/scaled)w x 250h h ">
<img src="{{ object.image|thumbnail:"250x200" }}" alt="image resized to 250wx200h ">
<img src="{{ object.image|thumbnail }}" alt="image resized to default 200w (or whatever you default it to) format">
Original http://www.djangosnippets.org/snippets/192/
Adapted http://www.djangosnippets.org/snippets/955/
Sampled From:
http://batiste.dosimple.ch/blog/2007-05-13-1/
http://vaig.be/2008/05/17/stdimagefield-improved-image-field-for-django/
A simple template filter that detects [iPernity](http://www.ipernity.com) static URLs and creates clickable thumbnail for them. Use it with [Lightbox](http://www.huddletogether.com/projects/lightbox2/) or any other funky image overlay script.
Your HTML code may contain this:
<p>A short example <img
src="http://...ipernity..."
title="The Description" />.</p>
After applying this filter it will become:
<p>A short example <a
href="http://...large version..."
title="The Title"><img
alt="The Title"
src="http://...thumb version..."/>
</a>.</p>
Thats all! You may have a look at this: [iPernity and static URLs](http://www.ipernity.com/group/api-users/discuss/20098)
This is an override the save method of our Photo model. This new save method essentially takes the image, thumbnails it into our various sets of dimensions (for … in self.IMAGE_SIZES…), and save each one (into its own ImageField) before finally call the overwritten method to save the original image.
Yes, the dimensions are hardcoded, and there is currently not a way to regenerate them in different sizes, but one shouldn't be that hard to come up with, because you just could just load each photo object to regenerate, then save it again (or something along those lines).
mattpdx helped a lot with figuring out this code.
**So you can upload rectangular pictures but still have square thumbnails.**
"Scale to **fill**" instead of the out of the box "scale to **fit**" you get with `Image.thumbnail`
Usage:
from django.db import models
from imagevariations.fields import ImageVariationsField, Thumbnail
class Image(models.Model):
name = models.CharField(max_length=20)
image = ImageVariationsField(upload_to='testimages', variations=(Thumbnail,) )
How to use in templates:
Use the lowercase name of the image variation class.
{{ object.image.variations.thumbnail.url }}
By default all image variations will use the same storage backend as the field but can be replaced per variation by setting self.storage on the variation class.
A filter to resize a ImageField on demand, a use case could be:
<img src="{{ object.image.url }}" alt="original image">
<img src="{{ object.image|thumbnail }}" alt="image resized to default 104x104 format">
<img src="{{ object.image|thumbnail:200x300 }}" alt="image resized to 200x300">
Original http://www.djangosnippets.org/snippets/192/
Standarization of image fields (for being used when saving models). Automatically creates thumbnail. Change image name to /path/to/images/<my_field>-<id>.<ext>. Resize image and thumbnail to specified size (optionally can crop image to force size).
This snippet creates thumbnails on-demand from a ImageField with any size using dynamics methods, like ``get_photo_80x80_url`` or ``get_photo_640x480_filename``, etc.
It assumes you have an `ImageField` in your Model called `photo` and have this in your models.py:
import re
from os import path
from PIL import Image
GET_THUMB_PATTERN = re.compile(r'^get_photo_(\d+)x(\d+)_(url|filename)$')
`models.py` example:
import re
from os import path
from PIL import Image
from django.db import models
GET_THUMB_PATTERN = re.compile(r'^get_photo_(\d+)x(\d+)_(url|filename)$')
class Photo(models.Model):
photo = models.ImageField(upload_to='photos/%Y/%m/%d')
<snippet here>
Example usage:
>>> photo = Photo(photo="/tmp/test.jpg")
>>> photo.save()
>>> photo.get_photo_80x80_url()
u"http://media.example.net/photos/2008/02/26/test_80x80.jpg"
>>> photo.get_photo_80x80_filename()
u"/srv/media/photos/2008/02/26/test_80x80.jpg"
>>> photo.get_photo_64x64_url()
u"http://media.example.net/photos/2008/02/26/test_64x64.jpg"
>>> photo.get_photo_64x64_filename()
u"/srv/media/photos/2008/02/26/test_64x64.jpg"
A Django image thumbnail filter, adapted from code by [Batiste Bieler](http://batiste.dosimple.ch/blog/2007-05-13-1/).
This updated version drops support for cropping and just rescales. You should use it in your templates like this:
`<img src='{{ MEDIA_URL }}{{ image.get_image_filename|thumbnail:"300w,listingimages" }}' alt="{{ image.title }}" title="{{ image.title }}" />`
This will produce a 300-pixel wide thumbnail of image, with the height scaled appropriately to keep the same image aspect ratio. 'listingimages' is the path under your MEDIA_ROOT that the image lives in - it'll be whatever upload_to is set to in your ImageField.
If instead you wanted an image scaled to a maximum height of 140px, you'd use something like this:
`<img src='{{ MEDIA_URL }}{{ image.get_image_filename|thumbnail:"140h,listingimages" }}' alt="{{ image.title }}" title="{{ image.title }}" />`
Note the number has changed from 300 to 140, and the trailing letter from 'w' to 'h'.
Please leave feedback and bug reports on [my blog, Stereoplex](http://www.stereoplex.com/two-voices/a-django-image-thumbnail-filter). I've only lightly tested this so you'll probably find something!
Prerequisites: [Python Imaging Library](http://www.pythonware.com/products/pil/)
This function scales a given image (provided as binary data in any format the PIL supports) to a specified size.
If the force parameter is True, the function makes sure that the resulting image is exactly the specified size, cropping and scaling it as necessary (but never distorting it) to make sure the whole image area is filled out.
If force is False, it simply uses the thumbnail function provided by the PIL, which preserves the image aspect ratio and does not increase the image dimensions beyond those of the original file, so you may not get an image that has the exact dimensions you specified.
The result image is returned as JPEG data.
A filter to resize a ImageField on demand, a use case could be:
`
<img src="object.get_image_url" alt="original image">
<img src="object.image|thumbnail" alt="image resized to default 200x200 format">
<img src="object.image|thumbnail:"200x300" alt="image resized to 200x300">
`
The filter is applied to a image field (not the image url get from *get_image_url* method of the model), supposing the image filename is "image.jpg", it checks if there is a file called "image_200x200.jpg" or "image_200x300.jpg" on the second case, if the file isn't there, it resizes the original image, finally it returns the proper url to the resized image.
There is a **TODO**: the filter isn't checking if the original filename is newer than the cached resized image, it should check it and resize the image again in this case.
This is a really useful function I used to create the resized preview images you can see on the [homepage of my site](http://www.obeattie.com/). Basically, it takes the original URL of an image on the internet, creates a resized version of that image by fitting it into the constraints specified (doesn't distort the image), and saves it to the MEDIA_ROOT with the filename you specify.
For example, I use this by passing it the URL of a Flickr Image and letting it resize it to the required size, and then saving it to a local path. It then returns the local path to the image, should you need it. I however just construct a relative URL from the image_name and save that to the database. This way, it's easy to display this image.
Hope it's useful!
Returns a sharpened copy of an image.
Resizing down or thumbnailing your images with PIL.Image tends to make them blurry. I apply this snippet to such images in order to regain some sharpness.