이미지 인코딩, 디코딩

김지수·2021년 3월 16일
1

OpenCV

목록 보기
6/9
post-thumbnail

코드

import cv2

def loadImage(file_path):
    # 이미지 읽기
    src = cv2.imread(file_path, cv2.IMREAD_COLOR)
    # 이미지 인코딩 
    # retval : 압축 결과(True/False), buf :인코딩된 이미지
    retval, buf = cv2.imencode('.webp',
                               src,
                               [cv2.IMWRITE_WEBP_QUALITY, 100])
    return src, retval, buf

src, retval, buf = loadImage('파일경로')

# 이미지 디코딩
webp_img = cv2.imdecode(buf, 1)

# 원본, 인코딩, 디코딩 이미지 화면 출력
cv2.imshow('src', src)
cv2.imshow('encoding', buf)
cv2.imshow('decoding', webp_img)

# 화면 출력창 대기/닫기
cv2.waitKey()
cv2.destroyAllWindows()

결과

좌측 창 : 원본 이미지
가운데 창 : 인코딩 이미지
우측 창 : 디코딩 이미지


설명

cv2.imencode 메소드

이미지 인코딩(압축)

cv2.imencode(ext, src[, params])

ParameterDescription
ext출력 파일 확장자
src압축할 이미지
paramsImwriteFlags

return : retval(압축 결과 : True / False), buf(인코딩된 이미지)

cv2.imdecode 메소드

이미지 디코딩(압축)

cv.imdecode(buf, flags)

ParameterDescription
buf인코딩된 배열
flagsImreadModes

return : 이미지 배열

profile
A Data human as a Learner, a Supporter, and a Listener

0개의 댓글