def test_format_inference_filename(): with tempfile.NamedTemporaryFile(suffix='.svg', delete=False) as f: name = f.name f.close() # Allow tests to run on Windows img = Image.from_file(name) assert img.format == 'svg+xml'
def test_value_repr_length(): with get_logo_png() as LOGO_PNG: with open(LOGO_PNG, 'rb') as f: img = Image.from_file(f) assert len(img.__repr__()) < 140 assert img.__repr__().endswith(")") assert img.__repr__()[-5:-2] == '...'
def helper_image(self: Core, *args): filename: str = args[0] x: int = int(args[1]) y: int = int(args[2]) w: int = int(args[3]) h: int = int(args[4]) key = (filename, abs(w), abs(h)) if key not in _loaded_images: _loaded_images[key] = Canvas(width=abs(w), height=abs(h)) _loaded_images[key].draw_image( Image.from_file(filename, width=abs(w), height=abs(h)), 0, 0, abs(w), abs(h)) self.canvas.translate(x, y) if w < 0: self.canvas.scale(-1, 1) if h < 0: self.canvas.scale(1, -1) self.canvas.draw_image(_loaded_images[key], 0, 0) if h < 0: self.canvas.scale(1, -1) if w < 0: self.canvas.scale(-1, 1) self.canvas.translate(-x, -y)
def test_format_inference_overridable(): with tempfile.NamedTemporaryFile(suffix='.svg', delete=False) as f: name = f.name f.close() # Allow tests to run on Windows img = Image.from_file(name, format='gif') assert img.format == 'gif'
def draw_splash(self, canvas, x, y, scale): if scale > 0.: splash_canvas = Canvas(width=self.cell_pixels, height=self.cell_pixels) sprite = Image.from_file('images/splash_2.png') with hold_canvas(splash_canvas): pos_x = self.cell_pixels // 2 pos_y = self.cell_pixels // 2 # Choose a random rotation angle # (but first set the rotation center with `translate`) splash_canvas.translate(pos_x, pos_y) splash_canvas.rotate(uniform(0., pi)) # scale the image splash_canvas.scale(scale) # Restore the canvas center splash_canvas.translate(-pos_x, -pos_y) # Draw the sprite splash_canvas.draw_image(sprite, 0, 0) x_px = x * self.cell_pixels + self.padding y_px = y * self.cell_pixels + self.padding canvas.draw_image(splash_canvas, x_px, y_px, width=self.cell_pixels, height=self.cell_pixels)
def draw_img(canvas, file, clear=False): # draws resized image on canvas and returns scale used with hold_canvas(canvas): if clear: canvas.clear() sprite1 = Image.from_file(file) width_canvas, height_canvas = canvas.width, canvas.height width_img, height_img = get_image_size(file) ratio_canvas = float(width_canvas) / height_canvas ratio_img = float(width_img) / height_img if ratio_img > ratio_canvas: # wider then canvas, scale to canvas width scale = width_canvas / width_img else: # taller then canvas, scale to canvas hight scale = height_canvas / height_img canvas.draw_image(sprite1, 0, 0, width=width_img * min(1, scale), height=height_img * min(1, scale)) return scale
def Single_Zone(self, Zone, fig=None, offset=np.array([0, 0])): w = self.margin wall_width = self.wall_th * self.scale Zone_w = (Zone.x_delta + 2 * w) * self.scale Zone_h = (Zone.y_delta + 2 * w) * self.scale canvas_w = Zone_w + 2 * wall_width canvas_h = Zone_h + 2 * wall_width w = w * self.scale if fig == None: canvas = MultiCanvas(4, width=canvas_w, height=canvas_h) else: canvas = fig # background canvas[0].translate(offset[0], offset[1]) Mars_img = Image.from_file('Images/Mars_surface.jpg') canvas3 = Canvas(width=1000, height=1000) canvas3.draw_image(Mars_img, 0, 0) canvas3.scale(3 * self.scale / 50) canvas[0].draw_image(canvas3, 0, 0) canvas[0].translate(-offset[0], -offset[1]) # Draw Zone canvas[1].translate(offset[0], offset[1]) canvas[1].fill_rect(0, 0, canvas_w, height=canvas_h) canvas[1].clear_rect(wall_width, wall_width, Zone_w, height=Zone_h) # Name of thr Zone canvas[1].font = '16px serif' canvas[1].fill_text(Zone.name, Zone_w / 2, 4 * wall_width) canvas[1].translate(-offset[0], -offset[1]) # Draw object insised the Zone canvas[2].translate(offset[0], offset[1]) if Zone.type == 'Landing_zone': charging = self.problem_2_canvas(Zone.charger) trash_bin = self.problem_2_canvas(Zone.deposit) canvas[2].fill_style = 'green' canvas[2].fill_rect(charging[0] - w / 2, charging[1] - w / 2, w) canvas[2].fill_style = 'blue' canvas[2].fill_rect(trash_bin[0] - w / 2, trash_bin[1] - w / 2, w) else: canvas[2].fill_style = 'brown' p_r = 0.1 x, y, radius = [], [], [] for i in range(0, Zone.max_sample): sam_coord = self.problem_2_canvas(Zone.samples_loc[i, :]) x.append(sam_coord[0]) y.append(sam_coord[1]) radius.append(p_r * self.scale) canvas[2].fill_circles(x, y, radius) for i in Zone.connections['Location']: canvas[2].fill_style = 'red' c_coord = self.problem_2_canvas(Zone.Location_2_coordinate[i]) x = c_coord[0] y = c_coord[1] canvas[2].fill_rect(x - w / 2, y - w / 2, w) canvas[2].translate(-offset[0], -offset[1]) return canvas
def load_sprites(self): ' load the sprite sheet and when loaded callback to split it into individual sprites ' sprites = Image.from_file('images/BabyRobot64_Sprites.png') self.sprite_canvas = Canvas(width=132, height=328, sync_image_data=True) self.sprite_canvas.draw_image(sprites, 0, 0) self.sprite_canvas.observe(self.get_array, 'image_data')
def add_tool(self, tool): self.tools[tool.tool_id] = tool icon = Image.from_file(icon_path(tool.icon, icon_format='svg'), width=ICON_WIDTH) button = ToggleButton(children=[icon], value=tool.tool_id) if self.children is None: self.children = (button, ) else: self.children = tuple(self.children) + (button, )
def from_file(cls, filename, **kwargs): """Create a `ImageStream` from a local file. Parameters ---------- filename: str The location of a file to read into the value from disk. **kwargs Extra keyword arguments for `ImageStream` """ return cls(image=Image.from_file(filename), **kwargs)
def add_tool(self, tool): self.tools[tool.tool_id] = tool icon = Image.from_file(icon_path(tool.icon, icon_format='svg'), width=ICON_WIDTH) button = v.Btn(v_on="tooltip.on", icon=True, children=[icon], value=tool.tool_id) annotated = v.Tooltip( bottom=True, v_slots=[{ 'name': 'activator', 'variable': 'tooltip', 'children': button}], children=[tool.tool_tip]) self.children = list(self.children) + [annotated]
def robot(self, canvas, x, y, room): rbt_img = Image.from_file('Images/robot.jpg') x = x * self.scale y = y * self.scale x += self.offset[room][0] + self.wall_th * self.scale y += self.offset[room][1] + self.wall_th * self.scale canvas3 = Canvas(width=1000, height=1000) canvas3.draw_image(rbt_img, 0, 0) canvas3.scale(0.1*self.scale/50) canvas.clear_rect(x, y, self.wall_th) canvas.draw_image(canvas3, x, y) return canvas
def test_serialize(): fstream = io.BytesIO(b'123') img = Image.from_file(fstream) img_state = img.get_state() # for python27 it is a memoryview assert isinstance(img_state['value'], (bytes, memoryview)) # make sure it is (for python 3), since that is what it will be once it comes off the wire img_state['value'] = memoryview(img_state['value']) # check that we can deserialize it and get back the original value img_copy = Image() img_copy.set_state(img_state) assert img.value == img_copy.value
def init_images(self): self.im_player = Image.from_file(path + '/images/player.jpg') self.im_player_hide = Image.from_file(path + '/images/player_hide.jpg') self.im_player_win = Image.from_file(path + '/images/player_win.jpg') self.im_player_loss = Image.from_file(path + '/images/player_loss.png') self.im_space = Image.from_file(path + '/images/space.jpg') self.im_chest = Image.from_file(path + '/images/chest_m.jpg') self.im_guards = [ Image.from_file(path + '/images/guard1.jpg'), Image.from_file(path + '/images/guard2.jpg'), Image.from_file(path + '/images/guard3.jpg') ] self.im_wall = Image.from_file(path + '/images/wall.jpg') self.imagedict = { obj.wall: self.im_wall, #0 obj.space: self.im_space, #1 (obj.player, player_state.show): self.im_player, #2 (obj.player, player_state.hide): self.im_player_hide, #2 (obj.player, player_state.win): self.im_player_win, #2 (obj.player, player_state.loss): self.im_player_loss, #2 obj.guard: self.im_guards, #3 obj.chest: self.im_chest } #4
def rover(self, canvas, coord, Zone): rvr_img = Image.from_file('Images/rover.jpg') canvas3 = Canvas(width=1000, height=1000) canvas3.draw_image(rvr_img, 0, 0) canvas3.scale(0.025 * self.scale / 50) coord_rvr = self.problem_2_canvas(coord) x = coord_rvr[0] - 0.25 * self.scale y = coord_rvr[1] - 0.25 * self.scale offset = Zone.coordinates * self.scale canvas[3].translate(offset[0], offset[1]) canvas[3].clear_rect(x, y, self.wall_th) canvas[3].draw_image(canvas3, x, y) canvas[3].translate(-offset[0], -offset[1]) return canvas
def draw_img(canvas, file, clear=False, has_border=False): """ draws resized image on canvas and returns scale used """ with hold_canvas(canvas): if clear: canvas.clear() sprite1 = Image.from_file(file) width_canvas, height_canvas = canvas.width, canvas.height width_img, height_img = get_image_size(file) ratio_canvas = float(width_canvas) / height_canvas ratio_img = float(width_img) / height_img if ratio_img > ratio_canvas: # wider then canvas, scale to canvas width scale = width_canvas / width_img else: # taller then canvas, scale to canvas hight scale = height_canvas / height_img image_width = width_img * min(1, scale) image_height = height_img * min(1, scale) image_x = 0 image_y = 0 if has_border: canvas.stroke_rect(x=0, y=0, width=image_width, height=image_height) image_width -= 2 image_height -= 2 image_x, image_y = 1, 1 canvas.draw_image(sprite1, image_x, image_y, width=image_width, height=image_height) return scale
def launch(self): self.cur_level = 1 self.init_map() self.init_images() self.create_panel() multi = MultiCanvas(2, width=self.map_size * self.n_pixels, height=self.map_size * self.n_pixels) multi[0].fill_style = 'black' multi[0].fill_rect(0, 0, multi.size[0], multi.size[1]) self.canvas = multi[1] self.output = widgets.Output() display( VBox([ Image.from_file(path + '/images/header.jpg', width=200, height=40), HBox([multi]) ]), self.output)
def draw_splash(self, canvas, x, y, puddle_type): if puddle_type > 0: # scale the puddle image according to its type (big or small) scale = puddle_type / 2 splash_canvas = Canvas(width=self.cell_pixels, height=self.cell_pixels) sprite = Image.from_file( os.path.join(self.working_directory, 'images/splash_2.png')) with hold_canvas(splash_canvas): pos_x = self.cell_pixels // 2 pos_y = self.cell_pixels // 2 # Choose a random rotation angle # (but first set the rotation center with `translate`) splash_canvas.translate(pos_x, pos_y) splash_canvas.rotate(uniform(0., pi)) # scale the image splash_canvas.scale(scale) # Restore the canvas center splash_canvas.translate(-pos_x, -pos_y) # Draw the sprite splash_canvas.draw_image(sprite, 0, 0) x_px = x * self.cell_pixels + self.padding y_px = y * self.cell_pixels + self.padding canvas.draw_image(splash_canvas, x_px, y_px, width=self.cell_pixels, height=self.cell_pixels)
def background(self, filename): """Set a background image.""" img = Image.from_file(filename) self._canvas[0].draw_image(img, x=0, y=0, \ width=self._canvas[0].size[0], height=self._canvas[0].size[1])
def test_format_inference_stream(): # There's no way to infer the format, so it should default to png fstream = io.BytesIO(b'') img = Image.from_file(fstream) assert img.format == 'png'
def test_format_inference_file(): with tempfile.NamedTemporaryFile(suffix='.gif', delete=False) as f: img = Image.from_file(f) assert img.format == 'gif'
def test_from_file(): with get_logo_png() as LOGO_PNG: with open(LOGO_PNG, 'rb') as f: img = Image.from_file(f) assert_equal_hash(img.value, LOGO_PNG_DIGEST)
def test_from_filename(): with get_logo_png() as LOGO_PNG: img = Image.from_file(LOGO_PNG) assert_equal_hash(img.value, LOGO_PNG_DIGEST)
def init_images(self, path=path_file): self.images_ava = {} self.images_ava_hide = {} avatars = [avatar.cowboy, avatar.stan, avatar.rock, avatar.pig, avatar.glass, avatar.dipper, avatar.zoose, \ avatar.super, avatar.garry, avatar.chui, avatar.lord, avatar.bill,] for ava in avatars: self.images_ava[( obj.player, ava)] = Image.from_file(path + '/images/avatar/{}.png'.format(ava)) self.images_ava_hide[ava] = Image.from_file( path + '/images/avatar/{}_h.png'.format(ava)) for ava in ava_guard.items(): self.images_ava[( obj.guard, ava)] = Image.from_file(path + '/images/{}.png'.format(ava)) self.images_ava[(obj.guard, ava_guard.killed)] = Image.from_file( path + '/images/{}.png'.format(ava_guard.killed)) for ava in ava_chest.items(): self.images_ava[( obj.chest, ava)] = Image.from_file(path + '/images/{}.png'.format(ava)) self.images_ava[(obj.chest, ava_chest.bulk)] = Image.from_file( path + '/images/{}.png'.format(ava_chest.bulk)) self.images_ava[(obj.wall, '')] = Image.from_file(path + '/images/wall.png') self.images_ava[(obj.ball, ava_ball.ball)] = Image.from_file(path + '/images/ball.png') for ava in ava_building.items(): self.images_ava[( obj.building, None)] = Image.from_file(path + '/images/{}.png'.format(ava)) self.image_killed = Image.from_file(path + '/images/player_loss.png') self.image_space = Image.from_file(path + '/images/space.jpg')
def test_value_repr_length(): with get_logo_png() as LOGO_PNG: with open(LOGO_PNG, 'rb') as f: img = Image.from_file(f) assert len(img.__repr__()) < 120 assert img.__repr__().endswith("...')")
def __init__(self, size=(400, 400)): self._size = size self._canvas = MultiCanvas(width=size[0], height=size[1]) self._turtle = Image.from_file( pathlib.Path(__file__).parent / "turtle.png") self.clear()