def test_name_invalid(self): image = Image() try: image.name = "myname.ext" self.fail("Should have raised exception") except InvalidImageError as e: self.assertTrue(type(e) is InvalidImageError)
def __add_captured_time_and_location(image: Image, path: str): with open(path, 'rb') as file: if image.type == 'heic': i = pyheif.read_heif(file) for metadata in i.metadata or []: if metadata['type'] == 'Exif': file = io.BytesIO( metadata['data'][6:] ) # for some reason there is a leading 'Exif00' .. ignore tags = exifread.process_file(file, details=False) if tags: if Constants.exif_date_time_original in tags.keys(): try: dt = datetime.strptime( str(tags[Constants.exif_date_time_original]), Constants.exif_date_time_format) except ValueError as e: print(e) print(image.full_path) else: image.captured = dt.timestamp() * 1000 if Constants.GPS['lat'] in tags.keys( ) and Constants.GPS['lon'] in tags.keys(): lat = Factory.__coord_from_dms( tags[Constants.GPS['lat']].values, tags[Constants.GPS['latR']].values) lon = Factory.__coord_from_dms( tags[Constants.GPS['lon']].values, tags[Constants.GPS['lonR']].values) image.set_location_from_lat_lon(lat, lon) if Constants.exif_width in tags.keys(): image.dimensions = f"{tags[Constants.exif_width].printable}x{tags[Constants.exif_height].printable}"
def get_image(seed, shape=-1, color=-1, n=1, nOtherShapes=0, shouldOthersBeSame=False): np.random.seed(seed) data = np.zeros((WIDTH, HEIGHT, 4), dtype=np.uint8) surf = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, WIDTH, HEIGHT) ctx = cairo.Context(surf) ctx.set_source_rgb(0.0, 0.0, 0.0) ctx.paint() shapes = [[None for c in range(N_CELLS)] for r in range(N_CELLS)] colors = [[None for c in range(N_CELLS)] for r in range(N_CELLS)] sizes = [[None for c in range(N_CELLS)] for r in range(N_CELLS)] shape = shape if shape >= 0 else np.random.randint(N_SHAPES) color = color if color >= 0 else np.random.randint(N_COLORS) for _ in range(n): # Random location r = np.random.randint(N_CELLS) c = np.random.randint(N_CELLS) shapes[r][c] = shape colors[r][c] = color sizes[r][c] = np.random.randint(N_SIZES) draw(shapes[r][c], colors[r][c], sizes[r][c], c, r, ctx) metadata = {"shapes": shapes, "colors": colors, "sizes": sizes} return Image(shapes, colors, sizes, data, metadata)
def get_target_image(seed, horizontal_position, vertical_position, shape, color, size): np.random.seed(seed) data = np.zeros((WIDTH, HEIGHT, 4), dtype=np.uint8) PIXEL_SCALE = 2 surf = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, WIDTH, HEIGHT) ctx = cairo.Context(surf) ctx.set_source_rgb(0.0, 0.0, 0.0) ctx.paint() shapes = [[None for c in range(N_CELLS)] for r in range(N_CELLS)] colors = [[None for c in range(N_CELLS)] for r in range(N_CELLS)] sizes = [[None for c in range(N_CELLS)] for r in range(N_CELLS)] shapes[horizontal_position][vertical_position] = shape colors[horizontal_position][vertical_position] = color sizes[horizontal_position][vertical_position] = size draw(shapes[horizontal_position][vertical_position], colors[horizontal_position][vertical_position], sizes[horizontal_position][vertical_position], vertical_position, horizontal_position, ctx) metadata = {"shapes": shapes, "colors": colors, "sizes": sizes} return Image(shapes, colors, sizes, data, metadata)
def from_elastic_entry(e): if e.kind == Constants.IMAGE_KIND: item = Image() elif e.kind == Constants.VIDEO_KIND: item = Video() elif e.kind == Constants.OTHER_KIND: item = Other() else: raise FactoryError( f"Entry mismatch, wrong kind {e.kind} found for: {e.name}; id:{e.meta.id}" ) item.full_path = os.path.join(e.path, e.name) if e.type != item.type: raise FactoryError(f"Type mismatch for {e.name}") if e.kind != item.kind: raise FactoryError(f"Kind mismatch for {e.name}") for attr, value in inspect.getmembers( e, lambda a: not (inspect.isroutine(a))): if attr not in Constants.leave_out_when_reading_from_elastic: setattr(item, attr, value) if item.hash != e.hash: raise FactoryError(f"Hash mismatch for {e.name}") if item.path_hash != e.path_hash: raise FactoryError(f"Path-hash mismatch for {e.name}") item.id = e.meta.id return item
def from_dropbox(entry): """Create an Image or Video object based on the dropbox path given""" try: result = Image() result.full_path = entry['path'] except InvalidImageError: try: result = Video() result.full_path = entry['path'] except InvalidVideoError: try: result = Other() result.full_path = entry['path'] except InvalidOtherError: raise FactoryError( f"Path {entry['path']} is neither image nor video nor other" ) del entry['path'] return result.update(entry)
def load_image(self): if LoadingWidget._image is None: LoadingWidget._image = Image(loading_path)
def load_data(queue, filename): try: data = {'image': Image(filename)} except ImageLoadError as e: data = {'error': e} queue.put(data)
def generate_image(seed, horizontal_position, vertical_position, shape, color, size, property_to_change: ImageProperty): np.random.seed(seed) target_image = get_target_image(seed, horizontal_position, vertical_position, shape, color, size) target_image.data = target_image.data[:, :, 0:3] if property_to_change == ImageProperty.Shape: n = N_SHAPES - 1 elif property_to_change == ImageProperty.Size: n = N_SIZES - 1 elif property_to_change == ImageProperty.Color: n = N_COLORS - 1 else: n = N_CELLS - 1 new_horizontal_position = horizontal_position new_vertical_position = vertical_position new_shape = shape new_color = color new_size = size result_images = [] value_to_change = 0 for _ in range(n): data = np.zeros((WIDTH, HEIGHT, 4), dtype=np.uint8) surf = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, WIDTH, HEIGHT) ctx = cairo.Context(surf) ctx.set_source_rgb(0.0, 0.0, 0.0) ctx.paint() shapes = [[None for c in range(N_CELLS)] for r in range(N_CELLS)] colors = [[None for c in range(N_CELLS)] for r in range(N_CELLS)] sizes = [[None for c in range(N_CELLS)] for r in range(N_CELLS)] if property_to_change == ImageProperty.Shape: if value_to_change == shape: value_to_change += 1 new_shape = value_to_change elif property_to_change == ImageProperty.Size: if value_to_change == size: value_to_change += 1 new_size = value_to_change elif property_to_change == ImageProperty.Color: if value_to_change == color: value_to_change += 1 new_color = value_to_change elif property_to_change == ImageProperty.HorizontalPosition: if value_to_change == horizontal_position: value_to_change += 1 new_horizontal_position = value_to_change elif property_to_change == ImageProperty.VerticalPosition: if value_to_change == vertical_position: value_to_change += 1 new_vertical_position = value_to_change # Random location shapes[new_horizontal_position][new_vertical_position] = new_shape colors[new_horizontal_position][new_vertical_position] = new_color sizes[new_horizontal_position][new_vertical_position] = new_size draw(shapes[new_horizontal_position][new_vertical_position], colors[new_horizontal_position][new_vertical_position], sizes[new_horizontal_position][new_vertical_position], new_vertical_position, new_horizontal_position, ctx) value_to_change += 1 metadata = {"shapes": shapes, "colors": colors, "sizes": sizes} new_image = Image(shapes, colors, sizes, data, metadata) new_image.data = new_image.data[:, :, 0:3] result_images.append(new_image) return target_image, result_images
def __image_from_directory_item(path: str) -> Image: image = Image() Factory.__entry_from_directory_item(image, path) Factory.__add_captured_time_and_location(image, path) return image
def test_diff(self): image = Image() image.full_path = "/this/is/my/image.png" image.modified = 1000000 image.size = 10 image.checksum = "ABCDEFGH" self.assertFalse(image.diff(image)) image2 = Image() image2.full_path = image.full_path image2.modified = 2000000 image2.size = image.size image2.checksum = image.checksum self.assertEqual(image.diff(image2), {"modified": image2.modified})
def test_uppercase_image(self): image = Image() image.name = "MyPic.PNG" self.assertEqual(image.type, "png") self.assertEqual(image.kind, Constants.IMAGE_KIND) self.assertEqual(image.name, "MyPic.PNG")
def test_jpeg_image(self): image = Image() image.name = "image.jpeg" self.assertEqual(image.type, "jpeg") self.assertEqual(image.kind, Constants.IMAGE_KIND) self.assertEqual(image.name, "image.jpeg")
def load_image(self): if CardWidget._image is None: CardWidget._image = Image(card_path)