从PythonMagick图像中获取像素数据? [英] Getting pixel data from a PythonMagick image?

查看:761
本文介绍了从PythonMagick图像中获取像素数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何从PythonMagick.Image实例获取像素数据而不必先将其写入磁盘?

Does anyone know a way get the pixel data from a PythonMagick.Image instance without having to write it to disk first?

例如,我可以读取使用图像:

For instance, I can read in an image using:

import PythonMagick
im = PythonMagick.Image('image.jp2')

我现在希望能够获得未压缩的图像数据,以便我可以在其他内容中使用它,如NumPy或matplotlib ,但我似乎无法找到任何方法来做到这一点。我只是直接使用matplotlib或PIL,但我正在阅读的图像格式是JPEG 2000,据我所知只有PythonMagick支持。

I would now like to be able to get the uncompressed image data so that I can use it in something else like NumPy or matplotlib, but I can't seem to find any way to do this. I would just use matplotlib or PIL directly but the image format I'm reading in is JPEG 2000 which is only supported by PythonMagick as far as I know.

有什么建议吗?

推荐答案

免责声明:我现在没有构建PythonMagick并且不是专家,(1)以下任何或所有可能都是错误的,(2)它肯定不会比你想要的那么具体,(3)如果其他人知道的更好,我希望他们不会因为看到而被推迟这里已经有了答案。无论如何:

Disclaimer: I don't have PythonMagick built where I am right now and am no expert, so (1) any or all of the following may be wrong, (2) it will certainly be less specific than you'd like, and (3) if someone else knows better I hope they won't be put off by seeing an answer already here. Anyway:

通过快速查看代码,看起来好像你可以使用 pixelColor 图像类上的$ c>方法。这将返回 PythonMagick.Color 值,您可以从中提取R,G,B组件。底层的C ++库支持使用 Image :: writePixels 一次读出大量像素,这也出现在 PythonMagick.Image ;但我认为正确使用该方法取决于PythonMagick中未实现的其他内容。很遗憾,因为我打赌它会比一次读取一个像素更有效率。

From a quick look at the code, it looks as if you can read pixel values one by one using the pixelColor method on the Image class. This returns a PythonMagick.Color value, from which you can extract R,G,B components. The underlying C++ library supports reading out lots of pixels at a time using Image::writePixels, which is also present in PythonMagick.Image; but I think the proper use of that method depends on other things that aren't implemented in PythonMagick. That's a pity, because I bet it would have been much much more efficient than reading one pixel at a time.

另外也许更好,看起来好像你可以写内容中的图像内容为 PythonMagick.Blob 对象,基本上与仅在没有文件的情况下写入文件相同:-)。您可以选择应该写入的格式,就像写入文件时一样。似乎有一些名为 get_blob_data 的东西用于提取 Blob 的内容。这样的事情:

Alternatively and probably better, it looks as if you can write the contents of the image to a PythonMagick.Blob object in memory, which basically does the same as writing to a file only without the file :-). You can choose what format it should write in, just as you do when writing to a file. There seems to be something called get_blob_data for extracting the contents of a Blob. Something like this:

im = PythonMagick.Image('image.jp2')
blob = PythonMagick.Blob()
im.write(blob, "png")
data = PythonMagick.get_blob_data(blob)

我认为生成的数据是一个Python字符串,其字节是图像的二进制表示。 (我假设您正在使用Python 2.x,其中字符串类型为8位。我不知道PythonMagick是否与3.x一起使用。)我认为有些格式基本上是原始像素数据;尝试RGB。然后,您可以通过大量 struct.unpack 或其他内容提取内容。

The resulting data is, I think, a Python string whose bytes are the binary representation of the image. (I'm assuming you're using Python 2.x, where the string type is 8-bit. I don't know whether PythonMagick works with 3.x.) I think there are some formats that are basically raw pixel data; try "RGB". You can then extract the contents via lots of struct.unpack or whatever.

这篇关于从PythonMagick图像中获取像素数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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