[iaread] [Up] [iaapplylut] Image file I/O

iawrite
Write an image file (PBM, PGM and PPM).

Synopsis

iawrite( arrayname, filename, mode = 'bin' )

Implemented in Python.

Input

arrayname Image. Gray-scale (uint8 or uint16) or binary image (logical).
filename String.
mode String.

Default: 'bin'

Examples

>>> f = Numeric.resize(range(256), (256, 256))

              
>>> f_color = Numeric.zeros((256, 256, 3))

              
>>> f_color[:,:,0] = f; f_color[:,:,1] = 127; f_color[:,:,2] = 255-f

              
>>> import os

              
>>> file_name = os.tempnam() # Name for a temporary file
'module' object has no attribute 'argv'
>>> print file_name
name 'file_name' is not defined
>>> print 'Saving f in '+file_name+'.pgm ...'
name 'file_name' is not defined
>>> iawrite(f, os.tempnam()+'.pgm')
'module' object has no attribute 'argv'
>>> print 'Saving f_color in '+file_name+'.ppm ...'
name 'file_name' is not defined
>>> iawrite(f_color, os.tempnam()+'.ppm')
'module' object has no attribute 'argv'

Source Code

def iawrite(arrayname, filename, mode='bin'):
    import Numeric
    import string
    import array
    import os
    def type(filename):
        import string
        is_pbm, is_pgm, is_ppm = 0, 0, 0
        if string.find(string.upper(filename), '.PBM') != -1:
            is_pbm = 1
        elif string.find(string.upper(filename), '.PGM') != -1:
            is_pgm = 1
        elif string.find(string.upper(filename), '.PPM') != -1:
            is_ppm = 1
        return is_pbm, is_pgm, is_ppm
    def bin2asc(bin_num_list):
        a = ''
        x = [128, 64, 32, 16, 8, 4, 2, 1]
        for i in range(0, len(bin_num_list)-7, 8):
            aux1 = bin_num_list[i:i+8]
            aux2 = x[0:len(aux1)]
            a = a + chr(innerproduct(aux1, aux2))
        return a
    arrayname = Numeric.asarray(arrayname)
    if len(arrayname.shape) == 1:
        arrayname = Numeric.transpose(arrayname[NewAxis,:])
        h, w = arrayname.shape
    elif len(arrayname.shape) == 3:
        h, w = arrayname.shape[2], arrayname.shape[1]
        aux = Numeric.zeros((w, h, 3))
        aux[:,:,0], aux[:,:,1], aux[:,:,2] = arrayname[0,:,:], arrayname[1,:,:], arrayname[2,:,:]
        arrayname = Numeric.transpose(aux, (1,0,2))
    else:
        arrayname = Numeric.transpose(arrayname)
        h, w = arrayname.shape
    is_pbm, is_pgm, is_ppm = type(filename)
    h, w = arrayname.shape[0], arrayname.shape[1]
    if is_pbm or is_pgm or is_ppm:
        try:
            un = len(os.environ['USER'])
            if filename[0:1+un] == '~' + os.environ['USER']:
                 filename = os.environ['HOME'] + filename[1+un:]
            elif filename[0:2] == '~/':
                 filename = os.environ['HOME'] + filename[1:]
        except:
            pass
        f = open(filename, 'w')
        if string.find(string.upper(mode), 'ASC') != -1:
            if is_pbm or is_pgm:
                if is_pbm:
                    f.write('P1\n')
                    arrayname = Numeric.greater(arrayname, Numeric.zeros(arrayname.shape))
                    arrayname = 1 - arrayname
                else:
                    f.write('P2\n')
                f.write('# By ia636 toolbox\n')
                f.write(str(h)+' '+str(w)+'\n')
                if is_pgm:
                    f.write('255\n')
                aux = Numeric.ravel(Numeric.transpose(arrayname))
                img = str(aux)[1:-1]
                f.write(img)
            else:
                f.write('P3\n')
                f.write('# By ia636 toolbox\n')
                f.write(str(h)+' '+str(w)+'\n')
                f.write('255\n')
                aux = Numeric.ravel(Numeric.transpose(arrayname, (1,0,2)))
                img = str(aux)[1:-1]
                f.write(img)
        elif string.find(string.upper(mode), 'BIN') != -1:
            if is_pbm:
                f.write('P4\n')
                f.write('# By ia636 toolbox\n')
                f.write(str(h)+' '+str(w)+'\n')
                arrayname = Numeric.greater(arrayname, Numeric.zeros(arrayname.shape))
                arrayname = 1 - arrayname
                aux = Numeric.ravel(Numeric.transpose(arrayname))
                img = bin2asc(aux.tolist())
                f.write(img)
            elif is_pgm:
                f.write('P5\n')
                f.write('# By ia636 toolbox\n')
                f.write(str(h)+' '+str(w)+'\n')
                f.write('255\n')
                aux = Numeric.ravel(Numeric.transpose(arrayname))
                img = map(chr, aux.tolist())
                img = string.join(img, '')
                f.write(img)
            else:
                f.write('P6\n')
                f.write('# By ia636 toolbox\n')
                f.write(str(h)+' '+str(w)+'\n')
                f.write('255\n')
                aux = Numeric.ravel(Numeric.transpose(arrayname, (1,0,2)))
                img = map(chr, aux.tolist())
                img = string.join(img, '')
                f.write(img)
        f.close()
    else:
        iaerror('error: file format was not specified!')
    
[iaread] [Up] [iaapplylut] http://www.python.org