Search code examples
pythonimageheightwidthimage-resizing

Adjusting only the width of a picture


Say an image with size (width = 200, height = 250)

I want to change the size to (width = 400, height = 250)

How would that be possible without giving the error "IndexError: image index out of range"?


Solution

  • Use PIL:

    from PIL import Image
    im=Image.open(filename)
    im2=im.resize((400,250))
    im2.save(filename)
    

    If currently, saves as new image, but if want to display it, do:

    im2.show()
    

    Easy as that.

    PIL.Image.resize does it.

    Related:

    See the docs

    See the docs about this

    Note for second argument, which has a default parameter (0), it's using NearestNeighbors, if you set it to 1 it's LANCZOS, if you set it to 2 it's BILINEAR, if you set it to 3 it's BICUBIC

    So see what you like the most :-)