Resize image using Python image library (PIL)


#   Resize an image, keeping the aspect ratio.
#   Uses the Python image library (PIL).
#
#   https://pillow.readthedocs.io/en/3.0.x/handbook/tutorial.html
#   pip install python-resize-image
#
#   Written by:  Mark Kiehl
#   http://mechatronicsolutionsllc.com/


def bShrinkImage(sFilePathImageIn, tSizeOut, sFilePathImageOut, bVerbose):
    """ Shrink image sFilePathImageIn to fit within tSizeOut, 
        keeping the original aspect ratio, and saving it to
	sFilePathImageOut.  sFilePathImageOut may be the same 
	as sFilePathImageIn (overwrites file).
	If tSizeOut is larger than the original, returns FALSE,
	but it will still write the original file to sFilePathImageOut,
	but with the original size. 
	(note that expanding an image is very complicated, and 
	 involves carefully considered consequences.)
	Requires Python Image Library (PIL) module
	(from PIL import Image)
    """
    if bVerbose == True: print 'Requested size: {} x {}  {:.5}'.format(tSizeOut[0], tSizeOut[1], float(tSizeOut[0])/float(tSizeOut[1]))
    #Use method .resize_thumbnail to shrink an image (keeping the aspect ratio)
    from resizeimage import resizeimage
    with open(sFilePathImageIn, 'r+b') as f:
        with Image.open(f) as image:
            if bVerbose == True: print 'Original size: {} x {}  {:.5}'.format(image.size[0],image.size[1], float(image.size[0])/float(image.size[1]))
            tSizeIn = (image.size[0],image.size[1])
            img_resize = resizeimage.resize_thumbnail(image, tSizeOut)
            img_resize.save(sFilePathImageOut, image.format)
    with open(sFilePathImageOut, 'r+b') as f:
        with Image.open(f) as image:
            if bVerbose == True: print 'Final size: {} x {}  {:.5}'.format(image.size[0],image.size[1], float(image.size[0])/float(image.size[1]))
    if min(tSizeOut) > min(tSizeIn):
        return False
    else:
        return True



# Time the script execution
import time
tStart = time.time()

import os
from shutil import *
import sys

# Change the current (working) directory to where the script is
os.chdir(os.path.split(sys.argv[0])[0])
#if bVerbose == True: print 'current folder: ' + os.getcwd()

sFilePathImageIn = os.getcwd() + r'\images\logo - Nordic Tug 37 + KnotWorkin.png'
#sFilePathImageIn = os.getcwd() + r'\images\Compass_rose.png'
if not os.path.exists(sFilePathImageIn): print 'ERROR - file not found {}'.format(sFilePathImageIn)

# create a new file name for saving the result
sFilePathImageOut = "%s%s%s" % (os.path.dirname(sFilePathImageIn), '\\', 'resized.png')
#print 'sFilePathImageOut = "{}" '.format(sFilePathImageOut)

import PIL
from PIL import Image
from resizeimage import resizeimage

#Resize the image and keep the original aspect ratio using .thumbnail() method
#An image aspect ratio is the relationship between the image width and height; [width]:[height]
tSizeOut = (281, 600)
#tSizeOut = (300, 231)
#tSizeOut = (800, 300)

#def bShrinkImage(sFilePathImageIn, tSizeOut, sFilePathImageOut, bVerbose):
bResult = bShrinkImage(sFilePathImageIn, tSizeOut, sFilePathImageOut, True)
if bResult == False: print 'The new file has NOT been resized, it could not be expanded'


tElapsed = time.time() - tStart
print 'Executed script in {:.2} sec'.format(tElapsed)