How do you resize an image in python pil?
View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the
same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.
Image Used: Python3
Output: Another example:Here we use the different newsize value. Python3
Output: Asked 13 years, 10 months ago Viewed 999k times Is there an obvious way to do this that I'm missing? I'm just trying to make thumbnails.
Ali Afshar 40.1k12 gold badges90 silver badges109 bronze badges asked Nov 7, 2008 at 23:08
saturdayplacesaturdayplace 8,0608 gold badges34 silver badges39 bronze badges 3 Define a maximum size. Then, compute a resize ratio by taking The proper size is There is of course also a library method to do this: the method
oberbaum 2,4547 gold badges36 silver badges52 bronze badges answered Nov 7, 2008 at 23:15
12 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. It does this by determining what percentage 300 pixels is of the original width (img.size[0]) and then multiplying the original height (img.size[1]) by that percentage. Change "basewidth" to any other number to change the default width of your images.
rubo77 18.4k28 gold badges127 silver badges217 bronze badges answered Jan 16, 2009 at 19:12
tomvontomvon 4,9113 gold badges21 silver badges16 bronze badges 10 I also recommend using PIL's thumbnail method, because it removes all the ratio hassles from you. One important hint, though: Replace
with
by default, PIL uses the Image.NEAREST filter for resizing which results in good performance, but poor quality. answered Jun 2, 2009 at 16:03
FranzFranz 1,2448 silver badges2 bronze badges 1 Based in @tomvon, I finished using the following (pick your case): a) Resizing height (I know the new width, so I need the new height)
b) Resizing width (I know the new height, so I need the new width)
Then just:
answered May 14, 2015 at 0:37
muZkmuZk 2,72820 silver badges22 bronze badges 5 If you are trying to maintain the same aspect ratio, then wouldn't you resize by some percentage of the original size? For example, half the original size
answered Dec 13, 2008 at 3:43 5
BenT 2,8843 gold badges17 silver badges36 bronze badges answered Mar 16, 2016 at 13:17
3 You can combine PIL's For instance, if you want to resize an image so that its height is no more than 100px, while keeping aspect ratio, you can do something like this:
Keep in mind that EDIT:
answered Jan 26, 2021 at 15:40
Vito GentileVito Gentile 12.5k9 gold badges58 silver badges91 bronze badges 3
I use this library:
answered Sep 27, 2015 at 10:02
guettliguettli 24k66 gold badges308 silver badges581 bronze badges 1 If you don't want / don't have a need to open image with Pillow, use this:
answered Jan 14, 2019 at 6:05
hoohoo-bhoohoo-b 1,05111 silver badges11 bronze badges Just updating this question with a more modern wrapper This library wraps Pillow (a fork of PIL) https://pypi.org/project/python-resize-image/ Allowing you to do something like this :-
Heaps more examples in the above link. answered May 15, 2018 at 10:04
ShannessShanness 3552 silver badges10 bronze badges 1 Open your image file
Use PIL Image.resize(size, resample=0) method, where you substitute (width, height) of your image for the size 2-tuple. This will display your image at original size:
This will display your image at 1/2 the size:
This will display your image at 1/3 the size:
This will display your image at 1/4 the size:
etc etc answered Feb 18, 2020 at 4:40
user391339user391339 7,82713 gold badges52 silver badges71 bronze badges 2 I will also add a version of the resize that keeps the aspect ratio fixed. In this case, it will adjust the height to match the width of the new image, based on the initial aspect ratio, asp_rat, which is float (!). But, to adjust the width to the height, instead, you just need to comment one line and uncomment the other in the else loop. You will see, where. You do not need the semicolons (;), I keep them just to remind myself of syntax of languages I use more often.
And, it is done. I tried to document it as much as I can, so it is clear. I hope it might be helpful to someone out there! answered May 2, 2020 at 22:18
RockOGOlicRockOGOlic 1501 silver badge7 bronze badges 0 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). 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:
answered Jan 22, 2019 at 18:02
noEmbryonoEmbryo 2,0862 gold badges9 silver badges14 bronze badges Have updated the answer above by "tomvon"
answered Sep 18, 2019 at 10:17
1 A simple method for keeping constrained ratios and passing a max width / height. Not the prettiest but gets the job done and is easy to understand:
Here's a python script that uses this function to run batch image resizing. answered May 31, 2018 at 6:40
AlexAlex 10.7k6 gold badges61 silver badges71 bronze badges I resizeed the image in such a way and it's working very well
answered Apr 8, 2019 at 19:20
To make the new image half the width and half the height of the original image, Use below code :
To resize with fixed width with ration:
answered Nov 16, 2021 at 13:04
1 My ugly example. Function get file like: "pic[0-9a-z].[extension]", resize them to 120x120, moves section to center and save to "ico[0-9a-z].[extension]", works with portrait and landscape:
answered May 22, 2013 at 10:48
NipsNips 12.4k23 gold badges60 silver badges100 bronze badges The simplest way that worked for me
Example
answered Sep 18, 2021 at 7:32
The following script creates nice thumbnails of all JPEG images preserving aspect ratios with 128x128 max resolution.
answered Nov 4, 2020 at 13:19
Riz.KhanRiz.Khan 3685 silver badges8 bronze badges
answered Jan 15 at 7:00
max 3,6962 gold badges8 silver badges24 bronze badges answered Aug 3, 2020 at 5:48
1 How do you resize an image using Python PIL?resize() Returns a resized copy of this image.. Syntax: Image.resize(size, resample=0). Parameters:. size – The requested size in pixels, as a 2-tuple: (width, height).. resample – An optional resampling filter. This can be one of PIL. Image. NEAREST (use nearest neighbour), PIL. Image. ... . Returns type: An Image object.. How do I resize an image using PIL and maintain its aspect ratio?How to Resize Images in a Fixed Aspect Ratio with Python's PIL Library. First line: Determine a fixed number for creating the ratio.. Second line: Open the image.. Third line: Create the height percent according to your fixed value.. Fourth line: Create the image width by multiplying it with the proportioned height value.. How do I resize and save an image in Python?Practical Data Science using Python
The Image module from pillow library has an attribute size. This tuple consists of width and height of the image as its elements. To resize an image, you call the resize() method of pillow's image class by giving width and height.
How do I resize an image in Python using Matplotlib?To resize an image, we will first read the image using the imread() function and resize it using the resize() function as shown below. The imread() returns an array that stores the image. We resize it with the resize() function.
|