This will display your image at 1/2 the size: display(im.resize((int(im.size/2),int(im.size/2)), 0) ) This will display your image at original size: display(im.resize((int(im.size),int(im.size)), 0) ) Use PIL Image.resize(size, resample=0) method, where you substitute (width, height) of your image for the size 2-tuple. Open your image file from PIL import Image Return img.resize(size_new, resample=Image.LANCZOS) If img_ratio = video_ratio: # image is not tall enough Width, height = video_size # these are the MAX dimensions So after I couldn't find an obvious way to do that here (or at some other places), I wrote this function and put it here for the ones to come: from PIL import Imageĭef get_resized_img(img_path, video_size): ![]() The Image.thumbnail method was promising, but I could not make it upscale a smaller image. I was trying to resize some images for a slideshow video and because of that, I wanted not just one max dimension, but a max width and a max height (the size of the video frame).Īnd there was always the possibility of a portrait video. Img.save('test-image-width.jpeg', img.format) This library wraps Pillow (a fork of PIL)Īllowing you to do something like this :- from PIL import Image Just updating this question with a more modern wrapper New_img_arr = numpy.array(omarray(img_arr).resize((new_width, new_height), Image.ANTIALIAS)) If you don't want / don't have a need to open image with Pillow, use this: from PIL import Image I use this library: pip install python-resize-image Image = resizeimage.resize_thumbnail(Image.open(fd), size) Img.save('output image name.png') # format may what you want *.png, *jpg, *.gifĭef resize_file(in_file, out_file, size): Img = img.resize((new_width, new_height), Image.ANTIALIAS) Img = Image.open('/your image path/image.jpg') # image extension *.png,*.jpg If you are trying to maintain the same aspect ratio, then wouldn't you resize by some percentage of the original size?įor example, half the original size half = 0.5 Then just: img = img.resize((new_width, new_height), Image.ANTIALIAS) ![]() One important hint, though: Replace im.thumbnail(size)īy default, PIL uses the Image.NEAREST filter for resizing which results in good performance, but poor quality.īased in I finished using the following (pick your case):Ī) Resizing height ( I know the new width, so I need the new height) new_width = 680ī) Resizing width ( I know the new height, so I need the new width) new_height = 680 I also recommend using PIL's thumbnail method, because it removes all the ratio hassles from you. Img = img.resize((basewidth,hsize), Image.ANTIALIAS) ![]() Hsize = int((float(img.size)*float(wpercent))) Change "basewidth" to any other number to change the default width of your images. It does this by determining what percentage 300 pixels is of the original width (img.size) and then multiplying the original height (img.size) by that percentage. ![]() This script will resize an image (somepic.jpg) using PIL (Python Imaging Library) to a width of 300 pixels and a height proportional to the new width. Print "cannot create thumbnail for '%s'" % infile Outfile = os.path.splitext(infile) + ".thumbnail" There is of course also a library method to do this: the method Image.thumbnail.īelow is an (edited) example from the PIL documentation. Then, compute a resize ratio by taking min(maxwidth/width, maxheight/height).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |