Reputation: 45
I have problem with saving the image with proper format when overriding save method in Model.
My code:
class Whatever(models.Model):
image = models.ImageField(upload_to="whatever")
def save(self, *args, *kwargs):
# Here I save file to the disk
super(Whatever, self).save(*args, **kwargs)
# Then i get this file
filename = MEDIA_ROOT + '/' + self.image.name
# And do something with it
img = cut_circle(filename, (42, 42), 42)
# Now save it
img.save(filename+)
And here is the problem, i need transparency in result image, but user can upload to server bad formats, jpeg for example.
So, how i can change file format in base? I tried self.image.save, but this way is complicated, i think.
Upvotes: 0
Views: 144
Reputation: 87077
I dont know what library you are using to cut_circle
, but if it is PIL
, it can save the images in PNG by passing that as a format keyword argument; so you could do:
img.save(filename,format='png')
This seems fine for JPG; But if you want to retain the transparency of the formats like gif, you will need to take care of that one explicitly as covered by Nadia.
Upvotes: 2