def jetkiller(img, colormap, ignore_gray): img.undo_group_start() gimp.progress_init("Executing Jet Killer ...") src_layer = img.active_layer w = src_layer.width h = src_layer.height dst_layer = gimp.Layer(img, src_layer.name + " - Jet Killer", w, h, gimpenums.RGBA_IMAGE) dst_layer.set_offsets( *src_layer.offsets) # at the same position as the source img.add_layer(dst_layer) # on top by default input_cmap = get_colormap("jet") output_cmap = get_colormap(colormap) def raw_convert_pixel(px): # Get nearest color from input colormap and return # corresponding color in output colormap dr = input_cmap[:, 0] - ord(px[0]) dg = input_cmap[:, 1] - ord(px[1]) db = input_cmap[:, 2] - ord(px[2]) dist = dr * dr + dg * dg + db * db idx = np.argmin(dist) return "".join([chr(j) for j in output_cmap[idx]]) cache = {} def convert_pixel(px): if px not in cache: cache[px] = raw_convert_pixel(px) return cache[px] gimp.tile_cache_ntiles(_tile_cache_size) src_rgn = src_layer.get_pixel_rgn(0, 0, src_layer.width, src_layer.height, False, False) dst_rgn = dst_layer.get_pixel_rgn(0, 0, dst_layer.width, dst_layer.height, True, True) for x in xrange(src_layer.width): for y in xrange(src_layer.height): pixel = src_rgn[x, y] if not ignore_gray or (pixel[0] != pixel[1] or pixel[1] != pixel[2]): new_rgb_pixel = convert_pixel(pixel[0:3]) if len(pixel) == 4: # has an alpha channel new_rgba_pixel = new_rgb_pixel + pixel[3] else: new_rgba_pixel = new_rgb_pixel + chr(255) dst_rgn[x, y] = new_rgba_pixel gimp.progress_update(float(x) / src_layer.width) # update progress bar dst_layer.flush() dst_layer.merge_shadow() dst_layer.update(0, 0, dst_layer.width, dst_layer.height) gimp.pdb.gimp_progress_end() # important for non-interactive mode gimp.displays_flush() img.undo_group_end()
def colorxhtml(img, drawable, filename, raw_filename, source_type, characters, size, separate): width = drawable.width height = drawable.height bpp = drawable.bpp if not drawable.is_rgb or drawable.has_alpha: return if source_type not in all_source_types: return gimp.tile_cache_ntiles(width / gimp.tile_width() + 1) html = file(filename, 'w') if separate: dirname, cssfile = os.path.split(filename) cssfile = os.path.splitext(cssfile)[0] + '.css' cssname = os.path.join(dirname, cssfile) css = file(cssname, 'w') if source_type == CHARS_SOURCE: chars = file(inspect.getsourcefile(colorxhtml)).read() elif source_type == CHARS_FILE: chars = file(characters).read() elif source_type == CHARS_PARAMETER: chars = characters allchars = string.maketrans('', '') goodchars = string.digits + string.ascii_letters + string.punctuation badchars = ''.join(c for c in allchars if c not in goodchars) chars = chars.translate(allchars, badchars) data = [escape_table.get(c, c) for c in chars] if data: data.reverse() else: data = list('X' * 80) pr = drawable.get_pixel_rgn(0, 0, width, height, False, False) gimp.progress_init(_("Saving as colored XHTML")) style = style_def % size if separate: ss = '<link rel="stylesheet" type="text/css" href="%s" />' % cssfile css.write(style) else: ss = '<style type="text/css">\n%s</style>' % style html.write(preamble % ss) colors = {} chars = [] for y in range(0, height): row = pr[0:width, y] while len(chars) < width: chars[0:0] = data for pixel in RowIterator(row, bpp): color = '%02x%02x%02x' % pixel style = 'background-color:black; color:#%s;' % color char = chars.pop() if separate: if color not in colors: css.write('span.N%s { %s }\n' % (color, style)) colors[color] = 1 html.write('<span class="N%s">%s</span>' % (color, char)) else: html.write('<span style="%s">%s</span>' % (style, char)) html.write('\n') gimp.progress_update(y / float(height)) html.write(postamble) html.close() if separate: css.close()
def colorxhtml(img, drawable, filename, raw_filename, source_type, characters, size, separate): width = drawable.width height = drawable.height bpp = drawable.bpp if not drawable.is_rgb or drawable.has_alpha: return if source_type not in all_source_types: return gimp.tile_cache_ntiles(width / gimp.tile_width() + 1) html = file(filename, 'w') if separate: dirname, cssfile = os.path.split(filename) cssfile = os.path.splitext(cssfile)[0] + '.css' cssname = os.path.join(dirname, cssfile) css = file(cssname, 'w') if source_type == CHARS_SOURCE: chars = file(inspect.getsourcefile(colorxhtml)).read() elif source_type == CHARS_FILE: chars = file(characters).read() elif source_type == CHARS_PARAMETER: chars = characters allchars = string.maketrans('', '') goodchars = string.digits + string.ascii_letters + string.punctuation badchars = ''.join([c for c in allchars if c not in goodchars]) chars = chars.translate(allchars, badchars) data = [] for c in chars: data.append(escape_table.get(c, c)) if data: data.reverse() else: data = list('X' * 80) pr = drawable.get_pixel_rgn(0, 0, width, height, False, False) gimp.progress_init(_("Saving as colored XHTML")) style = style_def % size if separate: ss = '<link rel="stylesheet" type="text/css" href="%s" />' % cssfile css.write(style) else: ss = '<style type="text/css">\n%s</style>' % style html.write(preamble % ss) colors = {} chars = [] for y in range(0, height): row = pr[0:width, y] while len(chars) < width: chars[0:0] = data for pixel in RowIterator(row, bpp): color = '%02x%02x%02x' % pixel style = 'background-color:black; color:#%s;' % color char = chars.pop() if separate: if color not in colors: css.write('span.N%s { %s }\n' % (color, style)) colors[color] = 1 html.write('<span class="N%s">%s</span>' % (color, char)) else: html.write('<span style="%s">%s</span>' % (style, char)) html.write('\n') gimp.progress_update(y / float(height)) html.write(postamble) html.close() if separate: css.close()