コード例 #1
0
    def save(self):
        product = super(ProductForm, self).save(commit=False)
        product.product_type = ProductType.objects.all()[0]
        product.save()

        x = self.cleaned_data.get('x')
        y = self.cleaned_data.get('y')
        w = self.cleaned_data.get('width')
        h = self.cleaned_data.get('height')

        image = Image.open(product.image)
        cropped_image = image.crop((x, y, w+x, h+y))
        resized_image = cropped_image.resize((400, 400), Image.ANTIALIAS)

        buffer = BytesIO()
        resized_image.save(fp=buffer, format='JPEG')
        pill_image = ContentFile(buffer.getvalue())

        
        image_name = product.image.name
        product.image.delete()
        temp_image = InMemoryUploadedFile(
            pill_image,       
            None,               
            image_name,           
            'image/jpeg',       
            pill_image.tell,  
            None)
        product.image.save(image_name, temp_image)
        product.save()
        temp_image.close()

        return product
コード例 #2
0
ファイル: models.py プロジェクト: tomrusso/objectiverse.com
def thing_post_save(sender, created, instance, **kwargs):
	if not created: return
	photo_filename = instance.photo
	basewidth = 150
	img = Image.open(photo_filename)
	wpercent = basewidth / float(img.size[0])
	hsize = int(img.size[1] * wpercent)
	img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
	img_io = StringIO.StringIO()
	img.save(img_io, format='JPEG')
	img_file = InMemoryUploadedFile(img_io, None, 'foo.jpg', 'image/jpeg', img_io.len, None)
	instance.small = img_file
	instance.save()
	img_file.close()
コード例 #3
0
def reduce_image_size(request):
    for product in Product.objects.all():
        image = Image.open(product.image)
        if (len(image.fp.read()) > 500000):
            buffer = BytesIO()
            image.save(fp=buffer, format='JPEG', quality=20, optimize=True)
            pill_image = ContentFile(buffer.getvalue())
            image_name = product.image.name
            product.image.delete()
            temp_image = InMemoryUploadedFile(pill_image, None, image_name,
                                              'image/jpeg', pill_image.tell,
                                              None)
            product.image.save(image_name, temp_image)
            product.save()
            temp_image.close()
    return HttpResponse('Success')