photomosaic เป็นเทคนิคที่เราสามารถแบ่งรูปภาพของเราออกเป็นตารางสี่เหลี่ยม แต่ละช่องจะถูกแทนที่ด้วยรูปภาพหรือสีอื่นๆ ดังนั้นเมื่อเราต้องการดูภาพจริงจากระยะไกล เราจะเห็นภาพจริง แต่ถ้าเข้าไปใกล้ๆ เราจะเห็นตารางของบล็อกสีต่างๆ
ในกรณีนี้ เรากำลังใช้โมดูล Python ที่เรียกว่า photomosaic ด้วยการใช้โมดูลนี้ เราสามารถสร้างโฟโตโมเสกได้อย่างง่ายดาย หากต้องการติดตั้งโปรดไปที่ลิงค์นี้ นอกจากนี้ยังจะดาวน์โหลด scikit เรียนรู้ โมดูล
sudo pip3 install photomosaic
โมดูลนี้มีคุณสมบัติบางอย่าง ดังต่อไปนี้ -
- ที่นี่เราสามารถใช้กระเบื้องที่มีขนาดต่างกันได้
- เราสามารถตั้งค่าชิ้นส่วนที่เล็กลงสำหรับรายละเอียดของภาพได้
- ใช้ Flicker api เพื่อรับคอลเล็กชันรูปภาพจำนวนมากเพื่อใช้เป็นไทล์
ในบทความนี้ เราจะมาดูวิธีการใช้โมดูลนี้สำหรับโฟโตโมเสกในวิธีที่ง่ายมาก
เรากำลังใช้รูปภาพจากไลบรารี skimage
ภาพหลัก

ขั้นตอนในการสร้างโฟโตโมเสก
- ถ่ายภาพจริง (นี่คือภาพจากไลบรารี skimage)
- กำหนดขนาดไทล์กริด
- ระบุตำแหน่งเพื่อสร้างบล็อกภาพ RGB ที่มีสีสันเป็นพูล
- ตั้งค่าโฟลเดอร์เป็น Pool สำหรับโฟโตโมเสก
- เปลี่ยนเป็นโฟโตโมเสกโดยใช้พูลและขนาดกริด
- บันทึกภาพ
- ทางออก
โค้ดตัวอย่าง
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))

เอาต์พุต (เรียกใช้ครั้งที่สอง ขนาดกริดคือ 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))
