English 中文(简体)
如何阅读二进制文件, 将数据转换成图像?
原标题:How can I read a binary file and turn the data into an image?

我想做的基本上就是拿一个文件, 将其二进制数据( 当然的十进制) 带入列表, 然后使用基于此列表的 PIL 生成灰度位图图像 。

例如,如果文件为5000字节(图像大小为100x50),而每个字节的整数介于0至255之间,我想将第一个字节画到第一个像素,然后下行直到所有字节都用尽为止。

到目前为止我唯一得到的 就是读取文件在:

f = open(file,  rb )
text = f.read()
for s in text:
    print(s)

此输出以小数表示的字节。

我在寻找如何完成这个任务的方向。我做了很多搜索,但似乎没有太多人试图做我想做的事。

任何帮助都将不胜感激!

问题回答

您可以使用来自文件 () 的 Numpy s 来高效读取它 :

#!/usr/bin/env python3

import numpy as np
from PIL import Image

# Define width and height
w, h = 50, 100

# Read file using numpy "fromfile()"
with open( data.bin , mode= rb ) as f:
    d = np.fromfile(f,dtype=np.uint8,count=w*h).reshape(h,w)

# Make into PIL Image and save
PILimage = Image.fromarray(d)
PILimage.save( result.png )

Keywords :PIL、Pillow、Python、Numpy,读原始、二进制、8比特、灰度灰度、图像、图像处理。

我不认为为此使用 PIL 将会非常有效, 但是您可以查看 < a href=' http://www.pythonware.com/library/pil/ handbook/imageraw.htm' rel="nofollow"\\ code> ImagyDraw 模块, 如果您想在空白画布上画画的话 。

我的方法会有点不同:因为您的文件格式类似于 Netpbm 格式,我会试着转换它。为了简单起见,在阅读时尝试添加/管理您格式的页眉,以便 PIL 能够本地阅读它 。

我认为应该这样。 scipy 是一个选项吗?

In [34]: f = open( image.bin ,  r )

In [35]: Y = scipy.zeros((100, 50))

In [38]: for i in range(100):
             for j in range(50):
                 Y[i,j] = ord(f.read(1))

In [39]: scipy.misc.imsave( image.bmp , Y)

https://web.archive.org/web/19980119020126/http://www.pythonware.com/library/pil/handbook/image.htm" rel=“nofollow”>PIL 图像文件:

Image.fromstring(mode, size, data)

例如:

im = Image.fromstring( L , (100, 50), text)

s 还有一个来自buffer 的 < code> 函数,但差异并不明显。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签