美文网首页
缩放、裁剪和补边 — OpenCV& Python

缩放、裁剪和补边 — OpenCV& Python

作者: WZChan | 来源:发表于2017-10-09 14:43 被阅读0次
    import cv2
    
    img_path = 'C:/Users/WZChan/Desktop/'
    img = cv2.imread(img_path + 'test_600x350.jpg')
    
    #resize img to 300x200
    img_300x200 = cv2.resize(img, (200, 300))
    #cv2.resize(imgfile, (width, height))
    
    img_half = cv2.resize(img, (0, 0), fx = 0.5, fy = 0.5, interpolation = cv2.INTER_NEAREST)
    
    img_300x300 = cv2.copyMakeBorder(img, 50, 50, 0, 0, cv2.BORDER_CONSTANT, value=(0, 0, 0))
    
    patch = img[20:150, -180:-50]
    # NOTE: its img[y: y + h, x: x + w] and *not* img[x: x + w, y: y + h]
    
    cv2.imwrite(img_path + 'cropped_img.jpg', patch)
    cv2.imwrite(img_path + 'resized_300x200.jpg', img_300x200)
    cv2.imwrite(img_path + 'resized_half.jpg', img_half)
    cv2.imwrite(img_path + 'resized_300x300.jpg', img_300x300)
    
    cropped_img.jpg resized_300x200.jpg resized_half.jpg resized_300x300.jpg

    相关文章

      网友评论

          本文标题:缩放、裁剪和补边 — OpenCV& Python

          本文链接:https://www.haomeiwen.com/subject/fmfpyxtx.html