728x90
반응형
필요한 패키지 불러오기
# 이미지 시각화
# package 불러오기
import numpy as np
from PIL import Image # image 읽어올때 PIL
import matplotlib.pyplot as plt
%matplotlib inline
이미지 2개 다운받고 경로 설정
# 이미지 파일 열기
path = 'image/animal1.jpg' # 다람쥐 사진 경로
image_pil = Image.open(path)
image = np.array(image_pil)
이미지 확인
# 이미지 확인해보기
print(image.shape) # 201,251, 3 201 - 세로 길이, 251 - 가로, 3 - 색상 rgb, 이미지는 2차원이지만 rgb때문에 3
print(np.min(image), np.max(image)) # 0,255 확인 range확인
그래프로 시각화
# 그래프로 시각화 하기
plt.hist(image.ravel(), 256, [0,256])
plt.show()
이미지 나타내기
# 그림 나타내기
plt.imshow(image)
plt.show()
이미지 흑백으로
# 이미지 흑백으로 열기
print("path : ",path)
image_pil = Image.open(path).convert("L") # 흑백으로
image_bw = np.array(image_pil)
print("image_bw : ", image_bw.shape) # 201, 251 , rgb가 빠져서 0,1 혹은 없음
흑백
# 흑백 이미지 열기
plt.subplot(1,2,1) # 수치적으로 점점 초록색 - 높은 값이 노랑색쪽 밝아진다.
plt.imshow(image_bw)
plt.subplot(1,2,2)
plt.imshow(image_bw, 'gray') # 흑백
plt.show()
다른 색상으로 변경
# 다른 색상으로 cmap 표현
# gray scale
print("image_bw : ",image_bw.shape)
print("image : ",image.shape)
# RdBu red,blue
# jet
plt.subplot(1,2,1)
plt.imshow(image_bw, 'RdBu')
plt.subplot(1,2,2)
plt.imshow(image_bw, 'jet') # 높을수록 빨간색, 낮을 수록 파란색
plt.show()
Colorbar 추가
# Colorbar 추가하기
plt.imshow(image_bw, 'jet')
plt.colorbar() # 컬러바로 색상 설명
plt.show()
이미지 사이즈 조절
# 이미지 사이즈 조절
plt.figure(figsize=(10,7))
plt.subplot(1,2,1)
plt.imshow(image_bw, 'jet')
plt.colorbar()
plt.subplot(1,2,2)
plt.imshow(image_bw, 'RdBu')
plt.show()
제목 추가
# 이미지 제목 추가
plt.title('squirrel')
plt.imshow(image)
plt.show()
두 번째 이미지 열기
# 두 번째 이미지 열기
dog_path = 'image/animal2.jpg'
dog_pil = Image.open(dog_path)
dog_image = np.array(dog_pil)
plt.imshow(dog_image)
plt.show()
각 이미지 크기 비교
# 각각 이미지 크기 비교
image.shape, dog_image.shape
이미지 합치기 전 이미지 크기 조절
# 이미지 합치기
import cv2 # cv2가 없다면 pip install opencv-python 으로 다운받기
squirrel_image = cv2.resize(image, (500,651)) # 반대로 써야한다.
squirrel_image.shape # 이미지 변경 완료
print("shape : ", squirrel_image.shape, dog_image.shape)
이미지 합치기
# 두 이미지 합쳐서 출력
plt.imshow(squirrel_image)
plt.imshow(dog_image, alpha=0.5) # 투명도를 안주면 겹쳐서 안보인다.
plt.show()
총합
#??
plt.figure(figsize=(10,9))
plt.subplot(121) # 2,2,1 로 인식한다.
plt.imshow(squirrel_image, 'gray')
plt.subplot(122)
plt.imshow(dog_image)
plt.show()
728x90
반응형
최근댓글