Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การนำ Photomosaics ไปใช้ใน Python

Photomosaic เป็นเทคนิคที่เราสามารถแบ่งภาพของเราเป็นกริดของสี่เหลี่ยม แต่ละสี่เหลี่ยมจะถูกแทนที่ด้วยภาพหรือสีอื่นๆ ดังนั้นเมื่อเราต้องการดูภาพจริงจากระยะหนึ่ง เราจะเห็นภาพจริง แต่ถ้าเราเข้าใกล้ เราจะเห็นกริดของบล็อกสีต่างๆ

ในกรณีนี้เราใช้โมดูล Python ชื่อ photomosaic ด้วยโมดูลนี้ เราสามารถสร้าง photomosaics ได้ง่าย ติดตั้งได้จากลิงก์นี้ จะดาวน์โหลดโมดูล scikit learn มาด้วย

sudo pip3 install photomosaic

โมดูลนี้มีคุณสมบัติบางอย่าง ดังนี้:

  • สามารถใช้ขนาด tiles ต่างๆ ได้
  • ตั้งค่า tiles เล็กกว่าสำหรับส่วนที่ละเอียดของภาพ
  • ใช้ flickr api เพื่อดึงชุดภาพขนาดใหญ่มาใช้เป็น tiles

ในบทความนี้ เราจะดูวิธีนำโมดูลนี้ไปใช้สำหรับ photomosaics อย่างง่าย

เราใช้ภาพจาก skimage library

Main Image

การนำ Photomosaics ไปใช้ใน Python

ขั้นตอนการสร้าง photomosaic

  • นำภาพจริงมา (ที่นี่ภาพจาก skimage library)
  • กำหนดขนาด grid tile
  • ให้ตำแหน่งสร้างบล็อก RGB สีสันสดใสเป็น pool
  • ตั้งค่าโฟลเดอร์เป็น Pool สำหรับ photomosaic
  • แปลงเป็น photomosaic โดยใช้ pool และ grid size
  • บันทึกภาพ
  • ออก

ตัวอย่างโค้ด

from skimage.io import *
import sys
import photomosaic as phmos
from skimage import data

image = data.coffee()  # Get coffee image from skimage
# Get mosaic size from command line argument
mos_size = (int(sys.argv[1]), int(sys.argv[2]))

# สร้าง image squares ทั้งหมดและ generate pool
phmos.rainbow_of_squares('square/')
square_pool = phmos.make_pool('square/*.png')

# สร้าง mosaic image และบันทึก
mosaic = phmos.basic_mosaic(image, square_pool, mos_size)
imsave('mosaic_op.png', mosaic)

ผลลัพธ์ (รันครั้งแรก Grid size 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]
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]
การนำ Photomosaics ไปใช้ใน Python

ผลลัพธ์ (รันครั้งที่สอง Grid size 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]
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]
การนำ Photomosaics ไปใช้ใน Python