def _select_top_object(): objects = ifaint.get_objects() if len(objects) > 0: ifaint.tool(0) ifaint.set_layer(1) i = ifaint.get_active_image() i.select(objects[-1])
def _toggle_zoom_fit_all(): """Toggles between zoom fit and zoom 1:1 for all images, zooming all depending on the current zoom of the active image""" active = ifaint.get_active_image() if active.get_zoom() == 1.0: ifaint.zoom_fit(ifaint.images) else: ifaint.zoom_default(ifaint.images)
def _browse_to_active_file(): """Calls ifaint.external_browse_to_file_command with the filename of the active image.""" if ifaint.external_browse_to_file_command is None: raise ValueError( "ifaint.external_browse_to_file_command not configured.") filename = ifaint.get_active_image().get_filename() if filename is not None: import subprocess subprocess.Popen(ifaint.external_browse_to_file_command(filename))
def _scroll_traverse(): """Scrolls through the image column by column, using scroll_page_down and scroll_page_right, eventually wrapping back to 0,0""" active = ifaint.get_active_image() max = active.get_max_scroll() current = active.get_scroll_pos() if current[1] >= max[1]: if current[0] >= max[0]: active.set_scroll_pos(0, 0) else: active.set_scroll_pos(current[0], 0) active.scroll_page_right() else: active.scroll_page_down()
def toggle_flag_pixel(): """Adds the image and the current mouse position to a map of image-to-pixel. Indicates this with the set_point_overlay-function. If the mouse position is already flagged for the image, the flag is removed instead. The flagging can be used for to re-anchor images with anchor_flagged. """ image = ifaint.get_active_image() pos = ifaint.get_mouse_pos() if image in _flagged and _flagged[image] == pos: del _flagged[image] image.clear_point_overlay() else: _flagged[image] = pos image.set_point_overlay(*pos)
def erase_rows(y0, y1): """Reduces the image height by removing all pixel rows from y0 to y1 (inclusive). """ y0, y1 = min(y0, y1), max(y0, y1) if y0 < 0: raise ValueError("Negative row specified.") image = ifaint.get_active_image() full = image.get_bitmap() w, h = full.get_size() h2 = h - (y1 - y0 + 1) if h2 <= 0: raise ValueError("Can't erase all rows.") bottom = full.subbitmap(0, y1 + 1, w, h - y1 - 1) image.blit((0, y0),bottom) image.set_rect(0,0, w, h2)
def _apply_images(func, images): if len(images) == 0: return func(ifaint.get_active_image()) if len(images) == 1: first_obj = images[0] if first_obj.__class__ == ifaint.Canvas: func(first_obj) return else: for img in first_obj: if img.__class__ != ifaint.Canvas: raise TypeError("Non-canvas specified") for img in first_obj: func(img) else: for img in images: if img.__class__ != ifaint.Canvas: raise TypeError("Non-canvas specified") for img in images: func(img)
def erase_columns(x0, x1): """Reduces the image width by removing all pixel columns from x0 to x1 (inclusive). """ x0, x1 = min(x0, x1), max(x0, x1) if x0 < 0: raise ValueError("Negative column specified.") image = ifaint.get_active_image() full = image.get_bitmap() w, h = full.get_size() w2 = w - (x1 - x0 + 1) if w2 <= 0: raise ValueError("Can't erase all columns.") right = full.subbitmap(x1 + 1, 0, w - x1 - 1, h) image.blit((x0,0),right) image.set_rect(0,0, w2, h)
def erase_selection(): """Removes the columns or rows indicated by the raster selection in the active image and shrinks the image. The raster selection must extend either across all rows or columns of the image. """ image = ifaint.get_active_image() x, y, w, h = image.get_selection() img_w, img_h = image.get_size() if h == img_h: erase_columns(x, x + w) image.set_selection(0,0,0,0) image.command_name = "Erase Columns" elif w == img_w: erase_rows(y, y + h) image.set_selection(0,0,0,0) image.command_name = "Erase Rows"
def to_text(temp_folder, image=None, language="eng"): """Uses tesseract-ocr to return a string of the raster text in the image. Uses the active image if image is None. Will leave some temporary files in the specified folder. """ if image is None: image = ifaint.get_active_image() temp_png = os.path.join(temp_folder, "tesseract-in.png") temp_txt = os.path.join(temp_folder, "tesseract-out") image.save_backup(temp_png) cmd = ["tesseract", temp_png, temp_txt, "-l %s" % language] tsr = sp.Popen(cmd, startupinfo=hide_console()) if tsr.wait() == 0: with open("%s.txt" % temp_txt, 'rb') as f: return f.read().decode("utf-8") else: raise ValueError("Failed running Tesseract.")
def _center_on_cursor(): image = ifaint.get_active_image() pos = image.get_mouse_pos() image.center(*pos)
def _select_bottom_object(): objects = ifaint.get_objects() if len(objects) > 0: i = ifaint.get_active_image() i.select(objects[0])
def _Rect(*args, **kwArgs): return ifaint.get_active_image().Rect(*args, **kwArgs)
def _Polygon(*args, **kwArgs): return ifaint.get_active_image().Polygon(*args, **kwArgs)
def reverse_frames(): img = ifaint.get_active_image() n = len(img.get_frames()) for i in range(n // 2): img.swap_frames(i, n - i - 1)
def _Line(*args, **kwArgs): return ifaint.get_active_image().Line(*args, **kwArgs)
def _list_frames(): return ifaint.get_active_image().get_frames()
def _active(func): l = lambda *args, **kwArgs: func(ifaint.get_active_image(), *args, **kwArgs ) l.__name__ = func.__name__ l.__doc__ = "Single-forward to Canvas." + func.__doc__ return l
def _Ellipse(*args, **kwArgs): return ifaint.get_active_image().Ellipse(*args, **kwArgs)
def _Group(*args, **kwArgs): return ifaint.get_active_image().Group(*args, **kwArgs)
def _pick_pattern_fg(): pattern = ifaint.Pattern(ifaint.get_mouse_pos(), ifaint.get_active_image().get_bitmap()) pattern.set_object_aligned(True) ifaint.set_fg(pattern)
def _Path(*args, **kwArgs): return ifaint.get_active_image().Path(*args, **kwArgs)
def _Text(*args, **kwArgs): return ifaint.get_active_image().Text(*args, **kwArgs)
def _Raster(*args, **kwArgs): return ifaint.get_active_image().Raster(*args, **kwArgs)