Solved thanks to
http://groups.google.com/group/django-users/browse_thread/thread/cae5c5a532a73b6d?pli=1

I was using image.resize instead of image.thumbnail. The second one
resize in place.

    def save(self, **kwargs):
        super(Foto, self).save(**kwargs)
        path = self.image.path
        i = Image.open(path)
        i.thumbnail(800,600), Image.BILINEAR)
        i.save(path)

Juanjo

2008/10/6 Juanjo Conti <[EMAIL PROTECTED]>:
> Thanks for your reply, but...
>
> Whats resized? It's not in my namespace.
>
> 2008/10/4 varikin <[EMAIL PROTECTED]>:
>>
>>
>>
>> On Oct 3, 4:18 pm, "Juanjo Conti" <[EMAIL PROTECTED]> wrote:
>>> Hi, I used to use a pre 1.0 svn version of Django. There I had this class:
>>>
>>> class Foto(models.Model):
>>>     descripcion = models.CharField(max_length=30, blank=True,
>>> verbose_name=u"descripción")
>>>     imagen = models.ImageField(upload_to='imagenes', verbose_name=u"foto")
>>>     inmueble = models.ForeignKey(Inmueble)
>>>
>>>     ....
>>>
>>>     def _save_FIELD_file(self, field, filename, raw_contents, save=True):
>>>         '''
>>>         Se reescribe el metodo de la clase padre para definir el
>>> nombre de archivo con
>>>         el que se guardará la imagen en el sistema de archivos. Se
>>> utilizan enteros crecientes.
>>>         '''
>>>         if TESTING:
>>>             field.upload_to = 'tests_dest'
>>>         filename = Foto._cambio_de_nombre(filename)
>>>         raw_contents = Foto._ajustar_tamanio(raw_contents)
>>>         super(Foto, self)._save_FIELD_file(field, filename, raw_contents, 
>>> save)
>>>
>>> The  _save_FIELD_file method was redefined to change the file name and
>>> resize it. Now in 1.0 I can rename the filename with a callable to use
>>> as upload_to ImageField argument:
>>>
>>> def _upload_to(instance, filename):
>>>     intancia.imagen. raw_contents = Foto._ajustar_tamanio(raw_contents)
>>>     filename = Foto._cambio_de_nombre(filename)
>>>     if TESTING:
>>>         return os.path.join('tests_dest', filename)
>>>     return os.path.join('imagenes', filename)
>>>
>>> but I don't know where I have to put my code to resize the image
>>> before it gets saved. Can anyone help me?
>>>
>>> Thanks,
>>>
>>> Juanjo
>>
>> I have been overriding the save method on the model.  I have found
>> that by the time the save method is called, the file is saved to the
>> disk already, So I open the original, resize and save to the
>> thumbnail:
>>
>> class Picture(models.Model)
>>    name = models.CharField(max_length=200)
>>    original = models.ImageField(upload_to='original')
>>    thumbnail = models.ImageField(upload_to='thumbnail')
>>
>>    def save(self, **kwargs):
>>            orig = Image.open(self.original.path)
>>            name = os.path.basename(self.original.name)
>>            height = int(100 * orig.size[1] / orig.size[0])
>>
>>            thumb = orig.resize((width, height), Image.ANTIALIAS)
>>            thumb_file = tempfile.NamedTemporaryFile('w+b')
>>            resized.save(thumb_file, 'JPEG')
>>            self.thumbnail.save(name, File(thumb_file), False)
>>            thumb_file.close()
>>
>>           super(Picture, self).save(**kwargs)
>>
>> I have to save the resized image to a temp file because
>> self.thumbnail.save takes a django.core.files.File object which takes
>> an python file object in the constructor.  I would love to be able to
>> use an in memory object instead of a temp file, but that isn't in
>> possible yet.
>>
>> John
>> >>
>>
>
>
>
> --
> Juanjo Conti
>



-- 
Juanjo Conti

--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to