簡體   English   中英

使用PIL和putdata()繪制像素數據

[英]Plotting pixel data using PIL and putdata()

我正在嘗試將一系列像素繪制到圖像上,並且正在考慮使用putdata()比使用putpixel()繪制每個像素要快得多的假設。

下面的代碼片段確實可以正常運行,但是速度很慢。 我現在正在嘗試將所有像素和顏色值都放入可以與putdata()配合使用的序列對象中,但是對於python來說還很陌生,並且努力以正確的順序獲取像素坐標和rgb顏色。

謝謝,

對於每個像素,我們循環遍歷它,從圖中查找“密度”(按x坐標,然后按y列出),並將其與顏色的rgb值進行匹配,其基本上是繪制彩色散點圖。

    plots = {800: {2400: 2, 1900: 2, 2000: 4, 2100: 5, 2200: 2, 2300: 1}, 1200: {2100: 1}, 900: {2100: 1, 1900: 1}, 1000: {2600: 1, 2100: 1, 1900: 1}, 300: {2000: 2, 2200: 2, 1200: 1, 2100: 1, 2500: 1}, 1100: {2000: 2, 2500: 1, 1900: 1}, 400: {2500: 1, 1800: 5, 1900: 4, 2000: 2, 2600: 1, 2200: 1}, 200: {2000: 1, 2600: 2, 2700: 2, 2500: 1}, 1300: {2000: 2}, 600: {2800: 2, 1800: 5, 1900: 2, 2000: 3, 2100: 5, 2200: 2}, 500: {2400: 6, 2500: 2, 2600: 1, 1900: 6, 2700: 1, 2000: 4, 2100: 2, 2300: 5}, 700: {2000: 9, 1900: 5, 2100: 2, 2800: 1, 1800: 7}}

image = Image.new('RGB', imgsize, rgbcolour)

    # Set colour values for each density
    colours = {0:rgbcolour,1:(125, 60, 218),2:(234, 163, 240),3:(66, 74, 145),4:(59, 165, 227),5:(0, 175, 89),6:(74, 224, 194),7:(255, 230, 66),8:(246, 148, 55),9:(255, 58, 239),10:(243, 36, 62)}

    # Loop through all pixels and plot them
    for x in xrange(roundup(column_stats['f2'][MAX])):
        for y in xrange(roundup(column_stats['f1'][MAX])):
            x_plot = int(round(float(x),-2))
            y_plot = int(round(float(y),-2))
            if x_plot in plots:
                if y_plot in plots[x_plot]:
                    image.putpixel((x,y),colours.get(plots[x_plot][y_plot], (243, 36, 62)))

image.save(name+'.png', "PNG")

一些循環優化:

# Loop through all pixels and plot them
x_max = roundup(column_stats['f2'][MAX])
y_max = roundup(column_stats['f1'][MAX])
for x in xrange(x_max):
    x_plot = int(round(float(x),-2))
    if x_plot in plots:
        plots_x = plots[x_plot]
        for y in xrange(y_max):
            y_plot = int(round(float(y),-2))
            if y_plot in plots_x:
                image.putpixel((x,y),colours.get(plots_x[y_plot], (243, 36, 62)))

另外,將colours更改為簡單列表(數組)。 避免使用字典,因為查找速度較慢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM