Easy thumbs in your models

3 views
Skip to first unread message

zenx

unread,
Sep 25, 2006, 7:03:57 PM9/25/06
to Django users
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):
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?
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=\"%s\" target=\"_blank\"><img src=\"%s\"
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 ;)

zenx

unread,
Sep 27, 2006, 1:40:33 PM9/27/06
to Django users
In delete function use:
super(MyClass, self).delete() instead of super(Artista, self).delete().
I think everything else is ok.

Andrew Gwozdziewycz

unread,
Sep 27, 2006, 4:59:43 PM9/27/06
to django...@googlegroups.com
See below


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

Andrew Gwozdziewycz

unread,
Sep 27, 2006, 5:00:36 PM9/27/06
to django...@googlegroups.com
Of course you have to "import os.path"

On Sep 25, 2006, at 7:03 PM, zenx wrote:

zenx

unread,
Sep 27, 2006, 5:22:49 PM9/27/06
to Django users
you're right, that's better. thank you!!!

Reply all
Reply to author
Forward
0 new messages