Home > Net >  How can I zoom(rescale) image in python (scikit, numpy, pill)?
How can I zoom(rescale) image in python (scikit, numpy, pill)?

Time:09-21

I need to zoom image 6x. But I use some code and It can zoom only 1.5x. What can I use else? Because if I change from 1.5 to 6 the code doesn't work.

from skimage.io import imread, imsave
from skimage.transform import rescale
from skimage import transform

img = imread('C:/abc.png')
imsave('img.png', img)

image_res = transform.rescale(crop, scale=1.5)
imsave('image_res.png', image_res)

CodePudding user response:

I would use pillow, but instead of using the resize method use the crop method

you need to do some math to know where to crop the image because Pillow doesn't have a final resolution thing, crop takes a 4-value tuple (fromX,fromY,toX,toY)

it should look like this:

from PIL import Image

img = Image.open("filepath")
img.save("img.png","PNG")

zoom = 5

image_res = img.crop((((img.size[0]/2)-img.size[0]/(zoom*2)),((img.size[1]/2)-img.size[1]/(zoom*2)),((img.size[0]/2) img.size[0]/(zoom*2)),((img.size[1]/2) img.size[1]/(zoom*2))))

image_res.save("image_res.png","PNG")

if you want the final image to have the same resolution as the original just use this in a resize method

from PIL import Image

img = Image.open("filepath")
img.save("img.png","PNG")

zoom = 5

image_res = img.resize((img.size[0],img.size[1]),box=(((img.size[0]/2)-img.size[0]/(zoom*2)),((img.size[1]/2)-img.size[1]/(zoom*2)),((img.size[0]/2) img.size[0]/(zoom*2)),((img.size[1]/2) img.size[1]/(zoom*2))))
image_res.save("image_res.png","PNG")
  • Related