Admin list thumbnail

 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

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

#

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.