Convert binary file to image

binary, image, image-processing, matrix, wget

Solution

If you have python with the PIL (Image) library installed:

import Image
def colormap(s):
    s_out = []
    for ch in s:   # assume always '\x00' or '\x01'
        if s == '\x00':
            s_out.append('\x00')  # black
        else:
            s_out.append('\xFF')  # white
    return ''.join(s_out)

N= 50   # for instance
fin = open('myfile.bin','rb')
data = fin.read(N*N)    # read NxN bytes
data = colormap(data)

# convert string to grayscale image

img = Image.fromstring('L', (N,N), data )
# save to file
img.save('thisfile.png')

data = fin.read(N*N)   # next NxN bytes
data = colormap(data)

img = Image.fromstring('L', (N,N), data )
img.save('thisfile2.png')

This can be easily modified to loop and sequence filenames, etc as needed

Problem

I need to find a fast way to convert a binary file to an image. The binary file consist of a NNN matrix and I want to associate 0 to a color and 1 to a different color. I need to perform this operation to more then 1000 binary files. If possible I'd like to avoid using MatLab, is there any tool/software (for unix) that would help me? EDIT: This is exactly what I was looking for! On the bottom of the page it says: "TIP: To process many files, use a shell script to pass this URL and your desired parameters to wget and then direct the output to file" Yet I can't do this. I tried with: ``` wget --post-data="blocksize=10&width=10&offset=0&markval=-1&autoscale=0" \ --post-file="userfile=/path.../filename" http://www.ryanwestafer.com/stuff/bin2img.php \ > output ``` but all I get is the original page downloaded in my local folder!

Original source