Thank you. I have just writed this code.
It works but I want to avoit to save 2 times with "super(book,
self).save()" ( see below ):
from django.db import models
from PIL import Image
import glob, os
thumb_size = 90, 90
class book(models.Model):
title = models.CharField(max_length=200)
photo = models.ImageField(upload_to='photo_book')
thumb = models.ImageField(upload_to='thumb_book', blank=True,
null=True)
def save(self):
super(book, self).save()
file_path = self.photo.url
if (file_path):
im = Image.open(file_path)
im.thumbnail(thumb_size, Image.ANTIALIAS)
root, immagin = os.path.split(file_path)
immagin, ext = os.path.splitext(immagin)
im.save("thumb_book/" + immagin + ".thumbnail.jpg",
"JPEG")
self.thumb = "thumb_book/" + immagin + ".thumbnail.jpg"
super(book, self).save()
--------------------
On Jan 4, 5:03 pm, Justin Myers <[email protected]> wrote:
> I'm not able to test this idea directly myself at the moment, but it
> appears you're storing an absolute path (i.e., starting from the
> filesystem root) in self.thumb. I'm pretty sure FileFields (and, by
> extension, ImageFields) store relative paths from settings.MEDIA_ROOT.
>
> (To make sure I'm thinking about this properly (since again, I can't
> double-check this right now), you might want to see what the value of
> the thumb field is in the database first. If I'm right, you should see
> a path that consists of a relative path followed by an absolute one,
> such as "2010/0104//home/username/webapps/media/2010/0104/
> something.thumbnail.jpg".)
>
> An absolute path is certainly helpful for actually operating on the
> file, but before
> self.thumb = imfile + ".thumbnail.jpg"
> you'll need something like
> imfile, ext = os.path.splitext(self.photo) # NOT .path, so this
> is a relative path, not an absolute one
> to make this work properly.
>
> Hope that helps.
> -Justin
>
> On Jan 4, 5:15 am, nameless <[email protected]> wrote:
>
> > I have writed this code but it doesn't work. What is the error ?
>
> > from django.db import models
> > from django.forms import ModelForm
> > from PIL import Image
> > import glob, os
>
> > thumb_size = 90, 90
>
> > class book(models.Model):
> > title = models.CharField(max_length=200)
> > photo = models.ImageField(upload_to='bookphoto')
> > thumb = models.ImageField(upload_to='bookthumb')
>
> > def save(self):
>
> > file_path = self.photo.path
> > if (file_path):
> > imfile, ext = os.path.splitext(file_path)
> > im = Image.open(file_path)
> > im.thumbnail(thumb_size, Image.ANTIALIAS)
> > im.save(imfile + ".thumbnail.jpg", "JPEG")
> > self.thumb = imfile + ".thumbnail.jpg"
> > super(book, self).save()
>
> > class BookForm(ModelForm):
>
> > class Meta:
> > model = book
> > exclude = ('thumb',)
>
> > Please help me, I am going crazy :-\
>
> > --------------
>
> > On Jan 2, 5:17 pm, Xia Kai(夏恺) <[email protected]> wrote:
>
> > > Hi,
>
> > > This is a fantastic app, though it might be too fat for a minimalist like
> > > me. ^_^
>
> > > I would recommend override the default save method of the model and resize
> > > the original photo using PIL. For the overriding part, you could consult
> > > the
> > > documentation:http://docs.djangoproject.com/en/dev/topics/db/models/#overriding-pre...
>
> > > ------------------------
> > > Xia Kai(夏恺)
> > > [email protected]http://blog.xiaket.org
>
> > > --------------------------------------------------
> > > From: "Chris Moffitt" <[email protected]>
> > > Sent: Saturday, January 02, 2010 11:59 PM
> > > To: <[email protected]>
> > > Subject: Re: Photo + thumbnail
>
> > > > You'll probably want to use one of Django's thumbnail apps. Here's the
> > > > one
> > > > I
> > > > recommend:
> > > >http://code.google.com/p/sorl-thumbnail/*
>
> > > > -*Chris
>
> > > > On Sat, Jan 2, 2010 at 9:48 AM, nameless <[email protected]> wrote:
>
> > > >> Hi everyone I have a simple question.
> > > >> This is my model:
>
> > > >> class book(models.Model):
> > > >> title = models.CharField(max_length=50)
> > > >> photo = models.ImageField(upload_to='images/avatar/')
> > > >> thumb = models.ImageField(upload_to='images/thumb/')
>
> > > >> I want in photo original photo and in thumb the same photo but
> > > >> resized.
> > > >> How do I do that in simplest way ?
>
> > > >> Thank you and Good year ^_^
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.