Example #1
0
def pil_to_array(pilImage):
    if pilImage.mode in ('RGBA', 'RGBX'):
        im = pilImage  # no need to convert images in rgba format
    else:  # try to convert to an rgba image
        try:
            im = pilImage.convert('RGBA')
        except ValueError:
            raise RuntimeError('Unknown image mode')

    x_str = im.tostring('raw', im.mode, 0, -1)
    x = numerix.fromstring(x_str, numerix.UInt8)
    x.shape = im.size[1], im.size[0], 4
    return x
Example #2
0
def pil_to_array( pilImage ):
    if pilImage.mode in ('RGBA', 'RGBX'):
        im = pilImage # no need to convert images in rgba format
    else: # try to convert to an rgba image
        try:
            im = pilImage.convert('RGBA')
        except ValueError:
            raise RuntimeError('Unknown image mode')

    x_str = im.tostring('raw',im.mode,0,-1)
    x = numerix.fromstring(x_str,numerix.UInt8)
    x.shape = im.size[1], im.size[0], 4
    return x
Example #3
0
def pil_to_array(pilImage):
    if pilImage.mode == 'P':  # convert from paletted
        im = pilImage.convert('RGBX')
    else:
        im = pilImage

    # There's a whole lotta conversion and copying going on
    # here -- could it be optimized?

    if im.mode in ('RGBA', 'RGBX'): n_channels = 4
    elif im.mode == 'RGB': n_channels = 3
    elif im.mode == 'L': n_channels = 1
    else: raise RuntimeError('Unknown image mode')

    x_str = im.tostring('raw', im.mode, 0, -1)
    x = numerix.fromstring(x_str, numerix.UInt8)
    if n_channels == 1:
        x.shape = im.size[1], im.size[0]
    else:
        x.shape = im.size[1], im.size[0], n_channels
    x = x.astype(numerix.Float32) / 255.0
    return x
Example #4
0
def pil_to_array( pilImage ):
    if pilImage.mode == 'P': # convert from paletted
        im = pilImage.convert('RGBX')
    else:
        im = pilImage

    # There's a whole lotta conversion and copying going on
    # here -- could it be optimized?

    if im.mode in ('RGBA','RGBX'): n_channels = 4
    elif im.mode == 'RGB': n_channels = 3
    elif im.mode == 'L': n_channels = 1
    else: raise RuntimeError('Unknown image mode')

    x_str = im.tostring('raw',im.mode,0,-1)
    x = numerix.fromstring(x_str,numerix.UInt8)
    if n_channels == 1:
        x.shape = im.size[1], im.size[0]
    else:
        x.shape = im.size[1], im.size[0], n_channels
    x=x.astype(numerix.Float32)/255.0
    return x