Home EngineerLab works #1 – The resizing image Python script

Lab works #1 – The resizing image Python script

by Quy Ta
2 minutes read

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.

    # I use conda to create the environment, python 3.11 is to be used
    conda create -n resize-images python=3.11
    
    # install the dependencies
    pip install pillow-heif==0.16.0

    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

    img_dir = 'images'
    files = [f for f in listdir(img_dir) if isfile(join(img_dir, f))]
    files = [f for f in files if f.split('.')[-1] == 'HEIC']

    Create output directories

    jpg_out_dir = 'outputs/jpeg_original'
    os.makedirs(jpg_out_dir, exist_ok=True)
    
    jpg_resized_out_dir = 'outputs/jpeg_resized'
    os.makedirs(jpg_resized_out_dir, exist_ok=True)

    Convert HEIC to JPG

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

    for fn in files:
      fp = img_dir + '/' + fn
      # convert heic to jpg
      heic_img = Image.open(fp)
      jpg_fn = jpg_out_dir + '/' + fn.split('.')[0] + '.jpg'
      heic_img.save(jpg_fn, format="JPEG", optimize = True, quality = 100)

    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

      # resize jpg
      jpg_resized_fn = jpg_resized_out_dir + '/' + fn.split('.')[0] + '.jpg'
      img = Image.open(jpg_fn)
      img.thumbnail((maxed_height, maxed_height))
      img.save(jpg_resized_fn)

    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

    You may also like