9-3Rotation.py

000: import cv2
001:
002: def __main():
003: img = cv2.imread(‘IMG_1609.JPG’)
004:
005: img = getResize(img)
006: org = img.copy()
007: height, width, channel = img.shape
008:
009: M = cv2.getRotationMatrix2D(center=(width / 2, height / 2), angle=60, scale=0.5)
010: # ここで処理を実行
011: dst = cv2.warpAffine(src=img, M=M, dsize=(width, height))
012:
013: cv2.imshow(‘Original’, org)
014: cv2.imshow(‘Final result’, dst)
015: cv2.waitKey(0)
016: cv2.destroyAllWindows()
017:
018: def getResize(src):
019: basePixSize = 1280 # 縦横で大きい辺の変更したいサイズ
020: height = src.shape[0]
021: width = src.shape[1]
022:
023: largeSize = max(height, width) # 大きい方の辺のサイズ
024: resizeRate = basePixSize / largeSize # 変更比率を計算
025:
026: dst = cv2.resize(src, (int(width * resizeRate), int(height * resizeRate)), interpolation=None)
027:
028: return dst
029:
030: if __name__ == ‘__main__’:
031: print(cv2.__version__)
032:
033: __main()