Rohit Pandey
Rohit Pandey

Reputation: 2681

PIL zoom into image at a particular point

I'm creating some images with python imaging library (PIL). Now, like we zoom into a map at a particular location, I want to similarly zoom into my image at a specified point. Note that this is different from resizing the image. I want the size to remain the same. I couldn't find any inbuilt method in the documentation that does this. Is anyone aware of a method that might achieve this. I'd ideally like to do this without other dependencies like openCV.

Upvotes: 6

Views: 11017

Answers (1)

Douwe Osinga
Douwe Osinga

Reputation: 116

I think you mean this:

def zoom_at(img, x, y, zoom):
    w, h = img.size
    zoom2 = zoom * 2
    img = img.crop((x - w / zoom2, y - h / zoom2, 
                    x + w / zoom2, y + h / zoom2))
    return img.resize((w, h), Image.LANCZOS)

This will crop the image around the point where you zoom into and then upscale the resulting image to the original size.

Upvotes: 8

Related Questions