简体   繁体   English

使用 PIL 裁剪图像时的背景颜色

[英]Background color when cropping image with PIL

The good thing with PIL.crop is that if we want to crop outside of the image dimensions, it simply works with: PIL.crop的好处是,如果我们想在图像尺寸之外进行裁剪,它只适用于:

from PIL import Image
img = Image.open("test.jpg")
img.crop((-10, -20, 1000, 500)).save("output.jpg")

Question: how to change the background color of the added region to white (default: black)?问题:如何将添加区域的背景颜色更改为白色(默认:黑色)?

在此处输入图像描述

Note:笔记:

I think it is not possible with one function call due to the relevant C function which seems to zero-out the destination image memory region (see it here: https://github.com/python-pillow/Pillow/blob/master/src/libImaging/Crop.c#L47 )我认为一个函数调用是不可能的,因为相关的 C 函数似乎将目标图像内存区域归零(在此处查看: https ://github.com/python-pillow/Pillow/blob/master/ src/libImaging/Crop.c#L47 )

You mentioned as not interested to create new Image and copy over it but i am pasting that kind of solution anyway for reference:您提到对创建新图像并对其进行复制不感兴趣,但无论如何我都会粘贴这种解决方案以供参考:

from PIL import Image
img = Image.open("test.jpg")
x1, y1, x2, y2 = -10, -20, 1000, 500  # cropping coordinates
bg = Image.new('RGB', (x2 - x1, y2 - y1), (255, 255, 255))
bg.paste(img, (-x1, -y1))
bg.save("output.jpg")

Output:输出:

在此处输入图像描述

You can do what you intend to after using the expand() function available in ImageOps module of PIL.使用 PIL 的ImageOps模块中可用的expand()函数后,您可以做您想做的事情。

from PIL import Image
from PIL import ImageOps
filename = 'C:/Users/Desktop/Maine_Coon_263.jpg'
img = Image.open(filename)

val = 10    #--- pixels to be cropped

#--- a new image with a border of 10 pixels on all sides
#--- also notice fill takes in the color of white as (255, 255, 255)
new_img = ImageOps.expand(img, border = val, fill = (255, 255, 255))

#--- cropping the image above will not result in any black portion
cropped = new_img.crop((val, val, 150, 150))

The crop() function only takes one parameter of how much portion has to be cropped. crop()函数只接受一个参数,即需要裁剪多少部分。 There is no functionality to handle the situation when a negative value is passed in. Hence upon passing a negative value the image gets padded in black pixels.当传入负值时,没有功能可以处理这种情况。因此,在传递负值时,图像会以黑色像素填充。

Using the expand() function you can set the color of your choice and then go ahead and crop as you wish.使用expand()函数,您可以设置您选择的颜色,然后继续并根据需要进行裁剪。

EDIT编辑

In response to your edit, I have something rather naïve in mind but it works.作为对您的编辑的回应,我有一些相当幼稚的想法,但它确实有效。

  • Get the absolute values of all the values to be cropped.获取所有要裁剪的值的绝对值。 You can use numpy.abs() .您可以使用numpy.abs()
  • Next the maximum among these values using numpy.max() .接下来使用numpy.max()这些值中的最大值。
  • Finally expand the image using this value and crop accordingly.最后使用这个值扩展图像并相应地裁剪。

This code will help you:此代码将帮助您:

#--- Consider these values in a tuple that are to crop your image 
crop_vals = (-10, -20, 1000, 500)

#--- get maximum value after obtaining the absolute of each
max_val = np.max(np.abs(crop_vals))

#--- add border to the image using this maximum value and crop
new_img = ImageOps.expand(img, border = max_val, fill = (255, 255, 255))
cropped = new_img.crop((max_val - 10, max_val - 20, new_img.size[0], new_img.size[1]))

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

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