contact@digitalnotebooks.co
Stay Organized, Stay Inspired. Let's Write.

Image Compression 25: Setup, Model & Logic

Back To All Notes
  • Notebook: Django: Automating Common Tasks
  • Speaker: Udemy Instructor Rathan Kumar
  • Date Created: Aug. 30, 2024, 6:25 p.m.
  • Owner: Rosilie

1. Go to Pillow documentation and install the Django package in the Djngo server terminal and in Celery terminal

$ pip install pillow

2. In the Django server, we create a new app IMAGE_COMPRESSION

$ python manage.py startapp image_compression

3.  Register the new app in SETTINGS.PY INSTALLED_APPS

4. Create the model in MODELS.PY

To make the QUALITY field a drop-down option, we use the SELECT TAG.  Where we see the VALUE AND THE LABEL (which is displayed as a user option in the dropdown)

5.  Register the model for our ADMIN panel. Update ADMIN.PY:

6. Make the necessary model migrations

$ python manage.py makemigrations

$ python manage.py migrate

7. Create the new URL pattern. Since we have a new app, we need to create a new pattern in our main project's URLS.PY.

Then, create a new URLS.PY file in our new app for image-compression-related URL paths.

8. Create the function in the VIEWS.PY

9. Create a new FOLDER called IMAGE_COMPRESSION, and in this folder, create a new file, COMPRESS.HTML

10. Update the HOME.HTML to call this new web page.

11. Create a FORMS.PY 

12. Call our form using our VIEWS.PY:

13. In the COMPRESS.HTML, update as:

14. We can change the label of the field on the form, so update FORMS.PY and add the line:

FROM:

TO:

15.  When we use io.BytesIO, we get bytes value of the image. To see what is the visual representation of these bytes, you can add the EXTENSION 'HEX EDITOR'.

Now open an image file. On the tab of this image, right-click, select 'REOPEN EDITOR WITH', and select HEXEDITOR and you will see the HEX value of the file.

16. We use BUFFER.SEEK(0) to make sure that after we save, we set our curser position back to 0.

17. The VIEWS.PY shall be:

18. Checking our ADMIN panel

19. To set the image format to any format not just JPEG, we update our VIEWS.PY AS:



No PDF file attached.

Notebook
Notebook Details
Title: Django: Automating Common Tasks
Category: Coding