Using Pillow Module for Image Processing in Django

  • Post author:
  • Post comments:0 Comments
  • Reading time:41 mins read

What is Pillow

The Pillow module is a powerful image processing library that can be integrated into your Django project to handle image manipulation tasks such as resizing, cropping, adding filters, and more. In this article, we’ll explore how to install and use the Pillow module in a Django project

Now let’s look at how can we install this module

Installing Pillow

Before we can use Pillow in our Django project, we need to install it. We can install Pillow using pip, the package installer for Python. We can simply run the following command in our terminal or command prompt:


pip install Pillow

After the installation is complete, we can verify that Pillow is installed by opening a Python shell and importing the library

If the import is successful, we can proceed with integrating Pillow into our Django project.

Using Pillow in a Django project

To use Pillow in a Django project, we need to add it to our project’s settings file. In the INSTALLED_APPS section, we add 'django.contrib.staticfiles' and 'Pillow' to the list of installed apps:


INSTALLED_APPS = [
        'django.contrib.staticfiles',    
        'Pillow',
        ]

Next, we can create a model that includes an image field that we want to manipulate. For example, let’s say we have a model called Profile with an image field called profile_picture:


from django.db import models

class Profile(models.Model):
    name = models.CharField(max_length=100)
    profile_picture = models.ImageField(upload_to='profile_pictures/')

We can then create a form that includes a FileField for uploading an image


from django import forms
from .models import Profile

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['name', 'profile_picture']

In our views file, we can define a function to handle the form submission and manipulate the uploaded image


from django.shortcuts import render, redirect
from .forms import ProfileForm
from PIL import Image

def create_profile(request):
    if request.method == 'POST':
        form = ProfileForm(request.POST, request.FILES)
        if form.is_valid():
            profile = form.save(commit=False)
            image = Image.open(profile.profile_picture)
            image = image.resize((400, 400))
            image.save(profile.profile_picture.path)
            profile.save()
            return redirect('profile-detail', pk=profile.pk)
    else:
        form = ProfileForm()
    return render(request, 'create_profile.html', {'form': form})

In this example, we’re using the Image class from the Pillow module to open the uploaded image, resize it to 400×400 pixels, and save the modified image to the same location as the original image. We then save the profile object with the modified image path.

Conclusion

The Pillow module is a powerful image-processing library that can be used to manipulate images in a Django project. By adding the Pillow module to our project’s settings file and using the Image class in our views, we can easily resize, crop, and apply filters to uploaded images. With Pillow, we can create a more dynamic and engaging user experience in our Django projects

Publisher

Publisher

Publisher @ideasorblogs

Leave a Reply