thumbnail an image
Thumbnail an image.
- image
Thumbnail an image.
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 FileField Widget that displays an image instead of a file path if the current file is an image. Could also be used with sorl.thumbnail to generate thumbnail images. **Example** class FileUploadForm(forms.ModelForm): upload = forms.FileField(widget=AdminThumbnailWidget) class Meta: model = FileUpload class FileUploadAdmin(admin.ModelAdmin): form = FileUploadForm admin.site.register(FileUpload, FileUploadAdmin)
A URL field specifically for images, which can validate details about the filesize, dimensions and format of an image at a given URL, without having to read the entire image into memory. Requires [Python Imaging Library](http://www.pythonware.com/library/pil/). *4th October, 2008* - updated for 1.0 compatibility.
This function emulates the file upload behaviour of django's admin, but can be used in any view. It takes a list of POST keys that represent uploaded files, and saves the files into a date-formatted directory in the same manner as a `FileField`'s `upload_to` argument.
AdminImageWidget is a ImageField Widget for admin that shows a thumbnail. Usage example on a form: class IconForm(forms.ModelForm): icon = forms.ImageField(label='icon', widget=AdminImageWidget)
link and img are both HTML construction helpers. Good idea to use these helpers if your html don't fit into templates.
This is a very simple way to display images within the admin list view. It is not efficient in the sense that the images are being downloaded in original format, however for cases where the images are not regularly accessed it may be a straightforward option. Can also be tied into WYSIWYG editors like TinyMCE by adding an appropriate href link in the return value.
ResizeImageField ================ (extension of RemovableImageField) ================================= by Wim Feijen, Go2People. What does it do? ---------------- ResizeImageField is a replacement for django's ImageField. It has two major benefits: 1. Creation of thumbnails and scaled images. 1. Extends the image upload form and adds a preview and a checkbox to remove the existing image. It's easy to use: - Replace ImageField by ResizeImageField - No further changes are necessary Requirements: ------------- Working installation of PIL, the Python Imaging Library Usage ----- - add resize_image to your app - add resize_filters.py to your templatetags - in settings.py, set a PHOTO_DIR, f.e. photos/original - in models.py, add: - from settings import PHOTO_DIR - from resize_image import ResizeImageField - photo = ResizeImageField(upload_to=PHOTO_DIR, blank=True) Scaled images will be stored in 'photos/scaled', thumbnails will be stored in 'photos/thumb'. Access your images from your template. Add:: {% load resize_filters %} {{ address.photo.url|thumb }} or:: {{ address.photo.url|scaled }} Defaults ------- - Scaled images are max. 200x200 pixels by default - Thumbnails are 50x50 pixels. Override the default behaviour in settings.py Scaling is done by PIL's thumbnail function, transparency is conserved. Credits ------ This code is an adaptation from python snippet 636 by tomZ: "Updated Filefield / ImageField with a delete checkbox"
txt2img tag shows on the web text as images, helping to avoid get indexed email address and some other information you don't want to be on search engines. Usage: `{{worker.email|txt2img:18|safe}}`
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!
I've reimplemented the code I found somewhere on the web within my models file. The earlier version was incapable of converting all formats to JPG while this code converts all formats and compresses all of them successfully. You need to have PILLOW installed for this to work.
#### this is the right and working way
Implements a Django form that integrates image uploading plus cropping using the awesome Jcrop plugin (http://deepliquid.com/content/Jcrop.html). NOTE: Still lacks proper error handling...
**General notes:** - Set MEDIA_URL (or whatever you use for uploaded content to point to S3 (ie. MEDIA_URL = "http://s3.amazonaws.com/MyBucket/")) - Put django-storage in project_root/libraries, or change the paths to make you happy. - This uses the functionality of django-storage, but *not* as DEFAULT_FILE_STORAGE. The functionality works like so: **Getting stuff to S3** - On file upload of a noted model, a copy of the uploaded file is saved to S3. - On any thumbnail generation, a copy is also saved to S3. **On a page load:** 1. We check to see if the thumbnail exists locally. If so, we assume it's been sent to S3 and move on. 2. If it's missing, we check to see if S3 has a copy. If so, we download it and move on. 3. If the thumb is missing, we check to see if the source image exists. If so, we make a new thumb (which uploads itself to S3), and move on. 4. If the source is also missing, we see if it's on S3, and if so, get it, thumb it, and push the thumb back up, and move on. 5. If all of that fails, somebody deleted the image, or things have gone fubar'd. **Advantages:** - Thumbs are checked locally, so everything after the initial creation is very fast. - You can clear out local files to save disk space on the server (one assumes you needed S3 for a reason), and trust that only the thumbs should ever be downloaded. - If you want to be really clever, you can delete the original source files, and zero-byte the thumbs. This means very little space cost, and everything still works. - If you're not actually low on disk space, Sorl Thumbnail keeps working just like it did, except your content is served by S3. **Problems:** - My python-fu is not as strong as those who wrote Sorl Thumbnail. I did tweak their code. Something may be wonky. YMMV. - The relative_source property is a hack, and if the first 7 characters of the filename are repeated somewhere, step 4 above will fail. - Upload is slow, and the first thumbnailing is slow, because we wait for the transfers to S3 to complete. This isn't django-storage, so things do genuinely take longer.