이미지들의 평균 그리기
Joonas' Note
이미지들의 평균 그리기 본문
- 설명
- 코드
- 결과
설명

이미지들의 평균 픽셀값을 확인하고 싶은 경우에 사용하면 된다.
오래 걸리는 경우에 로딩을 표시하려고 tqdm을 사용했는데 그냥 빼도 된다.
이미지의 픽셀값을 전부 다 저장하고 평균을 출력하다간 메모리가 터진다. 그래서 Moving average로 해결했다.
코드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from PIL import Image | |
from tqdm import tqdm | |
def file_list(path): | |
return list(map(lambda s: os.path.join(path, s), os.listdir(path))) | |
def plot_average_image(path): | |
a = file_list(path) | |
print(len(a), a[:5]) | |
a_ = None | |
n = 1 | |
for i in tqdm(a): | |
try: | |
img = Image.open(i, 'r').resize((228, 228)) | |
except: | |
print('Wrong image file:', i) | |
continue | |
img = np.array(img) | |
if n == 1: | |
a_ = img | |
else: | |
a_ = ((a_ * n) + img) / (n + 1) | |
n += 1 | |
del img | |
plt.imshow(a_ / 255.) | |
plt.axis('off') | |
plt.show() | |
del a_ | |
del a | |
plot_average_image('../animeface/images') | |
plot_average_image('../celebA/img_align_celeba/img_align_celeba') |
결과
아래와 같이 1,000장 정도 되는 얼굴 사진을 모두 겹쳐서 그려볼 수 있다.

'개발 > python' 카테고리의 다른 글
설치된 python의 지원 가능한 platform 확인 (0) | 2022.12.01 |
---|---|
[Beanie + PyTest] CollectionWasNotInitialized 오류 (0) | 2022.09.24 |
[코딩으로 풀어보기] 문제적남자 107화 - 1부터 9까지의 숫자를 한 번씩 사용해서 올바른 식 만들기 (0) | 2022.04.02 |
python으로 vscode extension 개발하기 (0) | 2022.01.28 |
큰 파일 내용 정렬은 어떻게 정렬할까? (How to sort lines of a large text file) (0) | 2021.09.01 |