Lab works #1 – The resizing image Python script

The script I just made is to solve the problem where I have to resize the photo, that I capture using my iPhone, and convert it from HEIC to JPG. The purpose is to upload the photos to this blog post and to make sure the photos are not taking too much disk space from the server. It’s taking too many steps and if I have many photos it will take time to do replicated stuff. Therefore I decided to write a script that took me only 15 minutes to search for the pieces of the script in Stackoverflow. Below are the steps or thought processes that I want to do to construct the scripts:

  1. List the images: to be read and executed later
  2. Create output directories: one for converted jpg images, another for the resized jpg images
  3. Loop through the input HEIC images, and convert them to JPG
  4. From the outputted JPG, resize and save them.

To understand the codes. First, we need to make sure we have the required dependencies.

Setup the environment

I am using python 3.11 and pillow. Initially I was planning to use OpenCV but Pillow is actually good enough.

    
    
    
    
    

    Read the input files

    Read the input files, in this example I’m going to save the input HEIC images in images directory. Then we should only read the images from there with the extension of HEIC only

    
    
    

    Create output directories

    
    
    
    
    

    Convert HEIC to JPG

    We loop through the file name in the list variable files . Use Pillow to do the magic

    
    
    
    
    
    

    Resize JPG with a ratio

    We want to have width and height no bigger than a specific size then we use Pillow to do the magic also

    
    
    
    
    

    And that is. The output structure of the code directory should be something like this

    The structure of the source code directory with outputs. All the above logic is stored in run.py a file

    I’ve uploaded the script below, feel free to pull it down and use it

    https://github.com/taquy/qt-labs/tree/master/resize-images

    Related posts

    Vietnam Dream

    Diary #21 – Returning to Tennis game

    Diary #18 – Passed the G2 Road Test