python截取图片的一部分

import cv2, sys

path=sys.argv[1]
im=cv2.imread(path)
name = path.split('/')[-1]

xy=[]
def getInfo(event, x,y,flags,param):
  if event == cv2.EVENT_LBUTTONDOWN:
    print x,y #im[y,x]
    xy.append(x)
    xy.append(y)
  if event == cv2.EVENT_RBUTTONDOWN:
    print x,y
    x1=xy[0]
    y1=xy[1]
    #xy.clear()
    x2,y2 = x-x1, y-y1
    d = min(x2,y2)
    cropped = im[y1:y1+d, x1:x1+d]
    cv2.imwrite("click/"+name, cropped)
    
cv2.namedWindow('image', 0)    
#cv2.resizeWindow('image', 1024, 1024)
cv2.imshow('image',im)
cv2.setMouseCallback("image",getInfo)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 左键单击选择左上角,右键单击选择右下角
  • 以长宽的最小值截取正方形图像
  • 结果保存在click中,记得先新建click文件夹

你可能感兴趣的:(python)