django-imagekit icon indicating copy to clipboard operation
django-imagekit copied to clipboard

how to blur a Image?

Open harshjadon9 opened this issue 3 years ago • 4 comments

### my model field :

    image = ProcessedImageField(
                                      processors=[ResizeToFit(1200)],
                                      format='WEBP',
                                      options={'quality': 10})

i want to blur a image and then save it to database

harshjadon9 avatar May 10 '21 19:05 harshjadon9

You can use GaussianBlur: https://github.com/matthewwithanm/pilkit/blob/2a9ad303e9eea7be33be7f046020d79b918383a4/pilkit/processors/filter.py#L3

julianwachholz avatar Oct 28 '21 07:10 julianwachholz

@julianwachholz the module seems to not be available

ericel avatar May 15 '22 10:05 ericel

@ericel the latest version on PyPI is from 2017 unfortunately. It means you'll have to install the package from source.

julianwachholz avatar May 15 '22 10:05 julianwachholz

@vstoykov Do you have permissions to push pilkit updates?

matthewwithanm avatar May 15 '22 15:05 matthewwithanm

  1. First you have to create a Django model to represent the image you want to store in the database.

For example:

from django.db import models
from imagekit.models import ProcessedImageField
from imagekit.processors import Blur

class BlurredImage(models.Model):
    image = ProcessedImageField(upload_to='images/',
                                processors=[Blur(radius=5)],
                                format='JPEG',
                                options={'quality': 90})

  1. Then apply these changes by running these commands in terminal:

python manage.py makemigrations

python manage.py migrate

  1. Now upload your image via a form that you already have created to store the image into your database take the image from the form and process it for blurring the image:
def upload_image(request):
    if request.method == 'POST':
        form = ImageUploadForm(request.POST, request.FILES)
        if form.is_valid():
            blurred_image = BlurredImage()
            blurred_image.image = form.cleaned_data['image']
            blurred_image.save()
            # Additional logic or redirect
    else:
        form = ImageUploadForm()
    return render(request, 'upload_image.html', {'form': form})

This code should be provided in views.py file

Aayush3014 avatar Jul 18 '23 08:07 Aayush3014

New version of pilkit is released with the GaussianBlur processor as suggested by @julianwachholz if you update pilkit you will be able to blur images.

vstoykov avatar Sep 27 '23 23:09 vstoykov