我如何将像素/ numpy数组转换为矢量点?

问题描述 投票:0回答:1

我想将灰度图片的像素值转换为矢量点,以便像素的灰度值确定相应点的半径。但是我完全陷入困境,该项目必须在星期日完成,目前我真的很拼命

背景:对于我的大学课程“ python入门”,我想用python构建“ rasterbator”(https://rasterbator.net/)的无耻副本(但以一种更为原始的方式)。

我想如何处理?:我用PIL加载图像,使其成为灰度图像并将其转换为numpy数组。然后,我将阵列切成许多小的正方形阵列(每个预期点一个分段),计算每个阵列的平均值,然后将其放回到一个比原始阵列小得多的阵列中。到那时为止,我能够做到(但是花了我很长时间)。现在,我想用点“替换”像素并创建多个PDF,以便您可以将其打印,粘贴在一起并制作一张大海报。

这种方法行得通吗?还是我吠错了树?

我是一名流血的python初学者。 python的问题对我来说,有太多我不知道的模块。答案可能很简单,但我根本不知道在哪里看。如果有人告诉我,我是朝正确的方向还是指向正确的方向,我将不胜感激。

非常感谢

这里是我到目前为止管理的代码(虽然不是很多)

from PIL import Image as img
import numpy as np

greyscale = np.asarray(img.open("test.jpg").convert("L")) #load picture into array and make it greyscale

end_width = 1500 # chosen width of final picture in mm (will be with kwargs later on)
dot_size = 13 #chosen dot-size of final pictutre in mm (will be with kwargs later on)
estimate_dot_count_x = int(np.ceil(end_width/dot_size)) # estimates the "horizontal resolution"

pixel_in_segment = int(np.ceil(greyscale.shape[1]/estimate_dot_count_x)) #calculates the edge length of a segment
W=pixel_in_segment #just for shorter formular later on 

estimate_dot_count_y = int(np.ceil(greyscale.shape[0]/pixel_in_segment)) # estimates the "vertical resolution"
final_dot_count_x=int(np.ceil(greyscale.shape[1]/W)) #final horizontal resolution for shape of new array
final_dot_count_y=int(np.ceil(greyscale.shape[0]/W)) #final vertical resolution for shape of new array
#slice array into multiple pieces
tiles = [greyscale[x:x+W,y:y+W] for x in range(0,greyscale.shape[0],W) for y in range(0,greyscale.shape[1],W)]
#calculate mean values of each segment an safe it to list
average_list = []
for pixel in tiles:
    result=int(np.mean(pixel))
    average_list.append(result)
#convert list back into an array
downscale=np.asarray(average_list, dtype=int).reshape(final_dot_count_y,final_dot_count_x)

编辑:我以某种方式设法将数组绘制为矢量点:

#inverse and normalize gray value so That I can multiply with max dot size
for ix,iy in np.ndindex(downscale.shape):
    downscale[ix,iy]= float(1-downscale[ix,iy]*(1/255))

reportlab是我一直在寻找的钥匙...

from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
#making dots
def printing(c):
    c.translate(spacing*0.5,imh-(spacing*0.5))
    for ix,iy in np.ndindex(downscale.shape):
       c.circle(iy*(spacing), ix*(-spacing), downscale[ix, iy]*max_dot_size, stroke=1, fill=1)
c = canvas.Canvas("hello.pdf", pagesize=(imwidth, imhight))
printing(c)
c.showPage()
c.save()

这提出了一个问题:我如何告诉reportlab,我想用一个普通的打印机格式(“ letter”或“ A4”)将这个大画布(尺寸为2m x1.5m)打印到多页上?

arrays python-3.x image vector-graphics reportlab
1个回答
0
投票

而不是“将图像切成小方块并计算每个方块的均值” ...如果您希望将80个点向下乘60个点,只需使用resize(),如下所示:

im_resized = im.resize((80, 60))
© www.soinside.com 2019 - 2024. All rights reserved.