Python 2.6:从数组创建图像 [英] Python 2.6: Creating image from array

查看:109
本文介绍了Python 2.6:从数组创建图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python新手在这里!因此,我有一个数据文件,该文件存储一个字节列表,表示图像中的像素值.我知道图像是3 x 3像素.到目前为止,这是我的代码:

Python rookie here! So, I have a data file which stores a list of bytes, representing pixel values in an image. I know that the image is 3-by-3 pixels. Here's my code so far:

# Part 1: read the data
data = []
file = open("test.dat", "rb")
for i in range(0, 9)
    byte = file.read(1)
    data[i] = byte
file.close()

# Part2: create the image
image = PIL.Image.frombytes('L', (3, 3), data)
image.save('image.bmp')

我有几个问题:

在第1部分中,这是读取二进制文件并将数据存储在数组中的最佳方法吗?

In part 1, is this the best way to read a binary file and store the data in an array?

在第2部分中,我收到错误消息"TypeError:必须是字符串或只读缓冲区,而不是列表.

In part 2, I get the error "TypeError: must be string or read-only buffer, not list.

这两个方面有帮助吗?

谢谢!

推荐答案

第1部分

如果您知道只需要9个字节的数据,那看起来是个很好的方法,尽管使用上下文管理器并跳过显式循环可能会更干净一些:

Part 1

If you know that you need exactly nine bytes of data, that looks like a fine way to do it, though it would probably be cleaner/clearer to use a context manager and skip the explicit loop:

with open('test.dat', 'rb') as infile:
    data = list(infile.read(9)) # read nine bytes and convert to a list

第2部分

根据文档,您必须传递给PIL.Image.frombytes的数据是:

Part 2

According to the documentation, the data you must pass to PIL.Image.frombytes is:

data –包含给定模式的原始数据的字节缓冲区.

data – A byte buffer containing raw data for the given mode.

列表不是字节缓冲区,因此您可能会浪费时间将输入转换为列表.我的猜测是,如果直接将其传递给字节串,那么您将获得所需的内容.这是我会尝试的:

A list isn't a byte buffer, so you're probably wasting your time converting the input to a list. My guess is that if you pass it the byte string directly, you'll get what you're looking for. This is what I'd try:

with open('test.dat', 'rb') as infile:
    data = infile.read(9) # Don't convert the bytestring to a list
image = PIL.Image.frombytes('L', (3, 3), data) # pass in the bytestring
image.save('image.bmp')

希望能有所帮助;显然我无法在这里进行测试,因为我不知道您的文件的内容是什么.

Hopefully that helps; obviously I can't test it over here since I don't know what the content of your file is.

当然,如果出于某些其他原因(确实令人怀疑,您确实需要将字节作为列表)(可疑-您可以像迭代列表一样遍历字符串),则始终可以在需要时将它们转换为列表(datalist = list(data))或在调用PIL时将它们连接成字符串:

Of course, if you really need the bytes as a list for some other reason (doubtful--you can iterate over a string just as well as a list), you can always either convert them to a list when you need it (datalist = list(data)) or join them into a string when you make the call to PIL:

image = PIL.Image.frombytes('L', (3, 3), ''.join(datalist))

第3部分

这是一个问题,但可能很重要:您知道您使用的是哪个版本的PIL吗?如果您使用的是真正原始的Python Imaging Library,那么您可能还会遇到该库的许多问题-自2009年以来,它是超级bug且不受支持.

Part 3

This is sort of an aside, but it's likely to be relevant: do you know what version of PIL you're using? If you're using the actual, original Python Imaging Library, you may also be running into some of the many problems with that library--it's super buggy and unsupported since about 2009.

如果您愿意,我高度建议您摆脱它,并抓住而是枕头叉,它是实时的功能版本.您无需更改任何代码(它仍会安装一个名为PIL的模块),但是Pillow库在跨越式方面优于原始的PIL.

If you are, I highly recommend getting rid of it and grabbing the Pillow fork instead, which is the live, functional version. You don't have to change any code (it still installs a module called PIL), but the Pillow library is superior to the original PIL by leaps and bounds.

这篇关于Python 2.6:从数组创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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