8-2Cutout.py

000: import cv2
001:
002: def __main():
003: img = cv2.imread(‘IMG_1692.jpg’)
004: img = getResize(img)
005: y = 100
006: x = 300
007: h = 500
008: w = 600
009: # ここで実行(元画に対して座標を指定するだけ)
010: dst = img[y:h, x:w]
011:
012: cv2.imshow(‘Original’, img)
013: cv2.imshow(‘Final result’, dst)
014: cv2.waitKey(0)
015: cv2.destroyAllWindows()
016:
017: def getResize(src):
018: basePixSize = 1280 # 縦横で大きい辺の変更したいサイズ
019: height = src.shape[0]
020: width = src.shape[1]
021:
022: largeSize = max(height, width) # 大きい方の辺のサイズ
023: resizeRate = basePixSize / largeSize # 変更比率を計算
024:
025: dst = cv2.resize(src, (int(width * resizeRate), int(height * resizeRate)), interpolation=None)
026:
027: return dst
028:
029: if __name__ == ‘__main__’:
030: print(cv2.__version__)
031:
032: __main()