Resize image using Python

walden systems, geeks corner, programming, languages, Python, scope, local scope, global scope, hoisting, functions, variables, developer, scripting, decrypt, aes256, encrypt, pycrypto, pip, tuple, dictionary, list, threading, multithreading, image
Python is a programming language that lets you work quickly and integrate systems more effectively.

Using the Python Image Library (PIL) you can resize an image. Several filters can be specified. ANTIALIAS is best for decreasing the size, the other filters work better with increasing the size. In this code snippet one image of each filter option is saved.

 1 import Image
 2 # open an image file (.bmp,.jpg,.png,.gif)
 3 imageFileName = "SamplePicture.jpg"
 4 img = Image.open(imageFileName)
 5 # new dimensions of image
 6 width = 500
 7 height = 420
 8 # Different filter options to resize the image
 9 img2 = img.resize((width, height), Image.NEAREST)      # use nearest neighbor
10 img3 = img.resize((width, height), Image.BILINEAR)     # linear interpolation in a 2x2 environment
11 img4 = img.resize((width, height), Image.BICUBIC)      # cubic spline interpolation in a 4x4 environment
12 img5 = img.resize((width, height), Image.ANTIALIAS)    # best down-sizing filter
13 extenstion = ".jpg"
14 img2.save("imageNearest" + extension)
15 img3.save("imageBilinear" + extension)
16 img4.save("imageBicubic" + extension)
17 img5.save("imageAntiAlias" + extension)

The above code won't keep the aspect ratio so if you want to resize image and keep the aspect ratio, we have to modify the code. We have to get the ratio of the current image and compare it to the resize ratio. If the original ratio is greater than proposed ratio, we use keep the new height and adjust the width accordingly. When calculating ratios, we have to cast one of the dimensions to float because Python returns integer when doing integer arithmetic. After we recalculate the new dimensions, we will have to cast the new dimensions to integer since image.resizer expects integers for height and width. The modified code snippet is set below

 1 import Image
 2 # open an image file (.bmp,.jpg,.png,.gif)
 3 imageFileName = "SamplePicture.jpg"
 4 img = Image.open(imageFileName)
 5 
 6 # gets the current width and height of original image
 7 orig_width, orig_height = img.size
 8 # Now we get the ratio.  We cast integer to float to
 9 # produce a float result
10 orig_ratio = ( ( float( orig_width ) ) / orig_height )
11 
12 # new dimensions of image
13 width = 600
14 height = 400
15 
16 # We now calculate the new proposed ratio
17 l_new_ratio = ( ( float( p_width ) ) / p_height )
18 # Set the aspect ratio to the largest image possible
19 if ( new_ratio < orig_ratio ) :
20     height = ( float( width ) * orig_height / orig_width )
21 else :
22     width = height * orig_ratio
23 # cast the new width and height to int since image.resizer takes integer values
24 width = int( width )
25 height = int( height )
26 
27 # Different filter options to resize the image
28 img2 = img.resize((width, height), Image.NEAREST)      # use nearest neighbor
29 img3 = img.resize((width, height), Image.BILINEAR)     # linear interpolation in a 2x2 environment
30 img4 = img.resize((width, height), Image.BICUBIC)      # cubic spline interpolation in a 4x4 environment
31 img5 = img.resize((width, height), Image.ANTIALIAS)    # best down-sizing filter
32 extenstion = ".jpg"
33 img2.save("imageNearest" + extension)
34 img3.save("imageBilinear" + extension)
35 img4.save("imageBicubic" + extension)
36 img5.save("imageAntiAlias" + extension)