def gridstack_test(self): """ dstack_test: Draw an image, stack it, and make sure it's the same as the numpy operation. """ C1 = ph.Canvas() C2 = ph.Canvas() C3 = ph.Canvas() C1 += ph.circle() C2 += ph.rectangle() C3 += ph.circle(x=1) # Make sure something was drawn assert_true(C1.img.sum() != 0) assert_true(C2.img.sum() != 0) assert_true(C3.img.sum() != 0) X = ph.gridstack([[C1, C2], [C2, C3]]) img_stack = np.vstack( [np.hstack([C1.img, C2.img]), np.hstack([C2.img, C3.img])]) assert_true(np.isclose(img_stack, X.img).all())
def text_hpos_test(self): C0 = ph.Canvas() C1 = ph.Canvas() C2 = ph.Canvas() C0 += ph.text(hpos="center") C1 += ph.text(hpos="left") C2 += ph.text(hpos="right") # Now check that none of them are equal for x, y in itertools.combinations([C0, C1, C2], r=2): assert_false((x.img == y.img).all())
def __init__(self, world_adapter: WorldAdapter): self.world_adapter = world_adapter self.world_adapter.add_renderer(self) self.terrain_map = [[ ph.Canvas().load(GameSpace.WaterTerrain().sprite_path_string) for x in range(self.world_adapter.width) ] for y in range(self.world_adapter.height)] self.rendered_canvas = ph.gridstack(self.terrain_map) self.rendered_canvas.name = WINDOW_NAME self.base_cell_width = self.terrain_map[0][0].width self.base_cell_height = self.terrain_map[0][0].height self._sprite_cache = keydefaultdict(lambda k: ph.Canvas().load(k))
def direct_load_test(self): """ direct_load_test: Load an image without creating a canvas. """ C = ph.Canvas(bg="yellow") C += ph.circle(color="g") C2 = ph.Canvas() with tempfile.NamedTemporaryFile(suffix=".png") as F: C.save(F.name) C2 = ph.load(F.name) dist = C.img[:, :, :3] - C2.img[:, :, :3] dist = dist.astype(float) / 255 assert_equal(dist.sum(), 0)
def empty_artist_test(self): canvas = ph.Canvas() class EmptyArtist(ph.Artist): pass canvas += EmptyArtist()
def check_draw_if_offscreen_is_ok_test(self): g = ph.gradient.linear(["r", "b"]) C = ph.Canvas() C += ph.circle(x=C.xmax + 20, y=C.ymax + 20, r=1, gradient=g) # Check that nothing was drawn assert (C.img[:, :, :3] == C.transform_color(C.bg)[:3]).all()
def combine_canvas_test(self): """ combine_canvas_test: Try to add two canvas together with blend, add, subtract modes. """ C1 = ph.Canvas() C1 += ph.circle(color="w") C2 = ph.Canvas() C2 += ph.circle(x=-0.5, color="b") C3 = C1.copy().combine(C2, mode="blend") C4 = C1.copy().combine(C2, mode="add") C5 = C1.copy().combine(C2, mode="subtract") # Now check that none of them are equal for x, y in itertools.combinations([C1, C3, C4, C5], r=2): assert_false((x.img == y.img).all())
def set_alpha_value_test(self): """ set_alpha_value_test Set the alpha channel to a specific value """ C = ph.Canvas() C.alpha = 120 assert_equal(C.alpha.mean(), 120)
def text_gradient_test(self): pal = ph.palette(15) C0 = ph.Canvas() C0 += ph.text(color=pal[0]) C1 = ph.Canvas() C1 += ph.text(color=pal[1]) g = ph.gradient.linear([pal[0], pal[1]]) C2 = ph.Canvas() C2 += ph.text(gradient=g) # Now check that none of them are equal for x, y in itertools.combinations([C0, C1, C2], r=2): assert_false((x.img == y.img).all())
def save_load_test(self): """ save_load_test: Create an image, save it to a tmp location and try to load it back in. Make sure we check in PNG (lossless) and don't try to check the alpha channel. """ C = ph.Canvas(bg="yellow") C += ph.circle(color="g") C2 = ph.Canvas() with tempfile.NamedTemporaryFile(suffix=".png") as F: C.save(F.name) C2.load(F.name) dist = C.img[:, :, :3] - C2.img[:, :, :3] dist = dist.astype(float) / 255 assert_equal(dist.sum(), 0)
def setup(self): kwargs = { "width": 200, "height": 200, "extent": 4.0, "bg": "black", "shift": 8, } self.canvas = ph.Canvas(**kwargs)
def set_rgb_value_test(self): """ set_rgb_value_test Set the rgb channel to a specific value """ C = ph.Canvas() C.rgb = 120 assert_equal(C.alpha.mean(), 0) assert_equal(C.rgb.mean(), 120)
def grid_points_test(self): """ grid_points_test: Test if we can get the grid coordinates, do twice to test cache. """ C = ph.Canvas() pts = C.grid_points() pts = C.grid_points() assert_true(len(pts), 2)
def grid_coordinates_test(self): """ grid_coordinates_test: Test if we can get the grid coordinates, do twice to test cache. """ C = ph.Canvas() coords = C.grid_coordinates() coords = C.grid_coordinates() assert_true(len(coords), 3)
def create_from_rgba_channel_img_test(self): """ create_from_rgba_channel_img_test: Create a canvas from a rgba channel image """ img = 37 * np.ones(shape=(100, 200, 4)).astype(np.uint8) C = ph.Canvas(img=img) assert_equal(C.rgb.mean(), 37) assert_equal(C.alpha.mean(), 37)
def zero_weight_test(self): C0 = ph.Canvas() C0 += ph.circle(color=[20, 30, 40, 50]) C1 = C0.copy() C1 += ph.filters.instafilter("1977", weight=0) # Check that they are equal assert_true(np.isclose(C0.img, C1.img).all())
def resize_exact_test(self): """ resize_exact_test: After scaling, it should have these exact dimensions """ C = ph.Canvas(width=200, height=200) C += ph.rectangle(x=0.5, y=0.25, x1=0.75, y1=0.75) C.resize(output_size=(100, 50)) assert_true(C.width == 100) assert_true(C.height == 50)
def print_statement_test(self): """ print_statement_test: Make sure we can print the object """ width, height, extent = 105, 207, 3 C = ph.Canvas(width, height, extent=extent) s1 = C.__repr__() s2 = f"Canvas (w/h) {height}x{width}, extent {extent}" assert_equal(s1, s2)
def draw_with_gradient_test(self): """ draw_with_gradient_test: Draw discrete gradient and check both colors are present in the corners. """ C = ph.Canvas() g = ph.gradient.linear(["r", "b"], interpolation="discrete") C += ph.rectangle(C.xmin, C.ymin, C.xmax, C.ymax, gradient=g) assert_true((C.transform_color("r") == C.img[0, 0]).all()) assert_true((C.transform_color("b") == C.img[-1, -1]).all())
def set_img_value_from_vector_test(self): """ set_rgb_value_from_vector_test Set the rgb channel to a specific value from a vector """ C = ph.Canvas() C.img = [1, 2, 3, 4] assert_equal(C[:, :, 0].mean(), 1) assert_equal(C[:, :, 1].mean(), 2) assert_equal(C[:, :, 2].mean(), 3) assert_equal(C[:, :, 3].mean(), 4)
def get_set_item_test(self): """ get_set_item_test: Test pixel access like a numpy array. """ C = ph.Canvas(bg="k") assert_true(C[20:30, 50:60].sum() == 0) # Set a 10x10 block red C[20:30, 50:60] = [255, 0, 0, 0] assert_equal(C[20:30, 50:60].sum(), 255 * 100)
def setup(self): kwargs = { "width": 200, "height": 200, "extent": 4.0, "bg": "black", "shift": 8, } self.source = ph.Canvas(**kwargs) self.source += ph.circle(x=1, y=-0.5, color=[50, 75, 100, 100]) self.source += ph.rectangle(x=-1, y=0.5, color=[70, 75, 100, 100]) self.target = self.source.copy()
def resize_up_test(self): """ resize_up_test: After scaling by 2x, it should be 400 pixels. """ C = ph.Canvas(width=200, height=200) C += ph.rectangle(x=0.5, y=0.25, x1=0.75, y1=0.75) assert_true(C.width == 200) assert_true(C.height == 200) C.resize(fx=2) assert_true(C.width == 400) assert_true(C.height == 400)
def set_img_value_from_string_test(self): """ set_rgb_value_from_string_test Set the rgb channel to a specific value from a string """ C = ph.Canvas() C.img = "r" assert_equal(C[:, :, 0].mean(), 255) assert_equal(C[:, :, 1].mean(), 0) assert_equal(C[:, :, 2].mean(), 0) assert_equal(C[:, :, 3].mean(), 255) C.img = 32 assert_equal(C.img.mean(), 32)
def blank_doesnt_modify_in_place_test(self): """ blank_doesnt_modify_in_place_test: If the canvas has not had anything painted on it, its sum is 0 If the canvas has been blanked out, its sum is 0 Otherwise, its sum should be > 0 """ canvas = ph.Canvas() assert_true(canvas.img.sum() == 0) canvas += ph.circle() assert_true(canvas.img.sum() > 0) canvas.blank() assert_true(canvas.img.sum() > 0) assert_true(canvas.blank().img.sum() == 0)
def layer_test(self): """ layer_test: Test if layers work by testing order of operations """ C1 = ph.Canvas() C2 = ph.Canvas() C1 += ph.circle(color="r") C1 += ph.circle(y=1, color="b") C1 += ph.transform.translate(x=1) C2 += ph.circle(color="r") with C2.layer() as CX: CX += ph.circle(y=1, color="b") CX += ph.transform.translate(x=1) img1, img2 = C1.img, C2.img # First make sure something was drawn in each case assert_true(img1.sum() > 0) assert_true(img2.sum() > 0) # Now check that they aren't equal assert_false((img1 == img2).all())
def regions_of_high_intensity(canvas, blocksize=3, kernel_size=5): # If color, remove the alpha channel and covert (assume BGR!) img = cv2.cvtColor(canvas.rgb, cv2.COLOR_RGB2GRAY) # Compute the an adaptive Threshold lap = cv2.adaptiveThreshold( img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blocksize, 4) kernel = np.ones([kernel_size] * 2) sig = convolve2d(255 - lap, kernel, mode="full", boundary="symm") sig /= kernel.sum() sig = sig.astype(lap.dtype) return ph.Canvas(img=sig)
def cutbox(canvas, pts, pixel_buffer=0): """ Given a set of points, cuts out of the canvas a bounding box with pixel_buffer number of pixels outside of the bbox. Returns both the bbox image and a mask of the points within the bbox. """ pts = np.array(pts) y0 = pts[:, 0].min() - pixel_buffer y1 = pts[:, 0].max() + pixel_buffer x0 = pts[:, 1].min() - pixel_buffer x1 = pts[:, 1].max() + pixel_buffer mask = np.zeros(canvas.shape[:2], canvas.img.dtype) hull = cv2.convexHull(pts) cv2.fillConvexPoly(mask, hull, color=255) img = canvas.img[x0:x1, y0:y1] submask = mask[x0:x1, y0:y1] return ph.Canvas(img=img), submask, mask
def missing_filter_test(self): canvas = ph.Canvas() canvas += ph.filters.instafilter("not_a_filter")
# A working file to test various aspects of the module import numpy as np import pixelhouse as ph pal = ph.palette(4) C = ph.Canvas(width=400, height=400, bg=pal[0]) C = ph.Animation(width=400, height=400, bg=pal[0]) C += ph.circle(x=1, y=-0.5, r=2, color=pal[1]) theta = np.linspace(0, 2 * np.pi) with C.layer() as CX: CX += ph.polyline(color="k") CX += ph.transform.rotate(theta) CX += ph.filters.gaussian_blur(0.25, theta / 6) C += ph.circle(x=-1, r=1.0) C.show()