Create a Python Django web application that allows users to upload images, apply basic image processing operations like resizing and cropping, and then download the processed images. The application should also maintain a history of uploaded and processed images using a MySQL database.
PLAINFORMAT
# Import required packages
from django.db import models
from django import forms
from django.shortcuts import render, redirect
from PIL import Image as PilImage
from io import BytesIO
from django.core.files.base import ContentFile
from django.urls import path

# Define the Image model
class Image(models.Model):
    original_image = models.ImageField(upload_to='original_images/')
    processed_image = models.ImageField(upload_to='processed_images/', blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"Image {self.pk}"

# Create the form for uploading images and applying image processing operations
class ImageForm(forms.ModelForm):
    width = forms.IntegerField(required=False)
    height = forms.IntegerField(required=False)

    class Meta:
        model = Image
        fields = ('original_image', 'width', 'height')

# Create the view for handling image uploads and processing
def index(request):
    if request.method == 'POST':
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid():
            img_obj = form.save(commit=False)
            original_image = PilImage.open(img_obj.original_image)
            width = form.cleaned_data['width'] or original_image.width
            height = form.cleaned_data['height'] or original_image.height
            processed_image = original_image.resize((width, height))
            
            buffer = BytesIO()
            processed_image.save(buffer, format='JPEG')
            img_obj.processed_image.save(f"processed_{img_obj.original_image.name}", ContentFile(buffer.getvalue()), save=False)
            img_obj.save()

            return redirect('index')

    else:
        form = ImageForm()

    images = Image.objects.all().order_by('-created_at')
    return render(request, 'image_app/index.html', {'form': form, 'images': images})

# Define the URL patterns
urlpatterns = [
    path('', index, name='index'),
]