【问题标题】:django, upload tif imagedjango,上传tif图片
【发布时间】:2021-08-26 12:54:56
【问题描述】:

我正在尝试使用 django ImageField 上传 tif 图像(灰度)。 我已经安装了 Pillow==8.3.1 并使用 Python 3.9。 该应用仅适用于 PNG/JPEG 图像。

这是我正在使用的模型:

class Upload(models.Model):
    image = models.ImageField(upload_to='images')
    title = models.CharField(max_length=200)
    action = models.CharField(max_length=50,choices=ACTION_CHOICES)
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.title
        #breakpoint()
    # def __str__(self):
    #     pixels = tfi.imread(self.image)
    #     return np.shape(np.array(pixels))
    def save(self,*args,**kwargs):
        #open image
        #breakpoint()
        if self.action=='tif':
            pixels = tfi.imread(self.image)
        else:
            pixels = Image.open(self.image)
       
        #pixels = tfi.imread(self.image)
        pixels = np.array(pixels)
        pixels=pixels[:,:,0]
        #pixels = pixels[0,:,:]
        #use the normalisation method
        img = get_image(pixels)
        im_pil=Image.fromarray(img)
        #save
        buffer = BytesIO()
        im_pil.save(buffer,format='png')
        image_png = buffer.getvalue()
        self.image.save(str(self.image), ContentFile(image_png),save=False)
        super().save(*args,**kwargs)
        #return self.image_png

【问题讨论】:

    标签: django python-imaging-library tiff


    【解决方案1】:

    Django 的 ImageField 需要第三方包 Pillow。这取决于枕头来验证文件是否确实是图像。这不取决于文件类型,而是取决于文件本身的内容。 请检查您使用的枕头版本,并检查您的枕头版本的相应文档。

    https://pypi.org/project/Pillow/

    supported image format by pillow 8.3.1

    但是,如果您仍然无法正常工作,您始终可以使用 FileField,它不会查找您正在上传的文件或格式,直到您上传文件,请查看文档

    https://www.geeksforgeeks.org/filefield-django-models/

    虽然我建议您仅将 imagefield 用于图像并阅读文档,但现在完全取决于您要使用什么。

    【讨论】:

      猜你喜欢
      • 2014-03-20
      • 1970-01-01
      • 2012-04-03
      • 2019-07-16
      • 2020-06-07
      • 2015-10-30
      • 2013-06-05
      • 2015-01-03
      • 1970-01-01
      相关资源
      最近更新 更多