In [1]:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
%matplotlib inline
from PIL import Image
In [2]:
import matplotlib
matplotlib.font_manager._rebuild()
In [4]:
#이미지 파일 열기#
path = 'source/dog.jpg'
image_pil=Image.open(path)
image = np.array(image_pil)
In [5]:
image_pil
Out[5]:
In [6]:
image.shape #세로,가로,RGB) gray는 1이다. 색상변경도 가능
Out[6]:
(734, 1100, 3)
In [7]:
np.min(image), np.max(image) #색에따른 array라 255가 끝이다 RGB
Out[7]:
(0, 255)
In [8]:
#그래프로 시각화 하기
plt.hist(image.ravel(),256,[0,256])
plt.show()
In [9]:
#array데이터로 그림 나다태기
plt.imshow(image)
plt.show()
In [10]:
#흑백으로 열기
image_pil=Image.open(path).convert('L')
image_bw=np.array(image_pil)
In [11]:
image_bw.shape #뒤에 아무것도 없다->흑백이란 소리
Out[11]:
(734, 1100)
In [12]:
plt.imshow(image_bw,'gray') #gray를 안붙여주면 밝기여부로만 표시됨 밝은곳은 노랑 어두운곳은 초록으로
plt.show()
In [13]:
plt.imshow(image_bw,'RdBu')
plt.show()
In [14]:
plt.imshow(image_bw,'jet') #jet를 가장 많이 사용한다 높은값은 빨강 낮은값 파랑으로 표시
plt.show()
In [15]:
plt.imshow(image_bw,'jet')
plt.colorbar() #colorbar()로 값에따른 색을 알수있다.
plt.show()
In [16]:
plt.figure(figsize=(10,10))
plt.imshow(image) #plt라 사이즈 조절가능
plt.show()
In [17]:
plt.figure(figsize=(10,10))
plt.imshow(image) #plt라 사이즈 조절가능
plt.title("댕댕이")
plt.show()
In [18]:
#2번째 이미지
cat_path = 'source/cat.jpg'
cat_pil=Image.open(cat_path)
cat_image=np.array(cat_pil)
In [19]:
plt.imshow(cat_image)
Out[19]:
<matplotlib.image.AxesImage at 0x28dcad6bc70>
In [20]:
#2번째 이미지를 1번째 이미지 모양에 맞추기
cat_image.shape
Out[20]:
(183, 275, 3)
In [22]:
image.shape
Out[22]:
(734, 1100, 3)
In [23]:
#합치기 전에 준비과정
import cv2
In [24]:
dog_image=cv2.resize(image,(275,183)) #cv2에서 resize할때는 가로,세로를 반대로 입력해줘야함
dog_image.shape
Out[24]:
(183, 275, 3)
In [27]:
plt.imshow(dog_image)
plt.imshow(cat_image,alpha=0.5) #alpha투명도
plt.show()
#이게 고양이만 보이는게 아니라 겹친상태임
In [30]:
#SUBPLOT
plt.figure(figsize=(10,10))
plt.subplot(221) #subplot(2,2,1)이랑 같은 문장임
plt.imshow(dog_image)
plt.subplot(222)
plt.imshow(image_bw,'gray')
plt.subplot(223)
plt.imshow(cat_image)
Out[30]:
<matplotlib.image.AxesImage at 0x28dcc1e7d00>
In [ ]:
728x90
반응형
'프로그래밍 > Python' 카테고리의 다른 글
[Python].ipynb to .py (Window, Linux) / convert .ipynb to .py (0) | 2022.04.11 |
---|---|
[Jupyter notebook] 주피터 노트북 모양대로 티스토리에 글쓰기 (0) | 2021.06.23 |
[Numpy,Matplotlib] 3.시각화matplotlib.pyplot as plt (0) | 2021.06.18 |
[Numpy,Matplotlib] 2.Numpy는 차원을 늘리고 줄일수 있다 (0) | 2021.06.18 |
[Numpy,Matplotlib] 1. 배열다루기+시각화의 기초 (0) | 2021.06.18 |