在Python中从离散像素值绘制图像的最快方法是什么? [英] What is the fastest way to draw an image from discrete pixel values in Python?

查看:222
本文介绍了在Python中从离散像素值绘制图像的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望根据计算出的像素值绘制图像,作为可视化某些数据的方法。基本上,我希望采用二维矩阵颜色三元组并渲染它。

I wish to draw an image based on computed pixel values, as a means to visualize some data. Essentially, I wish to take a 2-dimensional matrix of color triplets and render it.

请注意,这不是图像处理,因为我不是在转换现有图像,也不是进行任何形式的全图像转换,而且它也不是矢量图形我正在渲染的图像没有预先确定的结构 - 我可能会一次生成一个像素的无定形彩色斑点。

Do note that this is not image processing, since I'm not transforming an existing image nor doing any sort of whole-image transformations, and it's also not vector graphics as there is no pre-determined structure to the image I'm rendering- I'm probably going to be producing amorphous blobs of color one pixel at a time.

我现在需要渲染大约1kx1k像素的图像,但可扩展的东西会很有用。最终目标格式是PNG或任何其他无损格式。

I need to render images about 1kx1k pixels for now, but something scalable would be useful. Final target format is PNG or any other lossless format.

我目前通过ImageDraw的draw.point使用PIL,我很想知道,鉴于非常具体我需要相对基本的功能,是否有更快的库?

I've been using PIL at the moment via ImageDraw's draw.point , and I was wondering, given the very specific and relatively basic features I require, is there any faster library available?

推荐答案

如果你有可用的numpy和scipy(如果你有的话)在Python中操作大型数组,我推荐它们),然后scipy.misc.pilutil.toimage函数非常方便。
一个简单的例子:

If you have numpy and scipy available (and if you are manipulating large arrays in Python, I would recommend them), then the scipy.misc.pilutil.toimage function is very handy. A simple example:

import numpy as np
import scipy.misc as smp

# Create a 1024x1024x3 array of 8 bit unsigned integers
data = np.zeros( (1024,1024,3), dtype=np.uint8 )

data[512,512] = [254,0,0]       # Makes the middle pixel red
data[512,513] = [0,0,255]       # Makes the next pixel blue

img = smp.toimage( data )       # Create a PIL image
img.show()                      # View in default viewer

不错事情是toimage很好地处理不同的数据类型,所以浮点数的2d数组可以合理地转换为灰度等。

The nice thing is toimage copes with diferent data types very well, so a 2d array of floating point numbers gets sensibly converted to greyscale etc.

你可以从 http://docs.scipy.org/doc/scipy/reference/misc.html 。

这篇关于在Python中从离散像素值绘制图像的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆