1

I have a text file that contains image data (1 picture) in the format of hexadecimal.

12:0a:11:0b:08:18:0d:0e
0e:0c:14:12:0a:0d:10:0e
0f:12:0b:0e:0c:0e:0a:14
11:13:0c:0e:13:0e:0a:10
0e:0c:11:08:0c:0e:0f:0e
0c:0c:0b:12:06:10:0e:0e........and so on

The data is taken by an integrated serial camera (ucam). This picture is 80x60 8 bit grey scale RAW image. I want to convert the data into an image. Any ideas how to do that in Python?

furas
  • 134,197
  • 12
  • 106
  • 148
  • 1
    possible duplicate of [how to convert raw images to png in python?](http://stackoverflow.com/questions/9922402/how-to-convert-raw-images-to-png-in-python) – njzk2 Jul 15 '14 at 19:10
  • The raw data looks like it broken up into groups of eight. Does that mean anything? Does the file contain newlines? If you could paste the data for an entire sample image into your question it would be useful. – martineau Jul 15 '14 at 19:19
  • @njzk2 The person is asking how to convert "RAW image" to png format, not from "RAW data". He's already got an image. – user3826526 Jul 15 '14 at 19:22
  • they actually have a file that contains an array that is raw data. (if you read the code in the answer it is pretty clear.) – njzk2 Jul 15 '14 at 19:24
  • @martineau I only posted a section of the data because there are 600 lines of them. – user3826526 Jul 15 '14 at 19:26

2 Answers2

0

Maybe there are better solutions but I think it can be done with PyGame

Because there is only part of data so I use it repeatedly to get full image 80x60.
With full data it have to be change.

data ='''12:0a:11:0b:08:18:0d:0e
0e:0c:14:12:0a:0d:10:0e
0f:12:0b:0e:0c:0e:0a:14
11:13:0c:0e:13:0e:0a:10
0e:0c:11:08:0c:0e:0f:0e'''

sf = pygame.Surface((80,60), depth=24)

x = 0
y = 0

for __ in range(120): # repeat the same data
    for row in data.splitlines():
        for pixel in row.split(':'):
            c = int(pixel,16)
            sf.set_at((x,y), pygame.Color(c,c,c))
            x += 1
            if x == 80:
                x = 0
                y += 1

pygame.image.save(sf, 'new.png')

You will have to resize image on the screen to see some (grey) colors

enter image description here

furas
  • 134,197
  • 12
  • 106
  • 148
0

If you convert the textual representation into raw bytes, you can use PIL's Image.fromstring to convert it into an image.

from PIL import Image
values = [v for line in data.split() for v in line.split(':')]
bstr = ''.join(chr(int(v, 16)) for v in values)
im = Image.fromstring('L', (80, 60), bstr)
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622