4

I have an raw image file that is .bin and is composed of 16 bits unsigned intergers. Can the python imaging library take this type of file and process it? My code is not running properly and giving me an invalid file type error but I think it may be an error in the coding rather than just that it doesn't take this file type.

Any knowledge on this out there?

clifgray
  • 4,313
  • 11
  • 67
  • 116
  • Are these 16-bit unsigned integers representing gray values or do you take 3 consecutive ones to provide RGB? – Mark Ransom Jun 05 '12 at 17:07
  • they represent gray values and then i'm putting them through a bayer filter to get color. the problem is that after the filter everything is coming out slightly discolored so I want to change the raw data a bit to see what it does – clifgray Jun 05 '12 at 17:13

1 Answers1

3

Assuming your file doesn't have a header and is tightly packed, try the following:

with open('filename', 'rb') as f:
    im = Image.fromstring('L;16', (width, height), f.read()) # also try 'L;16B', 'I;16', and 'I;16B'
im.show()

The 'L' formats will truncate from 16 bits per pixel to 8; the 'I' formats will keep it at 16 bits per pixel but might be harder to work with.

If your raw file is encoded in some way you'll have to search for documentation on it, since raw formats aren't standardized at all. With a .bin extension I doubt that's the case.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • it is telling me that I can't so the open method with a file and that I need a string. then when I try to use the tostring() method it says that tostring() can not be used on a string. are you sure .bin files work I can't find it anywhere in the documentation. – clifgray Jun 05 '12 at 20:03
  • 1
    @clifgray, sorry I should have realized it wouldn't take a file directly. I fixed the example, try it now. As for `.bin` files, they aren't standardized either but it's usually used for files with no format. – Mark Ransom Jun 05 '12 at 20:08