我想将图像拆分为三角形瓷砖(等边)。我尝试使用https://alexwlchan.net/2016/10/tiling-the-plane-with-pillow/中的函数生成三角形的坐标。我的代码:
#import opencv
import math
image_path="/content/newspaper-icon-in-transparent-style-news-on-vector-25591681.jpg"
#Create Triangles
# https://alexwlchan.net/2016/10/tiling-the-plane-with-pillow/
#A horrizontal offset is added to ensure that images line up
#https://stackoverflow.com/questions/22588074/polygon-crop-clip-using-python-pil
def generate_coordinates_for_unit_triangles(image_width,image_height):
image_width=50;
image_height=50;
h=math.sin(math.pi/3)
for x in range(image_width):
for y in range(int(image_height / h)):
first_c,second_c,third_c=(x, y * h), (x+1, y * h), ((x+0.5, (y+1) * h))
first_sc, second_sc,third_sc=(x+1, y * h), (x+1.5, (y+1) * h), (x+0.5, (y+1) * h)
return first_c, second_c,third_c, first_sc, second_sc,third_sc
#return [(x, y * h), (x+1, y * h), (x+0.5, (y+1) * h)] ,[(x+1, y * h), (x+1.5, (y+1) * h), (x+0.5, (y+1) * h)]
##Generates the two triangles coordinates
first_c, second_c,third_c, first_sc, second_sc,third_sc=generate_coordinates_for_unit_triangles(50,50)
#convert image into numpy array
image_read=Image.open(image_path)
image_to_numpy=np.asarray(image_read)
shape_of_array=image_to_numpy.shape
print(shape_of_array)
mask_image=[first_c, second_c,third_c, first_sc, second_sc,third_sc]
我意识到这可能无法给出我想要的输出。
预期输入输出如下: [预期输入输出][1]
任何有关如何解决该问题的指导将不胜感激。[1]:https ://i.stack.imgur.com/vr7rV.jpg