Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert binary file to image

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!

like image 502
user1584773 Avatar asked Dec 30 '12 16:12

user1584773


People also ask

Can you convert binary to an image?

Just load your binary number and it will automatically get converted to an image. There are no ads, popups or nonsense, just a binary to image converter. Load a binary value, get an image. Created for developers by developers from team Browserling.

Is JPG a binary file?

Binary files can be used to store any data; for example, a JPEG image is a binary file designed to be read by a computer system. The data inside a binary file is stored as raw bytes, which is not human readable.


1 Answers

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

like image 153
greggo Avatar answered Oct 10 '22 16:10

greggo