Skip to content

Commit be19284

Browse files
authored
Merge pull request #116 from raj-mistry-01/main-616
Added the image compressor.
2 parents 4d507b3 + 4ae0159 commit be19284

File tree

15 files changed

+193
-0
lines changed

15 files changed

+193
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from django.contrib import admin
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
class CompressorConfig(AppConfig):
4+
default_auto_field = 'django.db.models.BigAutoField'
5+
name = 'compressor'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from django import forms
2+
3+
class ImageForm(forms.Form):
4+
image = forms.ImageField()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Image Compressor</title>
5+
</head>
6+
<body>
7+
<h1>Upload Image to Compress</h1>
8+
<form method="post" enctype="multipart/form-data">
9+
{% csrf_token %}
10+
{{ form.as_p }}
11+
<button type="submit">Compress</button>
12+
</form>
13+
{% if compressed_url %}
14+
<h2>Compressed Image:</h2>
15+
<a href="{{ compressed_url }}" download>Download Compressed Image</a>
16+
{% endif %}
17+
</body>
18+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from django.db import models
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from django.test import TestCase
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.urls import path
2+
from . import views
3+
4+
urlpatterns = [
5+
path('', views.index, name='index'),
6+
]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.shortcuts import render
2+
from .forms import ImageForm
3+
from PIL import Image
4+
from django.conf import settings
5+
import os
6+
from django.http import HttpResponse
7+
8+
def index(request):
9+
if request.method == 'POST':
10+
form = ImageForm(request.POST, request.FILES)
11+
if form.is_valid():
12+
image = form.cleaned_data['image']
13+
original_path = os.path.join(settings.MEDIA_ROOT, image.name)
14+
with open(original_path, 'wb+') as destination:
15+
for chunk in image.chunks():
16+
destination.write(chunk)
17+
img = Image.open(original_path)
18+
compressed_name = 'compressed_' + image.name
19+
compressed_path = os.path.join(settings.MEDIA_ROOT, compressed_name)
20+
if img.format == 'JPEG':
21+
img.save(compressed_path, 'JPEG', quality=50)
22+
else:
23+
img.save(compressed_path)
24+
return render(request, 'compressor/index.html', {'form': form, 'compressed_url': '/media/' + compressed_name})
25+
else:
26+
form = ImageForm()
27+
return render(request, 'compressor/index.html', {'form': form})

image_compressor/image_compressor/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
from django.core.asgi import get_asgi_application
4+
5+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "image_compressor.settings")
6+
7+
application = get_asgi_application()

0 commit comments

Comments
 (0)