def test_show_embedded_jpeg(tmp_path): data = np.random.random((100, 100)) rescaled = (255.0 / data.max() * (data - data.min())).astype(np.uint8) im = PILImage.fromarray(rescaled) uri = tmp_path / "test.jpg" im.save(uri) result = Image(uri)._repr_jpeg_() with open(uri, "rb") as fh: expected = b2a_base64(fh.read()).decode("ascii") assert result == expected fh.seek(0) embedded_image = Image(fh) assert result == embedded_image._repr_jpeg_()
def test_show_remote_ref(): from IPython.display import Image as IPyImage uri = "https://octodex.github.com/images/original.png" img = Image(uri) # TODO check the actual content assert img._repr_html_() == img.display()._repr_html_() assert img.display()._repr_html_() == IPyImage(uri)._repr_html_()
def test_embeded_image_from_bytesio(): data = np.random.random((100, 100)) rescaled = (255.0 / data.max() * (data - data.min())).astype(np.uint8) im = PILImage.fromarray(rescaled) buf = BytesIO() im.save(buf, format="PNG") buf.seek(0) image = Image(buf) assert np.array_equal(image.to_numpy(), rescaled)
def test_image_use_https_uri(): img = Image(WIKIPEDIA) fobj = BytesIO( requests.get(WIKIPEDIA, headers={ "User-Agent": "curl/7.72.0" }).content) pic = PIL.Image.open(fobj) assert np.array_equal(img.to_numpy(), np.array(pic))
def test_crop_in_batch(): uri = "http://farm2.staticflickr.com/1129/4726871278_4dd241a03a_z.jpg" img = Image(uri) data = img.to_numpy() patches = img.crop( [Box2d(10, 10, 30, 30), Box2d(15, 15, 35, 35), Box2d(20, 20, 40, 40)]) assert len(patches) == 3 assert np.array_equal(patches[0].to_numpy(), data[10:30, 10:30, :]) assert np.array_equal(patches[1].to_numpy(), data[15:35, 15:35, :]) assert np.array_equal(patches[2].to_numpy(), data[20:40, 20:40, :])
def image_copy(img: Image, uri: str) -> Image: """Copy the image to a new destination, specified by the URI. Parameters ---------- img : Image An image object uri : str The base directory to copy the image to. Return ------ Image Return a new image pointed to the new URI """ logger.info("Copying image src=%s dest=%s", img.uri, uri) return Image(_copy(img.uri, uri))
def to_image(image_data: Union[bytes, bytearray, str, Path]) -> Image: """Build an :py:class:`Image` from bytes, file-like object, str, or :py:class:`~pathlib.Path`. Parameters ---------- image_data : bytes, bytearray, str, Path The resource identifier or bytes of the source image. Return ------ img: Image An Image from the given embedded data or URI Example ------- >>> df = spark.read.format("image").load("<path-to-data>") >>> >>> df.withColumn("new_image", to_image("image.data")) """ return Image(image_data)
def deserialize(self, datum) -> "Image": from rikai.types.vision import Image # pylint: disable=import-outside-toplevel return Image(datum[0])
def image(uri: str) -> Image: """Build an :py:class:`Image` from a URI.""" return Image(uri)
def deserialize(self, datum) -> "Image": from rikai.types.vision import Image return Image(datum[0] or datum[1])
def test_crop_real_image(): uri = "http://farm2.staticflickr.com/1129/4726871278_4dd241a03a_z.jpg" img = Image(uri) data = img.to_numpy() patch = img.crop(Box2d(10, 10, 30, 30)) assert np.array_equal(patch.to_numpy(), data[10:30, 10:30, :])
def uri_to_pil(uri): # TODO: We can remove this after UDT is supported in Spark return Image(uri).to_pil()
def test_image_use_https_uri(): img = Image(WIKIPEDIA) fobj = BytesIO(requests.get(WIKIPEDIA).content) pic = PIL.Image.open(fobj) assert np.array_equal(img.to_numpy(), np.array(pic))