从图像读取十六进制数据时出现问题 - python 自动转换为字符串

发布于 2024-10-03 19:53:11 字数 345 浏览 1 评论 0原文

我正在使用 read(1) 一次一个字节地读取图像,并将其附加到列表中。图像数据都是十六进制数据。当我使用 print 函数打印列表时,它的格式为 '\xd7'

['\xd7', '\xd7', '\xd7', '\xd7', '\xd7', '\xd7', '\xd7',...]

问题是现在我需要对此十六进制数据执行一些计算,但是,它是字符串格式,而Python中的任何int或hex转换函数都不支持这种'\xd'字符串格式。它们需要 '0xd7' 或仅需要 'd7'

感谢您的帮助

I am reading in an image one byte at a time with with read(1), and appending it to a list. The image data is all hex data. When I print out the list with the print function it is in the format '\xd7'

['\xd7', '\xd7', '\xd7', '\xd7', '\xd7', '\xd7', '\xd7',...]

The problem is that now I need to perform some calculations on this hex data, however, it is in string format, and this '\xd' string format isn't supported by any of the int or hex conversion functions in python. They require a '0xd7' or just a 'd7'.

Thanks for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苏辞 2024-10-10 19:53:11

它将它们解释为字符,因此使用 ord 将它们转换为数字。即 ord('\xd7') 给出 215。

此外,如果您使用 Windows,或者程序可能必须在 Windows 上运行,请确保您已以二进制模式打开文件:打开(“imagefile.png”,“rb”)。在其他操作系统上没有区别。

It's interpreting them as characters, so use ord to turn them into numbers. I.e. ord('\xd7') gives 215.

Also if you use Windows, or the program might have to run on Windows, make sure that you've got the file open in binary mode: open("imagefile.png","rb"). Makes no difference on other operating systems.

垂暮老矣 2024-10-10 19:53:11

如果您需要 'd7''0xd7',而不是简单的 0xd7(即 215),hex() 或 '%x' 是您的朋友。

>>> ord('\xd7')
215
>>> ord('\xd7') == 215 == 0xd7
True
>>> hex(ord('\xd7'))
'0xd7'
>>> '%x' % ord('\xd7')
'd7'

另外,正如在其他答案中观察到的那样,请确保在模式中使用“b”打开,否则它可能会变得混乱,认为它是 UTF-8 或类似的东西,在某些字节序列上。

If you require 'd7' or '0xd7', rather than simply 0xd7 (viz, 215), hex() or '%x' are your friend.

>>> ord('\xd7')
215
>>> ord('\xd7') == 215 == 0xd7
True
>>> hex(ord('\xd7'))
'0xd7'
>>> '%x' % ord('\xd7')
'd7'

Also as observed in other answers, do make sure you open with the 'b' in the mode, otherwise it can get messed up, thinking it's UTF-8 or something like that, on certain sequences of bytes.

冷了相思 2024-10-10 19:53:11

您可以执行以下操作将它们放入数字数组中:

import array

data = array.array('B') # array of unsigned bytes

with open("test.dat", 'rb') as input:
    data = input.read(100)
    data.fromstring(data)

print data
# array('B', [215, 215, 215, 215, 215, 215, 215])

You could do something like this to get them into a numeric array:

import array

data = array.array('B') # array of unsigned bytes

with open("test.dat", 'rb') as input:
    data = input.read(100)
    data.fromstring(data)

print data
# array('B', [215, 215, 215, 215, 215, 215, 215])
§对你不离不弃 2024-10-10 19:53:11

read() 可以采用大于 1 的大小值:read(1024) 将从流中读取 1K 字节。这比一次读取一个字节并将其附加到前面的字节要快得多。

打印数据时你想做什么?查看字节值,还是显示图像?

数据不是“字符串格式”,它只是字节,但是当您打印它们时,打印例程会将非打印值转义为对人眼和大脑更有意义的东西。如果您想查看不转义的值,您可以迭代字节并将它们转换为十六进制值、十进制值或二进制值 - 无论什么对您和您的应用程序有效。 字符串格式化迷你语言将是一个很好的开始地方。

read() can take a size value larger than 1: read(1024) will read 1K worth of bytes from the stream. That will be a lot faster than reading a byte at a time and appending it to the previous bytes.

What are you trying to do when printing the data? See the byte values, or display the image?

The data isn't in "string format", it's just bytes, but when you print them the print routine will escape non-printing values into something that will mean more to human eyes and brains. If you want to see the values without the escaping you can iterate over the bytes and convert them to their hexadecimal values, or decimal, or binary - whatever works for you and your application. The string formatting mini-language will be a good starting place.

小巷里的女流氓 2024-10-10 19:53:11

如果你正在进行图像处理,那么你可能想看看 numpy。

还有一些软件包可以帮助您将图像读入内存(上面提到了 PIL,另一个是我自己的 mahotas< /a> 或 scikits.image)。

如果数据作为原始数据存在于文件中并且您知道尺寸,则可以执行以下

import numpy as np
img = np.empty( (n_rows, n_cols), dtype=np.uint8) # create an empty image
img.data[:] = input_file.read()

操作将数据放入 img 中。

python 图像处理的介绍网站是 http://pythonvision.org

If you are doing image processing, then you probably want to look at numpy.

There are a few packages that will help you read your image into memory too (PIL is mentioned above, another is my own mahotas or scikits.image).

If the data is in a file as raw data an you know the dimensions, you can do the following

import numpy as np
img = np.empty( (n_rows, n_cols), dtype=np.uint8) # create an empty image
img.data[:] = input_file.read()

to get your data into img.

An introductory website for image processing in python is http://pythonvision.org.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文