在 Python 中實現照片馬賽克


照片馬賽克是一種技術,我們可以將影像分割成一個正方形網格。每個正方形將被其他影像或顏色替換。因此,當我們想從一定距離觀看實際影像時,我們可以看到實際影像,但如果我們走近,我們可以看到不同顏色塊的網格。

在本例中,我們使用了一個名為 photomosaic 的 Python 模組。使用此模組,我們可以輕鬆建立一些照片馬賽克。要安裝它,請訪問此連結。它還會下載**scikit learn**模組。

sudo pip3 install photomosaic

此模組具有一些功能。這些功能列在下面:

  • 在這裡,我們可以使用不同大小的圖塊。
  • 我們可以為影像的細節部分設定更小的圖塊。
  • 使用 Flickr API 獲取大量可作為圖塊使用的影像。

在本文中,我們將瞭解如何以非常簡單的方式為此模組實現照片馬賽克。

我們使用 skimage 庫中的一個影像。

主影像

Main Output

建立照片馬賽克的步驟

  • 獲取實際影像(此處為 skimage 庫中的影像)
  • 定義網格圖塊大小
  • 提供一個位置來建立彩色 RGB 影像塊作為池
  • 將資料夾設定為照片馬賽克的池
  • 使用池和網格大小轉換為照片馬賽克。
  • 儲存影像
  • 退出

示例程式碼

from skimage.io import *
import sys
import photomosaic asphmos
from skimage import data
image = data.coffee() #Get coffee image from skimage
#Get the mosaic size from the command line argument.
mos_size = (int(sys.argv[1]), int(sys.argv[2]))
#create all image squares and generate pool
phmos.rainbow_of_squares('square/')
square_pool = phmos.make_pool('square/*.png')
#Create the mosaic image and save
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))
First Run

輸出(第二次執行,網格大小為 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))
Second Run

更新於: 2019-07-30

577 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.