training_exercise_1.0.0
Ok everyone, on to training exercise 1.0.0 – how to re-size jpeg and jpg images
Let’s start with something completely functional. This will be needed by anyone that ever SENDS EMAILS WITH PHOTO ATTACHMENTS (aka EVERYONE!) or works with web developers, mobile app developers, photographers, real estate agents, hiring managers, and virtually every single business owner that operates and makes revenue, period. Ever. Even the most grizzled anti-technology anti-change old school contractors now send images of their work.
Ever tried to send an email with a really awesome photo that looks great and you want to preserve the quality so you send it in the biggest file size… And what happens? It lingers in the outbox, takes up all your data, does not go through, frustrates you so you re-send it a billion more times and waste all your data, emotional energy, and time- just to get upset! This simple script will show you how to resize a JPEG image.. obviously there are easier and simple ways where you just click within your favorite Adobe or Microsoft image editor. That is not why we are doing this training exercise this way, right now.
The main reason we are doing this exercise is to get you more comfortable and familiar with writing simple scripts of code so that eventually you will be writing programs of your own. First, we are going to download and install via pip the pillow/PIL library by simply entering:
pip install pillow
You need to take a large photo/any size photo that you would like a compressed version of without losing the quality of the photo make it an easy name- if you name it image this script should work perfectly as is if it is a .jpg Now copy or move this photo in the user folder you use for python. This is very important. Now name it image. It needs to have a .jpeg or .jpg file extension for this exact script to work. If it is in .jpeg to start with, simply change the script everywhere it is .jpg to .jpeg
Once you have done that, I want you to write out the code below line by line. Feel free to cut and paste it all once, but we want you to do it line by line. Even if you just copy and paste going line by line that would be better than simply throwing it in the terminal and not knowing how or why it works or does not work. Not because this is the most efficient way to accomplish this, but because when you were just learning your baby mind is absorbing so much that you will get more in the long run out of typing these few lines out or copying and pasting them one by one than just cutting and pasting all of it and not learning to the degree that you would, having done it yourself. This is like writing in cursive for the first time right now if we were learning how to write the upper and lower case As..think Way back to kindergarten first grade maybe. In the future, you will definitely be going faster. As you are now with writing.
python
from PIL import Image
import os
image = Image.open('image.jpg')
width, height = image.size
new_size = (width//2), (height//2)
resized_image = image.resize(new_size)
resized_image.save('compressed_image.jpg', optimize=True, quality=50)
And that is it! there will now be a saved almost-unchanged quality photo that is now about 90% smaller, saved as compressed_image.jpg It is the same image as you used for image.jpg…just way way more efficient! Perfect! now just rename it and send/use it…be sure to double check the file size yourself to see the difference.
Congrats on writing your first script!
Until next time!