Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

Python3.6 + OpenCV3.1.1で画像サイズを変える

import cv2
import numpy as np
 
if __name__ == '__main__':
 
    # 画像の読み込み。第二引数"1"はグレースケール
    img_src = cv2.imread("picture.png", 1)
 
    # グレースケールに変換
    img_gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
 
    # 二値変換
    #しきい値を設定
    thresh = 100
    max_pixel = 255
    ret, img_dst = cv2.threshold(img_gray,
                                 thresh,
                                 max_pixel,
                                 cv2.THRESH_BINARY)

    height = img_dst.shape[0]
    width  = img_dst.shape[1]
    halfsize = (round(width/2),round(height/2))
    halfsize_dst = cv2.resize(img_dst,halfsize)
 
    # 表示
    cv2.imshow("Show BINARIZATION Image", halfsize_dst)
    cv2.imwrite("picture_bin.png", halfsize_dst)
    cv2.destroyAllWindows()