天天看點

python線條顔色不同_python-Matplotlib:用不同的顔色繪制許多斷開的線段

我在Python 3上嘗試了很多可用的2D渲染引擎,同時為面向圖像的Deep Learning&GAN的輸出階段尋找快速解決方案。

使用以下基準測試:在有和沒有抗鋸齒的情況下,将99行渲染為256x256屏外圖像(或更有效的效果)的時間。

結果以舊版x301筆記本電腦上的效率為順序:

PyGtk2:〜2500 FPS(Python 2,GTK 2,不确定如何擷取AA)

PyQt5:〜1200 FPS,使用抗鋸齒約為350

PyQt4:約1100 FPS,約380 AA

開羅:〜750 FPS,使用AA時約為250(使用“ FAST” AA時速度稍快)

PIL:〜600 FPS

基線是一個循環,大約需要0.1毫秒(10,000 FPS)來檢索随機數并調用原語。

PyGtk2的基本代碼:

from gtk import gdk

import random

WIDTH = 256

def r255(): return int(256.0*random.random())

cmap = gdk.Colormap(gdk.visual_get_best_with_depth(24), True)

black = cmap.alloc_color('black')

white = cmap.alloc_color('white')

pixmap = gdk.Pixmap(None, WIDTH, WIDTH, 24)

pixmap.set_colormap(cmap)

gc = pixmap.new_gc(black, line_width=2)

pixmap.draw_rectangle(gc, True, -1, -1, WIDTH+2, WIDTH+2);

gc.set_foreground(white)

for n in range(99):

pixmap.draw_line(gc, r255(), r255(), r255(), r255())

gdk.Pixbuf(gdk.COLORSPACE_RGB, False, 8, WIDTH, WIDTH

).get_from_drawable(pixmap, cmap, 0,0, 0,0, WIDTH, WIDTH

).save('Gdk2-lines.png','png')

這是針對PyQt5的:

from PyQt5.QtCore import Qt

from PyQt5.QtGui import *

import random

WIDTH = 256.0

def r255(): return WIDTH*random.random()

image = QImage(WIDTH, WIDTH, QImage.Format_RGB16)

painter = QPainter()

image.fill(Qt.black)

painter.begin(image)

painter.setPen(QPen(Qt.white, 2))

#painter.setRenderHint(QPainter.Antialiasing)

for n in range(99):

painter.drawLine(WIDTH*r0to1(),WIDTH*r0to1(),WIDTH*r0to1(),WIDTH*r0to1())

painter.end()

image.save('Qt5-lines.png', 'png')

以下是Python3-Cairo的完整性:

import cairo

from random import random as r0to1

WIDTH, HEIGHT = 256, 256

surface = cairo.ImageSurface(cairo.FORMAT_A8, WIDTH, HEIGHT)

ctx = cairo.Context(surface)

ctx.scale(WIDTH, HEIGHT) # Normalizing the canvas

ctx.set_line_width(0.01)

ctx.set_source_rgb(1.0, 1.0, 1.0)

ctx.set_antialias(cairo.ANTIALIAS_NONE)

#ctx.set_antialias(cairo.ANTIALIAS_FAST)

ctx.set_operator(cairo.OPERATOR_CLEAR)

ctx.paint()

ctx.set_operator(cairo.OPERATOR_SOURCE)

for n in range(99):

ctx.move_to(r0to1(), r0to1())

ctx.line_to(r0to1(), r0to1())

ctx.stroke()

surface.write_to_png('Cairo-lines.png')