1

Why does this not work?

from wand.image import Image

def upload_to_cars(instance, filename):
        blocks = filename.split('.')
        ext = blocks[-1]
        filename = "%s.%s" % (instance.name.replace(" ", "-"), ext)
        with Image (filename=filename) as img:
                img.type='grayscale';
                img.save  
        return filename
class Cars(models.Model):
        name = models.CharField(max_length=200)
        image_file = models.ImageField(upload_to=upload_to_cars, null=True, blank=True)

I want to upload an image and greyscale it in django. Everytime when I run this code it throws an

`decode delegate for this image format 'Porsche' @ error/constitute.c/ReadImage/544

Porsche is the name of the Class and supposed to be the name of the uploaded imagefile

Help!

Tom
  • 2,545
  • 5
  • 31
  • 71

1 Answers1

0

It is really simple, the problem is, the image gets uploaded into the media folder. So a simple string with "media/" will do the trick:

def grey():
        with Image (filename=media) as img:
            img.type='grayscale';
            img.save(filename=media)

def upload_to_cars(instance, filename):
    blocks = filename.split('.')
    ext = blocks[-1]
    filename = "%s.%s" % (instance.name.replace(" ", "-"), ext)
    global media
    media = "media/"+filename
    t=Timer(1.0,grey)
    t.start()
    return filename
Tom
  • 2,545
  • 5
  • 31
  • 71