How do you resize an image using python pil?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • 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.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.BILINEAR [linear interpolation], PIL.Image.BICUBIC [cubic spline interpolation], or PIL.Image.LANCZOS [a high-quality downsampling filter]. If omitted, or if the image has mode “1” or “P”, it is set PIL.Image.NEAREST.
    Returns type: An Image object.
     

    Image Used: 
     

    Python3

    from PIL import Image

    im = Image.open[r"C:\Users\System-Pc\Desktop\ybear.jpg"]

    width, height = im.size

    left = 4

    top = height / 5

    right = 154

    bottom = 3 * height / 5

    im1 = im.crop[[left, top, right, bottom]]

    newsize = [300, 300]

    im1 = im1.resize[newsize]

    im1.show[]

    Output: 
     

    Another example:Here we use the different newsize value.
     

    Python3

    from PIL import Image

    im = Image.open[r"C:\Users\System-Pc\Desktop\ybear.jpg"]

    width, height = im.size

    left = 6

    top = height / 4

    right = 174

    bottom = 3 * height / 4

    im1 = im.crop[[left, top, right, bottom]]

    newsize = [200, 200]

    im1 = im1.resize[newsize]

    im1.show[]

    Output: 
     


    Asked 13 years, 11 months ago

    Viewed 1.0m 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 min[maxwidth/width, maxheight/height].

    The proper size is oldsize*ratio.

    There is of course also a library method to do this: the method Image.thumbnail.
    Below is an [edited] example from the PIL documentation.

    import os, sys
    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.ANTIALIAS]
                im.save[outfile, "JPEG"]
            except IOError:
                print "cannot create thumbnail for '%s'" % infile
    

    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.

    from PIL import Image
    
    basewidth = 300
    img = Image.open['somepic.jpg']
    wpercent = [basewidth/float[img.size[0]]]
    hsize = int[[float[img.size[1]]*float[wpercent]]]
    img = img.resize[[basewidth,hsize], Image.ANTIALIAS]
    img.save['somepic.jpg']
    

    rubo77

    18.5k28 gold badges127 silver badges218 bronze badges

    answered Jan 16, 2009 at 19:12

    tomvontomvon

    4,9013 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

    im.thumbnail[size]
    

    with

    im.thumbnail[size,Image.ANTIALIAS]
    

    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,2548 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]

    new_width  = 680
    new_height = new_width * height / width 
    

    b] Resizing width [I know the new height, so I need the new width]

    new_height = 680
    new_width  = new_height * width / height
    

    Then just:

    img = img.resize[[new_width, new_height], Image.ANTIALIAS]
    

    answered May 14, 2015 at 0:37

    muZkmuZk

    2,74820 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

    half = 0.5
    out = im.resize[ [int[half * s] for s in im.size] ]
    

    answered Dec 13, 2008 at 3:43

    5

    from PIL import Image
    
    img = Image.open['/your image path/image.jpg'] # image extension *.png,*.jpg
    new_width  = 200
    new_height = 300
    img = img.resize[[new_width, new_height], Image.ANTIALIAS]
    img.save['output image name.png'] # format may what you want *.png, *jpg, *.gif
    

    BenT

    2,8843 gold badges17 silver badges36 bronze badges

    answered Mar 16, 2016 at 13:17

    3

    You can combine PIL's Image.thumbnail with sys.maxsize if your resize limit is only on one dimension [width or height].

    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:

    import sys
    from PIL import Image
    
    image.thumbnail[[sys.maxsize, 100], Image.ANTIALIAS]
    

    Keep in mind that Image.thumbnail will resize the image in place, which is different from Image.resize that instead returns the resized image without changing the original one.

    EDIT: Image.ANTIALIAS raises a deprecation warning, and will be removed in PIL 10 [July 2023]. Instead, you should use Resampling.LANCZOS:

    import sys
    from PIL import Image
    from PIL.Image import Resampling
    
    image.thumbnail[[sys.maxsize, 100], Resampling.LANCZOS]
    

    answered Jan 26, 2021 at 15:40

    Vito GentileVito Gentile

    12.5k9 gold badges58 silver badges91 bronze badges

    3

    from PIL import Image
    from resizeimage import resizeimage
    
    def resize_file[in_file, out_file, size]:
        with open[in_file] as fd:
            image = resizeimage.resize_thumbnail[Image.open[fd], size]
        image.save[out_file]
        image.close[]
    
    resize_file['foo.tif', 'foo_small.jpg', [256, 256]]
    

    I use this library:

    pip install python-resize-image
    

    answered Sep 27, 2015 at 10:02

    guettliguettli

    23.6k66 gold badges308 silver badges583 bronze badges

    1

    If you don't want / don't have a need to open image with Pillow, use this:

    from PIL import Image
    
    new_img_arr = numpy.array[Image.fromarray[img_arr].resize[[new_width, new_height], Image.ANTIALIAS]]
    

    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] //pypi.org/project/python-resize-image/

    Allowing you to do something like this :-

    from PIL import Image
    from resizeimage import resizeimage
    
    fd_img = open['test-image.jpeg', 'r']
    img = Image.open[fd_img]
    img = resizeimage.resize_width[img, 200]
    img.save['test-image-width.jpeg', img.format]
    fd_img.close[]
    

    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

    from PIL import Image
    im = Image.open["image.png"]
    

    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:

    display[im.resize[[int[im.size[0]],int[im.size[1]]], 0] ]
    

    This will display your image at 1/2 the size:

    display[im.resize[[int[im.size[0]/2],int[im.size[1]/2]], 0] ]
    

    This will display your image at 1/3 the size:

    display[im.resize[[int[im.size[0]/3],int[im.size[1]/3]], 0] ]
    

    This will display your image at 1/4 the size:

    display[im.resize[[int[im.size[0]/4],int[im.size[1]/4]], 0] ]
    

    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.

    from PIL import Image
    
    img_path = "filename.png";
    img = Image.open[img_path];     # puts our image to the buffer of the PIL.Image object
    
    width, height = img.size;
    asp_rat = width/height;
    
    # Enter new width [in pixels]
    new_width = 50;
    
    # Enter new height [in pixels]
    new_height = 54;
    
    new_rat = new_width/new_height;
    
    if [new_rat == asp_rat]:
        img = img.resize[[new_width, new_height], Image.ANTIALIAS]; 
    
    # adjusts the height to match the width
    # NOTE: if you want to adjust the width to the height, instead -> 
    # uncomment the second line [new_width] and comment the first one [new_height]
    else:
        new_height = round[new_width / asp_rat];
        #new_width = round[new_height * asp_rat];
        img = img.resize[[new_width, new_height], Image.ANTIALIAS];
    
    # usage: resize[[x,y], resample]
    # resample filter -> PIL.Image.BILINEAR, PIL.Image.NEAREST [default], PIL.Image.BICUBIC, etc..
    # //pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.resize
    
    # Enter the name under which you would like to save the new image
    img.save["outputname.png"];
    

    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].
    And there was always the possibility of a portrait video...
    The Image.thumbnail method was promising, but I could not make it upscale a smaller image.

    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
    
    def get_resized_img[img_path, video_size]:
        img = Image.open[img_path]
        width, height = video_size  # these are the MAX dimensions
        video_ratio = width / height
        img_ratio = img.size[0] / img.size[1]
        if video_ratio >= 1:  # the video is wide
            if img_ratio = video_ratio:  # image is not tall enough
                height_new = int[width / img_ratio]
                size_new = width, height_new
            else:  # image is taller than video
                width_new = int[height * img_ratio]
                size_new = width_new, height
        return img.resize[size_new, resample=Image.LANCZOS]
    

    answered Jan 22, 2019 at 18:02

    noEmbryonoEmbryo

    2,0862 gold badges9 silver badges14 bronze badges

    Have updated the answer above by "tomvon"

    from PIL import Image
    
    img = Image.open[image_path]
    
    width, height = img.size[:2]
    
    if height > width:
        baseheight = 64
        hpercent = [baseheight/float[img.size[1]]]
        wsize = int[[float[img.size[0]]*float[hpercent]]]
        img = img.resize[[wsize, baseheight], Image.ANTIALIAS]
        img.save['resized.jpg']
    else:
        basewidth = 64
        wpercent = [basewidth/float[img.size[0]]]
        hsize = int[[float[img.size[1]]*float[wpercent]]]
        img = img.resize[[basewidth,hsize], Image.ANTIALIAS]
        img.save['resized.jpg']
    

    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:

    def resize[img_path, max_px_size, output_folder]:
        with Image.open[img_path] as img:
            width_0, height_0 = img.size
            out_f_name = os.path.split[img_path][-1]
            out_f_path = os.path.join[output_folder, out_f_name]
    
            if max[[width_0, height_0]]  height_0:
                wpercent = max_px_size / float[width_0]
                hsize = int[float[height_0] * float[wpercent]]
                img = img.resize[[max_px_size, hsize], Image.ANTIALIAS]
                print['writing {} to disk'.format[out_f_path]]
                img.save[out_f_path]
                return
    
            if width_0 < height_0:
                hpercent = max_px_size / float[height_0]
                wsize = int[float[width_0] * float[hpercent]]
                img = img.resize[[max_px_size, wsize], Image.ANTIALIAS]
                print['writing {} to disk'.format[out_f_path]]
                img.save[out_f_path]
                return
    

    Here's a python script that uses this function to run batch image resizing.

    answered May 31, 2018 at 6:40

    AlexAlex

    10.8k6 gold badges61 silver badges71 bronze badges

    I resizeed the image in such a way and it's working very well

    from io import BytesIO
    from django.core.files.uploadedfile import InMemoryUploadedFile
    import os, sys
    from PIL import Image
    
    
    def imageResize[image]:
        outputIoStream = BytesIO[]
        imageTemproaryResized = imageTemproary.resize[ [1920,1080], Image.ANTIALIAS] 
        imageTemproaryResized.save[outputIoStream , format='PNG', quality='10'] 
        outputIoStream.seek[0]
        uploadedImage = InMemoryUploadedFile[outputIoStream,'ImageField', "%s.jpg" % image.name.split['.'][0], 'image/jpeg', sys.getsizeof[outputIoStream], None]
    
        ## For upload local folder
        fs = FileSystemStorage[]
        filename = fs.save[uploadedImage.name, uploadedImage]
    

    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 :

      from PIL import Image
      im = Image.open["image.jpg"]
      resized_im = im.resize[[round[im.size[0]*0.5], round[im.size[1]*0.5]]]
        
      #Save the cropped image
      resized_im.save['resizedimage.jpg']
    

    To resize with fixed width with ration:

    from PIL import Image
    new_width = 300
    im = Image.open["img/7.jpeg"]
    concat = int[new_width/float[im.size[0]]]
    size = int[[float[im.size[1]]*float[concat]]]
    resized_im = im.resize[[new_width,size], Image.ANTIALIAS]
    #Save the cropped image
    resized_im.save['resizedimage.jpg']
    

    # Importing Image class from PIL module
    from PIL import Image
    
    # Opens a image in RGB mode
    im = Image.open[r"C:\Users\System-Pc\Desktop\ybear.jpg"]
    
    # Size of the image in pixels [size of original image]
    # [This is not mandatory]
    width, height = im.size
    
    # Setting the points for cropped image
    left = 4
    top = height / 5
    right = 154
    bottom = 3 * height / 5
    
    # Cropped image of above dimension
    # [It will not change original image]
    im1 = im.crop[[left, top, right, bottom]]
    newsize = [300, 300]
    im1 = im1.resize[newsize]
    # Shows the image in image viewer
    im1.show[]
    

    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:

    def imageResize[filepath]:
        from PIL import Image
        file_dir=os.path.split[filepath]
        img = Image.open[filepath]
    
        if img.size[0] > img.size[1]:
            aspect = img.size[1]/120
            new_size = [img.size[0]/aspect, 120]
        else:
            aspect = img.size[0]/120
            new_size = [120, img.size[1]/aspect]
        img.resize[new_size].save[file_dir[0]+'/ico'+file_dir[1][3:]]
        img = Image.open[file_dir[0]+'/ico'+file_dir[1][3:]]
    
        if img.size[0] > img.size[1]:
            new_img = img.crop[ [
                [[[img.size[0]]-120]/2],
                0,
                120+[[[img.size[0]]-120]/2],
                120
            ] ]
        else:
            new_img = img.crop[ [
                0,
                [[[img.size[1]]-120]/2],
                120,
                120+[[[img.size[1]]-120]/2]
            ] ]
    
        new_img.save[file_dir[0]+'/ico'+file_dir[1][3:]]
    

    answered May 22, 2013 at 10:48

    NipsNips

    12.4k23 gold badges60 silver badges100 bronze badges

    The simplest way that worked for me

    image = image.resize[[image.width*2, image.height*2], Image.ANTIALIAS]
    

    Example

    from PIL import Image, ImageGrab
    image = ImageGrab.grab[bbox=[0,0,400,600]] #take screenshot
    image = image.resize[[image.width*2, image.height*2], Image.ANTIALIAS]
    image.save['Screen.png']
    

    answered Sep 18, 2021 at 7:32

    The following script creates nice thumbnails of all JPEG images preserving aspect ratios with 128x128 max resolution.

    from PIL import Image
    img = Image.open["D:\\Pictures\\John.jpg"]
    img.thumbnail[[680,680]]
    img.save["D:\\Pictures\\John_resize.jpg"]
    

    answered Nov 4, 2020 at 13:19

    Riz.KhanRiz.Khan

    3685 silver badges8 bronze badges

    ######get resize coordinate after resize the image using this function#####
    def scale_img_pixel[points,original_dim,resize_dim]:
            multi_list = [points]
            new_point_list = []
            multi_list_point = []
            for point in multi_list:
                multi_list_point.append[[point[0],point[1]]]
                multi_list_point.append[[point[2],point[3]]]
            for lsingle_point in multi_list_point:
                x1 = int[[lsingle_point[0] * [resize_dim[0] / original_dim[0]]]]
                y1 = int[[lsingle_point[1] * [resize_dim[1] / original_dim[1]]]]
                new_point_list.append[x1]
                new_point_list.append[y1]
                
            return new_point_list
        
        
        points = [774,265,909,409]
        original_dim = [1237,1036]
        resize_dim = [640,480]
        result = scale_img_pixel[points,original_dim,resize_dim]
        print["result: ", result]  
    

    answered Jan 15 at 7:00

    import cv2
    from skimage import data 
    import matplotlib.pyplot as plt
    from skimage.util import img_as_ubyte
    from skimage import io
    filename='abc.png'
    image=plt.imread[filename]
    im=cv2.imread['abc.png']
    print[im.shape]
    im.resize[300,300]
    print[im.shape]
    plt.imshow[image]
    

    max

    3,6962 gold badges8 silver badges24 bronze badges

    answered Aug 3, 2020 at 5:48

    1

    How do you resize an image in 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 can I get image size using PIL?

    How to get the size of an image with PIL in Python.
    image = PIL. Image. open["sample.png"] image to open..
    width, height = image. size. extract width and height from output tuple..
    print[width, height].

    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..

    Which function is used to resize the image in Python?

    Wand resize[] function in Python While Scaling Up refers to increase dimensions of an image, and making image size larger. resize[] function is used in order to Resize an image.

    Chủ Đề