Computer >> 컴퓨터 >  >> 프로그래밍 >> Python

파이썬으로 포토모자이크 만들기: photomosaic 모듈 활용 가이드


포토모자이크(Photomosaic)는 하나의 이미지를 정사각형 격자 형태로 분할한 뒤, 각 칸을 다른 이미지나 색상 블록으로 대체하는 기법입니다. 멀리서 보면 원본 이미지가 그대로 보이지만, 가까이 다가가면 다양한 색상의 작은 블록들이 격자 모양으로 배열되어 있는 것을 확인할 수 있습니다.

이 글에서는 파이썬의 photomosaic 모듈을 사용해 손쉽게 포토모자이크를 제작하는 방법을 소개합니다. 아래 명령어 한 줄로 설치할 수 있으며, 설치 과정에서 scikit-learn 모듈도 함께 다운로드됩니다.

sudo pip3 install photomosaic

photomosaic 모듈의 주요 특징은 다음과 같습니다.

  • 타일(tile)의 크기를 자유롭게 조절할 수 있습니다.
  • 이미지의 세부적인 부분에는 더 작은 타일을 지정해 디테일을 살릴 수 있습니다.
  • Flickr API를 활용하면 방대한 양의 이미지를 타일용 풀(pool)로 확보할 수 있습니다.

이제 이 모듈을 활용해 포토모자이크를 아주 간단한 방식으로 구현해 보겠습니다. 예제에서는 skimage(scikit-image) 라이브러리에서 제공하는 샘플 이미지를 사용합니다.

메인 이미지

파이썬으로 포토모자이크 만들기: photomosaic 모듈 활용 가이드

포토모자이크 생성 단계

  • 원본 이미지를 불러옵니다(여기서는 skimage 라이브러리의 이미지 사용).
  • 그리드 타일 크기를 정의합니다.
  • 컬러 RGB 이미지 블록들을 생성해 풀(pool)로 저장할 위치를 지정합니다.
  • 해당 폴더를 포토모자이크의 풀로 설정합니다.
  • 풀과 그리드 크기를 이용해 포토모자이크를 생성합니다.
  • 결과 이미지를 저장합니다.
  • 프로그램을 종료합니다.

예제 코드

from skimage.io import *
import sys
import photomosaic as phmos
from skimage import data
image = data.coffee() # skimage에서 커피 이미지 가져오기
# 커맨드 라인 인자로 모자이크 크기를 전달받습니다.
mos_size = (int(sys.argv[1]), int(sys.argv[2]))
# 모든 이미지 사각형을 생성하고 풀을 만듭니다.
phmos.rainbow_of_squares('square/')
square_pool = phmos.make_pool('square/*.png')
# 모자이크 이미지를 생성한 뒤 저장합니다.
mosaic = phmos.basic_mosaic(image, square_pool, mos_size)
imsave('mosaic_op.png', mosaic)

위 코드는 스크립트 실행 시 커맨드 라인 인자로 가로·세로 그리드 크기를 받아, 해당 크기만큼 이미지를 분할한 후 미리 만들어 둔 색상 블록 풀과 매칭시켜 최종 모자이크를 완성하는 구조입니다.

실행 결과 (첫 번째 실행, 그리드 크기 100 x 100)

$ python3 225.Photomosaic.py 100 100
5832it [00:02, 2506.05it/s]
analyzing pool: 100%|| 5832/5832 [00:08<00:00, 717.90it/s]
/usr/local/lib/python3.6/dist-packages/skimage/transform/_warps.py:105: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
warn("The default mode, 'constant', will be changed to 'reflect' in "
/usr/local/lib/python3.6/dist-packages/skimage/transform/_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images.
warn("Anti-aliasing will be enabled by default in skimage 0.15 to "
partitioning: depth 0: 100%|| 10000/10000 [00:00<00:00, 852292.94it/s]
analyzing tiles: 100%|| 10000/10000 [00:00<00:00, 93084.50it/s]
matching: 100%|| 10000/10000 [00:00<00:00, 30864.50it/s]
drawing mosaic: 100%|| 10000/10000 [00:00<00:00, 13227.12it/s]
/usr/local/lib/python3.6/dist-packages/skimage/util/dtype.py:141: UserWarning: Possible precision loss when converting from float64 to uint8
.format(dtypeobj_in, dtypeobj_out))

100 x 100 그리드로 실행한 결과입니다. 출력 과정에 나타나는 UserWarning 메시지는 skimage 버전 업데이트에 따른 단순 경고이므로 결과에는 영향을 주지 않습니다.

파이썬으로 포토모자이크 만들기: photomosaic 모듈 활용 가이드

실행 결과 (두 번째 실행, 그리드 크기 500 x 500)

$ python3 225.Photomosaic.py 500 500
5832it [00:02, 2634.16it/s]
analyzing pool: 100%|| 5832/5832 [00:08<00:00, 709.54it/s]
/usr/local/lib/python3.6/dist-packages/skimage/transform/_warps.py:105: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
warn("The default mode, 'constant', will be changed to 'reflect' in "
/usr/local/lib/python3.6/dist-packages/skimage/transform/_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images.
warn("Anti-aliasing will be enabled by default in skimage 0.15 to "
partitioning: depth 0: 100%|| 250000/250000 [00:00<00:00, 456159.45it/s]
analyzing tiles: 100%|| 250000/250000 [00:02<00:00, 113937.01it/s]
matching: 100%|| 250000/250000 [00:07<00:00, 32591.43it/s]
drawing mosaic: 100%|| 250000/250000 [00:02<00:00, 104349.90it/s]
/usr/local/lib/python3.6/dist-packages/skimage/util/dtype.py:141: UserWarning: Possible precision loss when converting from float64 to uint8
.format(dtypeobj_in, dtypeobj_out))

그리드 크기를 500 x 500으로 늘리면 분할되는 타일 수가 25만 개로 증가해 처리 시간이 다소 길어지지만, 훨씬 더 촘촘하고 정교한 포토모자이크 결과물을 얻을 수 있습니다.

파이썬으로 포토모자이크 만들기: photomosaic 모듈 활용 가이드