在Python PIL中以可设置的中心和比例裁剪图像 [英] Crop image with settable center and scale in Python PIL

查看:1377
本文介绍了在Python PIL中以可设置的中心和比例裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PIL裁剪图像,尽管它可以是其他模块.我需要使用比例因子(即1.5)进行裁剪的方法,这意味着输出将被放大1.5倍.此外,我需要设置缩放的中心.这意味着将x/2,y/2设置为中心将直接缩放到中心,而其他x,y值将放大这些像素.

I would like to crop an image using PIL, although it could be some other module. I need the method to crop with a scale factor, ie 1.5 meaning that the output would be 1.5x zoomed in. Additionally, I would need to set the center where it zooms. This means setting x/2,y/2 as the center would zoom straight to the center, but other x,y values would zoom into those pixels.

如果有人知道该怎么做,我将非常感谢您的帮助.

If anyone knows how to do this I would really appreciate any help.

现在我使用ims = im.crop((int((xx/i)/2),int((yy/i)/2),int((x +(x/i)))进行裁剪/2),int(((y +(y/i))/2)))) 但这只会放大到中心,而"i"没有给出合适的比例因子.

Right now I have some cropping working with ims = im.crop((int((x-x/i)/2), int((y-y/i)/2), int((x+(x/i))/2), int((y+(y/i))/2))) but that only zooms into the center, and "i" doesn't give a nice scale factor.

再次感谢您的帮助.

推荐答案

这仅仅是确定中心和尺寸的问题.

It is just a matter of getting the center and the sizes right.

  1. 确定要修剪的点的中心
  2. 使用比例因子确定新尺寸
  3. 确定裁剪图像的边界框

下面的脚本应该可以解决问题.

The following script should do the trick.

import os.path
from PIL import Image

def get_img_dir():
    src_dir = os.path.dirname(__file__)
    img_dir = os.path.join(src_dir, '..', 'img')
    return img_dir

def open_img():
    img_dir = get_img_dir()
    img_name = 'amsterdam.jpg'
    full_img_path = os.path.join(img_dir, img_name)
    img = Image.open(full_img_path)
    return img

def crop_image(img, xy, scale_factor):
    '''Crop the image around the tuple xy

    Inputs:
    -------
    img: Image opened with PIL.Image
    xy: tuple with relative (x,y) position of the center of the cropped image
        x and y shall be between 0 and 1
    scale_factor: the ratio between the original image's size and the cropped image's size
    '''
    center = (img.size[0] * xy[0], img.size[1] * xy[1])
    new_size = (img.size[0] / scale_factor, img.size[1] / scale_factor)
    left = max (0, (int) (center[0] - new_size[0] / 2))
    right = min (img.size[0], (int) (center[0] + new_size[0] / 2))
    upper = max (0, (int) (center[1] - new_size[1] / 2))
    lower = min (img.size[1], (int) (center[1] + new_size[1] / 2))
    cropped_img = img.crop((left, upper, right, lower))
    return cropped_img

def save_img(img, img_name):
    img_dir = get_img_dir()
    full_img_path = os.path.join(img_dir, img_name)
    img.save(full_img_path)

if __name__ == '__main__':
    ams = open_img()

    crop_ams = crop_image(ams, (0.50, 0.50), 0.95)
    save_img(crop_ams, 'crop_amsterdam_01.jpg')

    crop_ams = crop_image(ams, (0.25, 0.25), 2.5)
    save_img(crop_ams, 'crop_amsterdam_02.jpg')

    crop_ams = crop_image(ams, (0.75, 0.45), 3.5)
    save_img(crop_ams, 'crop_amsterdam_03.jpg')

原始图片:

crop_amsterdam_01.jpg:

crop_amsterdam_01.jpg:

crop_amsterdam_02.jpg:

crop_amsterdam_02.jpg:

crop_amsterdam_03.jpg:

crop_amsterdam_03.jpg:

这篇关于在Python PIL中以可设置的中心和比例裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆