Login

Tag "image"

Snippet List

A Django image thumbnail filter

I've gotten this code originally from Dan Fairs. I've edited it a bit to make it do what I wanted it to do. I've sent a mail and the original can be considered public domain code. The same is true for this

  • image
  • resize
Read More

Easy file upload handler

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.

  • image
  • forms
  • view
  • upload
  • imagefield
  • filefield
  • file
Read More

auto image field w/ prepopulate_from & default

Given such code: class ProductImage(models.Model): fullsize = models.ImageField(upload_to= "products/%Y/%m/%d/") display = AutoImageField(upload_to= "products/%Y/%m/%d/",prepopulate_from='fullsize', size=(300, 300)) thumbnail = AutoImageField(upload_to="products/%Y/%m/%d/",null=True,default='/media/noimage.jpg')) display will be automatically resized from fullsize, and thumbnail will default to /media/noimage.jpg if no image is uploaded Note: At some point the code broke against trunk, which has now been updated to work properly

  • image
  • models
  • db
  • field
Read More

[UPDATE]Filter to resize a ImageField on demand

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/

  • template
  • image
  • thumbnail
Read More

Admin Image Widget

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)

  • image
  • newforms-admin
  • widget
  • file
  • nfa
Read More

Image gradients on the fly

A Django view to create an image gradient on the fly as a PNG file. The direction, size and colors of the gradient a specified in the filename and extracted by Django in the urls.py. Example usage from CSS: `background: url(/gradient-down-255,255,255-to-0,0,0-70-of-120.png) repeat-x;` creates a 70-pixel vertical gradient as background from white to gray. No static images needed. To modify, nothing but the CSS needs to be edited.

  • image
  • gradient
  • background
Read More

Image Standarization

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).

  • image
  • thumbnail
  • resize
  • rename
Read More

Resize image on save

This snippet is extracted from my Photo model. I use it to ensure that any uploaded image is constrained to a specified size (resized on save). In my case, I don't need to maintain a "thumbnail" and "fullsize" version, so I just store the resized version to save space.

  • image
  • pil
  • resize
Read More

Dynamic thumbnail generator

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"

  • image
  • thumbnail
  • model
  • imagefield
Read More

UPDATED: Django Image Thumbnail Filter

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!

  • filter
  • image
  • thumbnail
Read More

html helpers for images and links

link and img are both HTML construction helpers. Good idea to use these helpers if your html don't fit into templates.

  • template
  • image
  • html
  • img-src
  • link
  • a-href
  • construction
  • helper
Read More

ImageURLField for forms

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.

  • image
  • pil
  • validation
  • url
  • form
  • field
Read More

Simple admin list thumbnail view

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.

  • image
  • admin
Read More

Random-image template tag

This tag makes it easy to have a random rotation of images on a page. Don't forget to set your MEDIA_URL.

  • template
  • image
  • random
Read More
Author: pbx
  • 3
  • 12

64 snippets posted so far.