70 likes | 186 Views
Sometimes you may need to resize image in Python. Here is how to do this using Python Image Library.#python #image <br><br>Visit https://techimbo.com/how-to-resize-image-in-python/
E N D
Install Python Image Library For this purpose, we will use Python Image Library (PIL). You can install it with the following command. $ pip install Pillow
Create Python Script Run the following command to create a new python script $ vi test.py
Add Code to Resize Image Add the following code to your python script to resize image #!/usr/bin/env python import os, sys from PIL import Image size = 128, 128 for infile in sys.argv[1:]: outfile = os.path.splitext(infile)[0] + ".thumbnail" if infile != outfile: try: im = Image.open(infile) im.thumbnail(size, Image.Resampling.LANCZOS) im.save(outfile, "JPEG") except IOError: print "cannot create thumbnail for '%s'" % infile
Make Script Executable Make the python script executable with the following command $ sudo chmod +x test.py
Run Script Run python script with following command. Provide path to image file as first argument. $ python test.py /path/to/image
Thank You Visit for details https://techimbo.com/how-to-resize-image-in-python/