Login

Admin list thumbnail

Author:
theetderks
Posted:
May 16, 2007
Language:
Python
Version:
.96
Tags:
admin imagefield
Score:
8 (after 8 ratings)

This code will add a thumbnail image to your Model's Admin list view. The code will also generate the thumb images, so the first view may be a little slow loading.

This assumes you have an ImageField in your Model called image, and the field's upload_to directory has a subdirectory called tiny. You then must add "thumb" to your Model's Admin list_display.

The thumbnail images are also linked to the full size view of the image.

I found this VERY useful... hope someone else does as well.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<<add to top of file>>

import os, Image
TINY_SIZE = (80,80)    #thumb size (x,y)

<<add to Model>>

	def thumb(self):
		tinythumb = self.image.replace('\\','/').split('/')
		tinythumb[-1] = 'tiny/'+tinythumb[-1]
		tinythumb = '/'.join(tinythumb)
		if not os.path.exists(MEDIA_ROOT+tinythumb):
			im = Image.open(MEDIA_ROOT+self.image)
			im.thumbnail(TINY_SIZE,Image.ANTIALIAS)
			im.save(MEDIA_ROOT+tinythumb,"JPEG")
		return """<a href="/media/%s"><img src="/media/%s" alt="tiny thumbnail image" /></a>"""%(self.image,tinythumb)
	thumb.allow_tags = True

More like this

  1. ImageWithThumbsField by zenx 6 years, 8 months ago
  2. Template Tag of Django Image Thumb Creator by ayang23 4 years, 7 months ago
  3. django-thumbs black&white support by rdugo 2 years, 11 months ago
  4. Sorl Thumbnail + Amazon S3 by skoczen 6 years, 2 months ago
  5. Letterbox for sorl-thumbnail by Mogga 6 years, 4 months ago

Comments

marco (on September 21, 2007):

Hi all, i did try this code but it doesn't work. This code: im = Image.open(MEDIA_ROOT+self.image) im.thumbnail(TINY_SIZE,Image.ANTIALIAS) im.save(MEDIA_ROOT+tinythumb,"JPEG")

don't generate the thumb in the folder "tiny"... I work with Django on OSX.

Can anyone please help me on this? thanks in advance guys.

#

theetderks (on October 3, 2007):

Marco -

Have you installed and imported PIL (Python Imaging Library called: Image)?

If your not sure, from a python prompt try: import Image

What does your traceback look like?

Thomas

#

imdimankov (on November 20, 2007):

Hello,

The same idea in a more correct way:

 def thumb(self):
      tiny = path.join(path.dirname(self.get_image_filename()), 'tiny', self.image)
      if not path.exists(tiny):
          im = Image.open(self.get_image_filename())
          im.thumbnail(TINY_SIZE, Image.ANTIALIAS)
          im.save(tiny, 'JPEG')
      tiny_url = path.join(MEDIA_URL, 'tiny', self.image)
      return '<a href="%s" target="_blank"><img src="%s" alt="tiny thumbnail image"/></a>' % (self.get_image_url(), tiny_url)
  thumb.allow_tags = True
  thumb.short_description = 'Thumb'

Of course, you should add "from os import path" at the beginning of the file.

Thanks, Dmitriy

#

vectart (on February 27, 2011):

And more correct for change list:

models.py

from django.conf import settings

"""add this methods to the model class"""

def get_image_filename(self):
    return '%s%s' % (settings.MEDIA_ROOT, self.image, )

def get_image_url(self):
    return '%s%s' % (settings.MEDIA_URL, self.image, )

admin.py

from os import path
import Image

TINY_SIZE = (80,80)
def show_thumb(self):
    tiny = path.join(path.dirname(self.get_image_filename()), 'tiny', str(self.image))
    if not path.exists(tiny):
        im = Image.open(self.get_image_filename())
        im.thumbnail(TINY_SIZE, Image.ANTIALIAS)
        if not path.exists(path.dirname(tiny)):
            from os import makedirs
            makedirs(path.dirname(tiny))
        im.save(tiny, 'JPEG')
    tiny_url = path.join(path.dirname(self.get_image_url()), 'tiny', str(self.image))
    return '<a href="%s/"><img src="%s" alt="tiny thumbnail image"/></a>' % (self.id, tiny_url)
show_thumb.allow_tags = True
show_thumb.short_description = ''

#

Please login first before commenting.