为什么这不起作用?
来自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)
我想上传一张图片并在django中对其进行灰度化。每当我运行此代码时,它都会抛出一个
`解码代表这个图像格式'保时捷'@ 误差/ constitute.c / ReadImage / 544
Porsche是Class的名称,应该是上传的图像文件的名称
帮助!
答案 0 :(得分:0)
这很简单,问题是,图像上传到媒体文件夹。因此,使用"media/"
的简单字符串可以解决这个问题:
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