3

I have an raw binary image stored as a .bin file and it has some important data on it. The problem is that the color information is slightly off so I need to alter it a little bit. Is there any way I can get in and multiply the R, G, and B values by a scalar and then save it back as a raw binary file?

I am hoping to use the python imaging library to do this as I already know the basics of the image module. I need to multiply every pixel by the same value but it will be a different value for R, G, and B. I have the following code to open the file but then I don't know what to do there to alter the RGB values.

fileName = raw_input("Enter a file name: ")

with open(fileName) as f:
    im = Image.fromstring('RGB', (3032, 2016), f.read())

Let me know if you need any more information.

UPDATE:

I wrote the following code which is converting the image in a way that I would like but it gives me an error. The code is as follows:

with open(C:\Users\name\imagedata\visiblespec.bin, 'rb') as f:
    im = Image.fromstring('RGB', (3032, 2016), f.read())

im = im.convert('RGB')
r, g, b = im.split()
r = r.point(lambda i: i * (255/171))
g = g.point(lambda i: i * (255/107))
b = b.point(lambda i: i * (255/157))
out = Image.merge('RGB', (r, g, b))


out.save(C:\Users\name\imagedata\visiblespecredone.bin)

and my error is this:

Traceback (most recent call last):
  File "C:\Users\Patrick\workspace\colorCorrect\src\rawImage.py", line 18, in <module>
    im = Image.fromstring('RGB', (3032, 2016), f.read()) 
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1797, in fromstring
    im.fromstring(data, decoder_name, args)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 594, in fromstring
    raise ValueError("not enough image data")
ValueError: not enough image data

This may be a completely wrong way to edit the RGB values, I know it works for a JPEG and t may only work for a JPEG but this is what I would like to do.

8
  • 1
    Could you elaborate on the format of your raw binary file? Commented Jun 6, 2012 at 22:33
  • 16 bit unsigned integers around 11 mbs and written in hexadecimal Commented Jun 6, 2012 at 22:41
  • It would appear to be that your file size is either not (or not being read as) a correct multiple of RGB values. Try .frombuffer method and check the size is what you expect? Commented Jun 6, 2012 at 23:29
  • For binary data: 3032 x 2016 = 6,112,512 pixels x 3 bytes per pixel (RGB) = 18,337,536 bytes = 17.488 MB, so 11 MB sounds too small (as in not enough image data). Commented Jun 7, 2012 at 2:17
  • Indeed is does sound like a 15 or 16 bit RGB format. Unfortunately the only raw modes that PIL v1.1.7 handles (as defined in the unpack.c file) are BGR;15 or BGR;16 which it will converted to RGB888. Commented Jun 7, 2012 at 2:42

2 Answers 2

2
import numpy as np

shape = (2016, 3032)
im = np.fromfile('visiblespec.bin', 'uint16').reshape(shape)

def tile(pattern, shape):
    return np.tile(np.array(pattern, 'bool'), (shape[0] // 2, shape[1] // 2))

r = tile([[0, 0], [0, 1]], shape)
g = tile([[0, 1], [1, 0]], shape)
b = tile([[1, 0], [0, 0]], shape)

im = im.astype('float32')
im[r] *= 255 / 171.
im[g] *= 255 / 107.
im[b] *= 255 / 157.
np.rint(im, out=im)
np.clip(im, 0, 65535, out=im)
im = im.astype('uint16')

im.tofile('visiblespecredone.bin')
Sign up to request clarification or add additional context in comments.

Comments

0

You might want to look at ImageMagick with the Python bindings or PIL.

If you just need to read the file and manipulate the binary data, do something like:

with open(filename, 'rb') as img:
   wrd=img.read(2)
   while wrd:
       # wrd == 2 bytes == your 16 bits...

6 Comments

yeah I'm using PIL right now but can't quite figure out how to edit specific values with it but I'm going through the documentation again
Doing it this way would be very slow if done in pure Python for an image of this size.
@martineau: maybe, but the underlying OS will buffer this more than most give credit for.
@drewk: Buffering isn't going to eliminate the bit manipulations and scaling of three component values needed per pixel -- not to mention those likely necessary to write the image back out (in its original format).
@martineau: Agreed. It is not an ideal solution, but bit-by-bit with PIL is not going to be a speed demon either. It sounds like he has an image size error in any case, and this does not really apply.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.