opencv_python 이미지 불러오기, 저장

Posted by PeEn
2019. 12. 31. 21:42 Programing/opencv

opencv_python 이미지 불러오기

# -*- coding: utf-8 -*-
import cv2


img_file ="../pic/lena.jpg" #이미지 경로설정
img = cv2.imread(img_file) #이미지를 읽어 변수로 할당

if img is not None:
    cv2.imshow('IMG',img)    #이미지 화면에 표시
    cv2.waitKey()            #키입력까지 대기
    cv2.destroyAllWindows()  #창닫기
else:
    print("Don't found image file")

img_file ="이미지 경로, 읽기모드"
* 읽기모드를 쓰지 않으면 RGB모드로 읽어온다.

opencv_python 이미지 저장, 그레이 모드로 읽기

# -*- coding: utf-8 -*-
import cv2

img_file = "../pic/lena.jpg" #이미지 경로설정
save_img = "../pic/lena_gray.jpg" # 저장할 경로설정

img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE) #이미지를 그레이모드로 읽어 변수로 할당

if img is not None:
    cv2.imshow('IMG',img)           #이미지 화면에 표시
    cv2.imwrite(save_img,img)  		#이미지 저장
    cv2.waitKey()                   #키입력까지 대기
    cv2.destroyAllWindows()         #창닫기
else:
    print("Don't found image file")