我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用Image.merge()。
def do_merge(self): """usage: merge <string:mode> <image:pic1> [<image:pic2> [<image:pic3> [<image:pic4>]]] Merge top-of stack images in a way described by the mode. """ mode = self.do_pop() bandlist = [] for band in mode: bandlist.append(self.do_pop()) self.push(Image.merge(mode, bandlist)) # Image class methods
def image_to_string(image, lang=None, boxes=False, config=None): ''' Runs tesseract on the specified image. First, the image is written to disk, and then the tesseract command is run on the image. Resseract's result is read, and the temporary files are erased. also supports boxes and config. if boxes=True "batch.nochop makebox" gets added to the tesseract call if config is set, the config gets appended to the command. ex: config="-psm 6" ''' if len(image.split()) == 4: # In case we have 4 channels, lets discard the Alpha. # Kind of a hack, should fix in the future some time. r, g, b, a = image.split() image = Image.merge("RGB", (r, g, b)) input_file_name = '%s.bmp' % tempnam() output_file_name_base = tempnam() if not boxes: output_file_name = '%s.txt' % output_file_name_base else: output_file_name = '%s.box' % output_file_name_base try: image.save(input_file_name) status, error_string = run_tesseract(input_file_name, output_file_name_base, lang=lang, boxes=boxes, config=config) if status: errors = get_errors(error_string) raise TesseractError(status, errors) f = open(output_file_name) try: return f.read().strip() finally: f.close() finally: cleanup(input_file_name) cleanup(output_file_name)
def main(): if len(sys.argv) == 2: filename = sys.argv[1] try: image = Image.open(filename) if len(image.split()) == 4: # In case we have 4 channels, lets discard the Alpha. # Kind of a hack, should fix in the future some time. r, g, b, a = image.split() image = Image.merge("RGB", (r, g, b)) except IOError: sys.stderr.write('ERROR: Could not open file "%s"\n' % filename) exit(1) print(image_to_string(image)) elif len(sys.argv) == 4 and sys.argv[1] == '-l': lang = sys.argv[2] filename = sys.argv[3] try: image = Image.open(filename) except IOError: sys.stderr.write('ERROR: Could not open file "%s"\n' % filename) exit(1) print(image_to_string(image, lang=lang)) else: sys.stderr.write('Usage: python tesseract.py [-l language] input_file\n') exit(2)
def __init__(self, im): data = None colortable = None # handle filename, if given instead of image name if hasattr(im, "toUtf8"): # FIXME - is this really the best way to do this? im = unicode(im.toUtf8(), "utf-8") if Image.isStringType(im): im = Image.open(im) if im.mode == "1": format = QImage.Format_Mono elif im.mode == "L": format = QImage.Format_Indexed8 colortable = [] for i in range(256): colortable.append(rgb(i, i, i)) elif im.mode == "P": format = QImage.Format_Indexed8 colortable = [] palette = im.getpalette() for i in range(0, len(palette), 3): colortable.append(rgb(*palette[i:i+3])) elif im.mode == "RGB": data = im.tostring("raw", "BGRX") format = QImage.Format_RGB32 elif im.mode == "RGBA": try: data = im.tostring("raw", "BGRA") except SystemError: # workaround for earlier versions r, g, b, a = im.split() im = Image.merge("RGBA", (b, g, r, a)) format = QImage.Format_ARGB32 else: raise ValueError("unsupported image mode %r" % im.mode) # must keep a reference, or Qt will crash! self.__data = data or im.tostring() QImage.__init__(self, self.__data, im.size[0], im.size[1], format) if colortable: self.setColorTable(colortable)