Python画像を中心から指定サイズで切り出し【OpenCV・Numpy】

python コンピュータ
python
多分Numpyの機能だとは思うのですが、img[top:bottom, left:right]で切り出しをしています。

#!/usr/bin/env python3
# coding: utf8

import cv2
import numpy as np
import os, glob


# 画像を中心から指定サイズで切り出し

def trim_center(img, width, height):
    h, w = img.shape[:2]
    
    top = int((h / 2) - (height / 2))
    bottom = top+height
    left = int((w / 2) - (width / 2))
    right = left+width
    
    return img[top:bottom, left:right]

if __name__ == '__main__':
    src_file = 'C:/Users/PC01114/Pictures/202205061049.JPG'
    width = height = 64
    img = cv2.imread(src_file)
    dst = trim_center(img, width, height)
    cv2.imwrite('output.png', dst)

コメント