Python: converting PIL.Image to nympy array and back, resizing image

The example, I have used today:
import PIL
import numpy
from PIL import Image

def resize_image(numpy_array_image, new_height):
# convert nympy array image to PIL.Image
image
= Image.fromarray(numpy.uint8(numpy_array_image))
old_width
= float(image.size[0])
old_height
= float(image.size[1])
ratio
= float( new_height / old_height)
new_width
= int(old_width * ratio)
image
= image.resize((new_width, new_height), PIL.Image.ANTIALIAS)
# convert PIL.Image into nympy array back again
return array(image)




As an Amazon Associate I earn from qualifying purchases.

My favorite quotations..


“A man should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.”  by Robert A. Heinlein

"We are but habits and memories we chose to carry along." ~ Uki D. Lucas


Popular Recent Articles