lheezy
lheezy

Reputation: 542

Problem reading hex data from image - python automatically converts to a string

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

Upvotes: 2

Views: 3189

Answers (5)

martineau
martineau

Reputation: 123481

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])

Upvotes: 2

Chris Morgan
Chris Morgan

Reputation: 90812

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.

Upvotes: 2

luispedro
luispedro

Reputation: 7024

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.

Upvotes: 0

the Tin Man
the Tin Man

Reputation: 160571

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.

Upvotes: 1

Thomas K
Thomas K

Reputation: 40350

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.

Upvotes: 4

Related Questions