Alex
Alex

Reputation: 44275

How to resize an image with pillow?

Using python 3.6.9 and pillow 6.2.1 on a Mac Mojave I am trying to resize an image with pillow. Here is a complete example code:

import numpy
from PIL import Image

data = numpy.zeros((100, 100, 3), dtype=numpy.uint8)
data[50, 51] = [255, 0, 0]
data[50, 52] = [0, 255, 0]
data[50, 53] = [0, 0, 255]

image = Image.fromarray(data)
image.save('orig.png')
image.resize((500,500))
image.save('resized.png')

I expect the image named resized.png to have 5 times the size as the image orig.png, but the images appear to have the same size, and they have the exact same file size.

Is that a bug in the pillow package or what am I missing?

Upvotes: 3

Views: 331

Answers (2)

wookiekim
wookiekim

Reputation: 1176

Try

image = image.resize((500,500))

the resize does not change the image size in-place, it returns the resized image.

Upvotes: 1

Guy
Guy

Reputation: 50809

image.resize returns the new image rather than changing the existing one

def resize(self, size, resample=NEAREST, box=None):
    # ...
    return self._new(self.im.resize(size, resample, box))

You need to save the returned image

image = image.resize((500,500))
image.save('resized.png')

Upvotes: 2

Related Questions