Reputation: 11523
I'm trying to resize images with Pillow but it is not working correctly, I always get a very small image. I need three different sizes, that is why I'm doing this:
# Save Base64 Images
cover = Image.open(StringIO.StringIO((base64.b64decode(cover_image_64))))
cover_original = cover
cover_small = cover
cover_medium = cover
small_size = ProductCover.IMAGE_SIZES["small"]
medium_size = ProductCover.IMAGE_SIZES["medium"]
print(small_size)
print(medium_size)
# HERE->
cover_small.thumbnail(small_size, Image.ANTIALIAS)
cover_medium.thumbnail(medium_size, Image.ANTIALIAS)
original_image_file = StringIO.StringIO()
small_image_file = StringIO.StringIO()
medium_image_file = StringIO.StringIO()
cover_small.save(small_image_file, format="JPEG")
cover_medium.save(medium_image_file, format="JPEG")
cover_original.save(original_image_file, format="JPEG")
ProductCover.objects.create(product=product,
original=InMemoryUploadedFile(original_image_file, None, "cover_original.jpg",
"image/jpeg", original_image_file.len, None),
medium=InMemoryUploadedFile(medium_image_file, None, "cover_medium.jpg",
"image/jpeg", medium_image_file.len, None),
small=InMemoryUploadedFile(small_image_file, None, "small_small.jpg",
"image/jpeg", small_image_file.len, None))
The print statements print this to the console:
(60, 60)
(512, 512)
Why then I get three 60*60 images?
The code seems so straightforward and simple that I'm completely lost. Maybe its Django. I don't know.
Upvotes: 1
Views: 77
Reputation: 59228
You are assigning the same object reference to three variable names here:
cover_original = cover
cover_small = cover
cover_medium = cover
Since all three point to the same object, calling .thumbnail()
on any of them will modify the others. Try cloning the image using Image.copy()
instead:
cover_original = cover.copy()
cover_small = cover.copy()
cover_medium = cover.copy()
Upvotes: 1