hotamul의 개발 이야기

[Python] OpenCV로 image size 조절하기 본문

Dev./Python

[Python] OpenCV로 image size 조절하기

hotamul 2023. 1. 1. 15:58

Interpolation Methods for resizing

  • cv2.INTER_AREA: 주로 이미지 크기를 줄이는데 사용한다.
  • cv2.INTER_CUBIC: 속도는 느리지만 Interpolation 성능이 좋다.
  • cv2.INTER_LINEAR: 주로 이미지 크기를 늘리는데 사용하고 OpenCV의 default Interpolation method이다.

참고: OpenCV - InterpolationFlags

Shrink & Enlarge Image

$ pip install opencv-python-headless numpy
import numpy as np
import cv2

# loading image
img = cv2.imread('./testimg.jpg')

# shrinking image
# width = columns , height = rows
img_shrink = cv2.resize(img, dsize=(320,240), interpolation=cv2.INTER_AREA)
# enlarge
# cv2.INTER_CUBIC, cv2.INTER_LINEAR
img_zoom = cv2.resize(img, dsize=(1280,960), interpolation=cv2.INTER_CUBIC)

# Check size
print(img.shape, img_shrink.shape, img_zoom.shape)
((480, 640, 3), (240, 320, 3), (960, 1280, 3))
# Show image
cv2.imshow('original',img)
cv2.imshow('Shink',img_shrink)
cv2.imshow('Zoom',img_zoom)
cv2.waitKey(0)
cv2.destroyAllWindows()

'Dev. > Python' 카테고리의 다른 글

진짜 이상한 Python의 del  (0) 2023.12.11
[Python] OpenCV Face Detection  (0) 2023.01.01
[Python] image 파일 numpy.ndarray로 불러오기  (0) 2023.01.01
[Data][Python] Pandas + Numpy example  (0) 2022.01.10
[Data][Python] Pandas example  (0) 2022.01.10
Comments