繁体   English   中英

使用 PIL 在中心裁剪图像

[英]Crop an image in the centre using PIL

如何在中心裁剪图像? 因为我知道该框是一个定义左、上、右和下像素坐标的 4 元组,但我不知道如何获取这些坐标,因此它会在中心进行裁剪。

假设您知道要裁剪到的尺寸 (new_width X new_height):

import Image
im = Image.open(<your image>)
width, height = im.size   # Get dimensions

left = (width - new_width)/2
top = (height - new_height)/2
right = (width + new_width)/2
bottom = (height + new_height)/2

# Crop the center of the image
im = im.crop((left, top, right, bottom))

如果您尝试将小图像裁剪得更大,这会中断,但我假设您不会尝试这样做(或者您可以捕捉到这种情况而不是裁剪图像)。

所提出的解决方案的一个潜在问题是在所需大小和旧大小之间存在奇怪差异的情况下。 每边不能有半个像素。 必须选择一侧放置额外的像素。

如果水平方向有奇数差异,下面的代码会将额外的像素放在右侧,如果垂直方向有奇数差异,额外的像素会放在底部。

import numpy as np

def center_crop(img, new_width=None, new_height=None):        

    width = img.shape[1]
    height = img.shape[0]

    if new_width is None:
        new_width = min(width, height)

    if new_height is None:
        new_height = min(width, height)

    left = int(np.ceil((width - new_width) / 2))
    right = width - int(np.floor((width - new_width) / 2))

    top = int(np.ceil((height - new_height) / 2))
    bottom = height - int(np.floor((height - new_height) / 2))

    if len(img.shape) == 2:
        center_cropped_img = img[top:bottom, left:right]
    else:
        center_cropped_img = img[top:bottom, left:right, ...]

    return center_cropped_img

我觉得最适合大多数应用程序的最简单的解决方案仍然缺失。 接受的答案存在像素不均匀的问题,尤其是对于 ML 算法,裁剪图像的像素数至关重要。

在以下示例中,我想将图像从中心裁剪为 224/100。 我不在乎像素向左还是向右移动 0.5,只要输出图片始终具有定义的尺寸即可。 它避免了对数学的依赖。*。

from PIL import Image
import matplotlib.pyplot as plt


im = Image.open("test.jpg")
left = int(im.size[0]/2-224/2)
upper = int(im.size[1]/2-100/2)
right = left +224
lower = upper + 100

im_cropped = im.crop((left, upper,right,lower))
print(im_cropped.size)
plt.imshow(np.asarray(im_cropped))

输出是裁剪前的(代码中未显示):

在此处输入图片说明

后:

在此处输入图片说明

元组显示尺寸。

这是我一直在寻找的功能:

from PIL import Image
im = Image.open("test.jpg")

crop_rectangle = (50, 50, 200, 200)
cropped_im = im.crop(crop_rectangle)

cropped_im.show()

取自另一个答案

我最初使用了接受的答案:

import Image
im = Image.open(<your image>)
width, height = im.size   # Get dimensions

left = (width - new_width)/2
top = (height - new_height)/2
right = (width + new_width)/2
bottom = (height + new_height)/2

# Crop the center of the image
im = im.crop((left, top, right, bottom))

但是我遇到了 Dean Pospisil 提到的问题

所提出的解决方案的一个潜在问题是在所需大小和旧大小之间存在奇怪差异的情况下。 每边不能有半个像素。 必须选择一侧放置额外的像素。

Dean Pospisil 的解决方案有效,我也想出了自己的计算来解决这个问题:

import Image
im = Image.open(<your image>)
width, height = im.size   # Get dimensions

left = round((width - new_width)/2)
top = round((height - new_height)/2)
x_right = round(width - new_width) - left
x_bottom = round(height - new_height) - top
right = width - x_right
bottom = height - x_bottom

# Crop the center of the image
im = im.crop((left, top, right, bottom))

使用接受的答案,将180px x 180px裁剪为180px x 101px图像将导致裁剪图像为180px x 102px

根据我的计算,它将被正确裁剪为180px x 101px

可能是我参加这个聚会迟到了,但至少我在这里我想将图像居中裁剪 将 9:16 图像转换为 16:9 纵向到横向

这是我使用的算法:

  1. 将图像分成 4 个相等的部分
  2. 丢弃第 1 部分和第 4 部分
  3. 将左设置为 0,右设置为图像的宽度

代码 :

from PIL import Image

im = Image.open('main.jpg')
width, height = im.size

if height > width:
    h2 = height/2
    h4 = h2/2

    border = (0, h4, width, h4*3)

    cropped_img = im.crop(border)
    cropped_img.save("test.jpg")

前 :

在此处输入图片说明

后:

在此处输入图片说明 我希望这有帮助

裁剪中心及周边:

def im_crop_around(img, xc, yc, w, h):
    img_width, img_height = img.size  # Get dimensions
    left, right = xc - w / 2, xc + w / 2
    top, bottom = yc - h / 2, yc + h / 2
    left, top = round(max(0, left)), round(max(0, top))
    right, bottom = round(min(img_width - 0, right)), round(min(img_height - 0, bottom))
    return img.crop((left, top, right, bottom))


def im_crop_center(img, w, h):
    img_width, img_height = img.size
    left, right = (img_width - w) / 2, (img_width + w) / 2
    top, bottom = (img_height - h) / 2, (img_height + h) / 2
    left, top = round(max(0, left)), round(max(0, top))
    right, bottom = round(min(img_width - 0, right)), round(min(img_height - 0, bottom))
    return img.crop((left, top, right, bottom))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM