Ask Your Question
1

What is the process of converting a .png picture into tessellation using Python?

asked 2022-05-11 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-06-27 16:00:00 +0000

plato gravatar image

There are multiple ways to approach this problem using Python, but one possible solution is:

  1. Import the required libraries:
import numpy as np import cv2 from itertools import cycle 
  1. Load the .png image into a Numpy array:
image = cv2.imread('image.png') 
  1. Convert the image to grayscale:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
  1. Define the tessellation pattern using a list of shapes (e.g., squares and triangles):
shapes = ['square', 'triangle'] 
  1. Define the size and spacing of the patterns:
size = 20 spacing = 5 
  1. Loop through the grayscale image and apply the tessellation pattern:
tessellated_image = np.zeros_like(gray) for i, row in enumerate(range(0, gray.shape[0], size + spacing)): shape_cycle = cycle(shapes) for j, col in enumerate(range(0, gray.shape[1], size + spacing)): shape = next(shape_cycle) if shape == 'square': tessellated_image[row:row+size, col:col+size] = gray[row:row+size, col:col+size] elif shape == 'triangle': if i % 2 == 0: pts = np.array([(col, row), (col+size, row), (col+size//2, row+size)]) else: pts = np.array([(col, row+size), (col+size, row+size), (col+size//2, row)]) mask = np.zeros_like(gray) cv2.fillPoly(mask, [pts], 255) tessellated_image += cv2.bitwise_and(mask, gray) 
  1. Save the tessellated image:
cv2.imwrite('tessellated_image.png', tessellated_image) 

Note that this code only implements a very basic tessellation pattern, and more complex patterns can be achieved by modifying the shape list and the shape drawing code in step 6. Additionally, this code does not optimize the tessellation for efficiency or visual quality, and further adjustments may be required depending on the specific use case.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-11 11:00:00 +0000

Seen: 10 times

Last updated: Jun 27 '21