如何在Django模板中渲染没有背景的PNG图像?

时间:2019-06-05 05:37:21

标签: django image django-models

我需要在前端显示png透明图像,但无法正确设置其格式。

我试图将格式更改为png,但它正在呈现黑色背景。

class HelpCategories(models.Model):
    Name1 = (
        ('Getting Started','Getting Started'),
        ('Pricing','Pricing'),
        ('Integration','Integration'),
        ('Security','Security'),
        ('Product Professionals','Product Professionals'),
        ('About Products','About Products'),
    )
    Title = models.CharField(max_length=40, default='Getting Started')
    image = ImageField(upload_to='help_image', null=True, blank=True)

    def __str__(self):
        return self.Title

    def save(self, *args, **kwargs):
        if self.image:
            imageTemporary = Image.open(self.image).convert('RGB')
            outputIoStream = BytesIO()
            imageTemporaryResized = imageTemporary.resize((400,400)) 
            imageTemporaryResized.save(outputIoStream, format='PNG', quality=300)
            outputIoStream.seek(0)
            self.image = InMemoryUploadedFile(outputIoStream,'ImageField', "%s.png" % self.image.name.split('.')[0], 'image/png', sys.getsizeof(outputIoStream), None)
            super(HelpCategories, self).save(*args, **kwargs)

我只需要能够看到模板中的图像对象。

0 个答案:

没有答案