On Sep 25, 2006, at 7:03 PM, zenx wrote:
I needed to make this and I hope it helps some people there. Features:
- Generate thumbs from ImageFields when creating the models
(file.jpg -> thumbnail saved as: file.thumbnail.jpg)
- Showing thumbs in admin
- Deleting thumbs when deleting the object
In your models.py:
================================
from PIL import Image
import glob, os
thumb_size = 90, 90
class MyClass(models.Model):
foto = models.ImageField(upload_to='my_photos/',blank=True)
def save(self):
file_path = self.get_foto_filename()
# is there a photo?
#if (file_path):
# This isn't very convincing. I'd use:
if os.path.exists(file_path):
file, ext = os.path.splitext(file_path)
im = Image.open(file_path)
# thanks to PIL ;)
im.thumbnail(thumb_size, Image.ANTIALIAS)
# save thumbnail
im.save(file + ".thumbnail.jpg", "JPEG")
super(MyClass, self).save()
def delete(self):
file_path = self.get_foto_filename()
# There's is a file?
# This isn't very convincing. I'd use:
if os.path.exists(file_path):
# if (file_path):
file, ext = os.path.splitext(file_path)
thumb = file + ".thumbnail.jpg"
# if there is a thumb we delete it
if(thumb):
os.remove(thumb)
super(Artista, self).delete()
def show_thumb(self):
# get the url of the file
file_path = self.get_foto_url()
# add .thumbnail between file and extension
file, ext = os.path.splitext(file_path)
file = file + ".thumbnail.jpg"
if(file_path):
return "<a href="" target=\"_blank\"><img src="">
border=\"0\" /></a>" % (file_path,file)
mostrar_thumb.allow_tags = True
mostrar_thumb.short_description='Foto'
class Admin:
list_display = ('show_thumb',)
==================================
That's all. If you make any improvements let me know them ;)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---