def image_from_ndarray(array, format, size = None): """ Creates an Image from a numpy ndarray object. The format may be 'RGB' or 'RGBA'. If a size is specified, the array will be implicitly reshaped to that size, otherwise the size is inferred from the first two dimensions of the array. """ if array.itemsize <> 1: raise ValueError("Color component size must be 1 byte") if size is not None: width, height = size data_size = array.size pixel_size = data_size // (width * height) if pixel_size <> len(format): raise ValueError("Array has wrong shape for specified size and format") else: height, width, pixel_size = array.shape if pixel_size <> len(format): raise ValueError("Array has wrong shape for specified format") bps = 8 spp = pixel_size alpha = format.endswith("A") csp = NSCalibratedRGBColorSpace bpp = bps * spp bpr = width * pixel_size fmt = NSAlphaNonpremultipliedBitmapFormat ns_rep = NSBitmapImageRep.alloc() planes = planes_t(array.ctypes.data, 0, 0, 0, 0) ns_rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_( ctypes.addressof(planes), width, height, bps, spp, alpha, False, csp, fmt, bpr, bpp) image = Image.__new__(Image) image._init_from_ns_rep(ns_rep) image._data = array return image
def image_from_pil_image(pil_image): """Creates an Image from a Python Imaging Library (PIL) Image object.""" mode = pil_image.mode w, h = pil_image.size data = pil_image.tostring() alpha = False cmyk = False floating = False if mode == "1": bps = 1; spp = 1 elif mode == "L": bps = 8; spp = 1 elif mode == "RGB": bps = 8; spp = 3 elif mode == "RGBA": bps = 8; spp = 4; alpha = True elif mode == "CMYK": bps = 8; spp = 4; cmyk = True elif mode == "I": bps = 32; spp = 1 elif mode == "F": bps = 32; spp = 1; floating = True else: raise ValueError("Unsupported PIL image mode '%s'" % mode) if cmyk: csp = NSDeviceCMYKColorSpace else: csp = NSCalibratedRGBColorSpace fmt = NSAlphaNonpremultipliedBitmapFormat if floating: fmt |= NSFloatingPointSamplesBitmapFormat bpp = bps * spp bpr = w * ((bpp + 7) // 8) if debug_pil: print "GUI.PIL:" print "image size =", (w, h) print "data size =", len(data) print "bits per sample =", bps print "samples per pixel =", spp print "bits per pixel =", bpp print "bytes per row =", bpr hack_objc_sig() ns_rep = NSBitmapImageRep.alloc() planes = planes_t(data, "", "", "", "") ns_rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_( ctypes.addressof(planes), w, h, bps, spp, alpha, False, csp, fmt, bpr, bpp) # planes = (data, "", "", "", "") # ns_rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_( # planes, w, h, bps, spp, alpha, False, csp, bpr, bpp) image = Image.__new__(Image) image._init_from_ns_rep(ns_rep) image._data = data return image
def image_from_pil_image(pil_image): """Creates an Image from a Python Imaging Library (PIL) Image object.""" mode = pil_image.mode w, h = pil_image.size data = pil_image.tostring() if mode == "RGB": bps = 3; alpha = False elif mode == "RGBA": bps = 4; alpha = True else: raise ValueError("Unsupported PIL image mode '%s'" % mode) bpr = w * bps image = Image.__new__(Image) image._gdk_pixbuf = gdk.pixbuf_new_from_data(data, COLORSPACE_RGB, alpha, 8, w, h, bpr) return image
def image_from_pil_image(pil_image): """Creates an Image from a Python Imaging Library (PIL) Image object.""" mode = pil_image.mode w, h = pil_image.size data = pil_image.tostring() if mode == "RGB": bps = 3 alpha = False elif mode == "RGBA": bps = 4 alpha = True else: raise ValueError("Unsupported PIL image mode '%s'" % mode) bpr = w * bps image = Image.__new__(Image) image._gdk_pixbuf = gdk.pixbuf_new_from_data(data, COLORSPACE_RGB, alpha, 8, w, h, bpr) return image
def image_from_ndarray(array, format, size=None): """ Creates an Image from a numpy ndarray object. The format may be 'RGB' or 'RGBA'. If a size is specified, the array will be implicitly reshaped to that size, otherwise the size is inferred from the first two dimensions of the array. """ if array.itemsize != 1: raise ValueError("Color component size must be 1 byte") if size is None: shape = array.shape if len(shape) != 3: raise ValueError("Array has wrong number of dimensions") height, width, pixel_size = shape if pixel_size != len(format): raise ValueError("Last dimension of array does not match format") else: width, height = size pixel_size = len(format) data_size = array.size if data_size != width * height * pixel_size: raise ValueError( "Array has wrong shape for specified size and format") shape = (height, width, pixel_size) array = array.reshape(shape) swapped = ndarray(shape, uint8) swapped[..., 0] = array[..., 2] swapped[..., 1] = array[..., 1] swapped[..., 2] = array[..., 0] if pixel_size == 4: fmt = gdi.PixelFormat32bppARGB swapped[..., 3] = array[..., 3] else: fmt = gdi.PixelFormat24bppRGB data = swapped.tostring() bitmap = gdi.Bitmap.from_data(width, height, fmt, data) image = Image.__new__(Image) image._win_image = bitmap image._data = data return image
def image_from_pil_image(pil_image): """Creates an Image from a Python Imaging Library (PIL) Image object.""" pil_image.load() mode = pil_image.mode w, h = pil_image.size if mode == "RGB": r, g, b = pil_image.split() pil_image = merge(mode, (b, g, r)) fmt = gdi.PixelFormat24bppRGB elif mode == "RGBA": r, g, b, a = pil_image.split() pil_image = merge(mode, (b, g, r, a)) fmt = gdi.PixelFormat32bppARGB else: raise ValueError("Unsupported PIL image mode '%s'" % mode) data = pil_image.tostring() bitmap = gdi.Bitmap.from_data(w, h, fmt, data) image = Image.__new__(Image) image._win_image = bitmap image._data = data return image
def image_from_ndarray(array, format, size = None): """ Creates an Image from a numpy ndarray object. The format may be 'RGB' or 'RGBA'. If a size is specified, the array will be implicitly reshaped to that size, otherwise the size is inferred from the first two dimensions of the array. """ if array.itemsize <> 1: raise ValueError("Color component size must be 1 byte") if size is None: shape = array.shape if len(shape) <> 3: raise ValueError("Array has wrong number of dimensions") height, width, pixel_size = shape if pixel_size <> len(format): raise ValueError("Last dimension of array does not match format") else: width, height = size pixel_size = len(format) data_size = array.size if data_size <> width * height * pixel_size: raise ValueError("Array has wrong shape for specified size and format") shape = (height, width, pixel_size) array = array.reshape(shape) swapped = ndarray(shape, uint8) swapped[..., 0] = array[..., 2] swapped[..., 1] = array[..., 1] swapped[..., 2] = array[..., 0] if pixel_size == 4: fmt = gdi.PixelFormat32bppARGB swapped[..., 3] = array[..., 3] else: fmt = gdi.PixelFormat24bppRGB data = swapped.tostring() bitmap = gdi.Bitmap.from_data(width, height, fmt, data) image = Image.__new__(Image) image._win_image = bitmap image._data = data return image
def image_from_ndarray(array, format, size=None): """ Creates an Image from a numpy ndarray object. The format may be 'RGB' or 'RGBA'. If a size is specified, the array will be implicitly reshaped to that size, otherwise the size is inferred from the first two dimensions of the array. """ if array.itemsize <> 1: raise ValueError("Color component size must be 1 byte") if size is not None: width, height = size data_size = array.size pixel_size = data_size // (width * height) if pixel_size <> len(format): raise ValueError( "Array has wrong shape for specified size and format") else: height, width, pixel_size = array.shape if pixel_size <> len(format): raise ValueError("Array has wrong shape for specified format") bps = 8 spp = pixel_size alpha = format.endswith("A") csp = NSCalibratedRGBColorSpace bpp = bps * spp bpr = width * pixel_size fmt = NSAlphaNonpremultipliedBitmapFormat ns_rep = NSBitmapImageRep.alloc() planes = planes_t(array.ctypes.data, 0, 0, 0, 0) ns_rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_( ctypes.addressof(planes), width, height, bps, spp, alpha, False, csp, fmt, bpr, bpp) image = Image.__new__(Image) image._init_from_ns_rep(ns_rep) image._data = array return image
def image_from_pil_image(pil_image): """Creates an Image from a Python Imaging Library (PIL) Image object.""" mode = pil_image.mode w, h = pil_image.size data = pil_image.tostring() alpha = False cmyk = False floating = False if mode == "1": bps = 1 spp = 1 elif mode == "L": bps = 8 spp = 1 elif mode == "RGB": bps = 8 spp = 3 elif mode == "RGBA": bps = 8 spp = 4 alpha = True elif mode == "CMYK": bps = 8 spp = 4 cmyk = True elif mode == "I": bps = 32 spp = 1 elif mode == "F": bps = 32 spp = 1 floating = True else: raise ValueError("Unsupported PIL image mode '%s'" % mode) if cmyk: csp = NSDeviceCMYKColorSpace else: csp = NSCalibratedRGBColorSpace fmt = NSAlphaNonpremultipliedBitmapFormat if floating: fmt |= NSFloatingPointSamplesBitmapFormat bpp = bps * spp bpr = w * ((bpp + 7) // 8) if debug_pil: print "GUI.PIL:" print "image size =", (w, h) print "data size =", len(data) print "bits per sample =", bps print "samples per pixel =", spp print "bits per pixel =", bpp print "bytes per row =", bpr hack_objc_sig() ns_rep = NSBitmapImageRep.alloc() planes = planes_t(data, "", "", "", "") ns_rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_( ctypes.addressof(planes), w, h, bps, spp, alpha, False, csp, fmt, bpr, bpp) # planes = (data, "", "", "", "") # ns_rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_( # planes, w, h, bps, spp, alpha, False, csp, bpr, bpp) image = Image.__new__(Image) image._init_from_ns_rep(ns_rep) image._data = data return image