python图片裁剪crop

1、opencv

import os
import cv2
image = cv2.imread('img1.jpg')
cropImg = image[int(302-150):int(302+150), int(278-150):int(278+150)]
cv2.imwrite('crop1.jpg', cropImg)

2、PIL

from PIL import Image
im = Image.open("renren.jpeg")
# 图片的宽度和高度
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
'''
裁剪:传入一个元组作为参数
元组里的元素分别是:(距离图片左边界距离x, 距离图片上边界距离y,距离图片左边界距离+裁剪框宽度x+w,距离图片上边界距离+裁剪框高度y+h)
'''
# 截取图片中一块宽和高都是250的
x = 100
y = 100
w = 250
h = 250
region = im.crop((x, y, x+w, y+h))
region.save("./crop_test1.jpeg")
 
# 截取图片中一块宽是250和高都是300的
x = 100
y = 100
w = 250
h = 300
region = im.crop((x, y, x+w, y+h))
region.save("./crop_test2.jpeg")

https://www.jb51.net/article/116789.htm

猜你喜欢

转载自blog.csdn.net/weixin_41770169/article/details/94600505