Reputation: 734
I'm making a model called Image that has a method to save another version of itself with a different size. But I can't get the ImageField in the new version to accept the saved image file. It fails on the save method, with 'NoneType' object is not subscriptable.
def newversion(self, size):
""" Save a new version of the image """
image_version = Image(user=self.user, original=self)
image_version.save()
path, old_name = os.path.split(self.image.path)
ext = os.path.splitext(old_name)[1]
vsn_name = "{0}{1}".format(str(image_version.id),ext)
vsn_path = os.path.join(path, vsn_name)
pil_image = PIL.Image.open(self.image.path)
if pil_image.mode not in ('L','RGB'):
pil_image = pil_image.convert('RGB')
pil_image.thumbnail(size, PIL.Image.ANTIALIAS)
pil_image.save(vsn_path, pil_image.format)
image_version.image.save(vsn_name,ImageFile(open(vsn_path)), True)
Upvotes: 1
Views: 580
Reputation: 734
It is because I am on Windows, and I need to open the file with "rb":
open(vsn_path, 'rb')
Upvotes: 1