在python中使用PIL裁剪图像

时间:2021-04-14 00:23:55

I want to crop image in the way by removing first 30 rows and last 30 rows from the given image. I have searched but did not get the exact solution. Does somebody have some suggestions?

我想通过从给定图像中删除前30行和最后30行来裁剪图像。我搜索过但没有得到确切的解决方案。有人有一些建议吗?

2 个解决方案

#1


120  

There is a crop() method:

有一个crop()方法:

w, h = yourImage.size
yourImage.crop((0, 30, w, h-30)).save(...)

#2


9  

You need to import PIL (Pillow) for this. Suppose you have an image of size 1200, 1600. We will crop image from 400, 400 to 800, 800

您需要为此导入PIL(枕头)。假设您的图像大小为1200,1600。我们将裁剪400,400到800,800的图像

from PIL import Image
img = Image.open("ImageName.jpg")
area = (400, 400, 800, 800)
cropped_img = img.crop(area)
cropped_img.show()

#1


120  

There is a crop() method:

有一个crop()方法:

w, h = yourImage.size
yourImage.crop((0, 30, w, h-30)).save(...)

#2


9  

You need to import PIL (Pillow) for this. Suppose you have an image of size 1200, 1600. We will crop image from 400, 400 to 800, 800

您需要为此导入PIL(枕头)。假设您的图像大小为1200,1600。我们将裁剪400,400到800,800的图像

from PIL import Image
img = Image.open("ImageName.jpg")
area = (400, 400, 800, 800)
cropped_img = img.crop(area)
cropped_img.show()