Skip to content

Commit

Permalink
8-bit conversion flag
Browse files Browse the repository at this point in the history
  • Loading branch information
chaichontat committed Mar 8, 2024
1 parent baa3df1 commit 58799f9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
10 changes: 6 additions & 4 deletions loopy/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ def cli():
@click.option(
"scale", "--scale", "-s", default=1, type=float, help="Scale in meters per pixel.", show_default=True
)
@click.option(
"quality", "--quality", default=90, type=int, help="JPEG compression quality.", show_default=True
)
@click.option(
"translate",
"--translate",
Expand All @@ -50,11 +47,16 @@ def cli():
type=click.Tuple([float, float]),
help="Translation to be applied in y and x.",
)
@click.option("--convert8bit", is_flag=True, help="Convert the image to 8-bit. ")
@click.option(
"quality", "--quality", default=90, type=int, help="JPEG compression quality. Only applies if image is 8-bit or converting to 8-bit.", show_default=True
)
def image(
tiff: Path,
out: Path | None = None,
name: str | None = None,
channels: str | None = None,
convert8bit: bool = False,
quality: int = 90,
scale: float = 1,
translate: tuple[float, float] = (0, 0),
Expand All @@ -79,7 +81,7 @@ def image(

(
Sample(name=name, path=out / name)
.add_image(tiff, channels=c, scale=scale, translate=translate, quality=quality)
.add_image(tiff, channels=c, scale=scale, translate=translate, quality=quality, convert_to_8bit=convert8bit)
.write()
)

Expand Down
8 changes: 5 additions & 3 deletions loopy/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class Config:

@classmethod
def from_tiff(
cls, tif: Path, *, scale: float, translate: tuple[float, float] = (0, 0), rgb: bool = False
cls, tif: Path, *, scale: float, translate: tuple[float, float] = (0, 0), rgb: bool = False, convert_to_8bit: bool = False
) -> Self:
return cls.from_img(imread(tif), scale=scale, translate=translate, rgb=rgb)
return cls.from_img(imread(tif), scale=scale, translate=translate, rgb=rgb, convert_to_8bit=convert_to_8bit)

@classmethod
def from_img(
Expand Down Expand Up @@ -92,7 +92,9 @@ def from_img(
elif img.dtype == np.uint16:
if convert_to_8bit:
log("Converting uint16 to uint8.", type_="WARNING")
dived = np.divide(img, 256, casting="unsafe") # So that this remains an uint16.
maxval = img.max()
divide = 2 ** (int(np.log2(maxval)) + 1) // 256
dived = np.divide(img, divide, casting="unsafe") # So that this remains an uint16.
del img
img = dived.astype(np.uint8)
del dived
Expand Down
3 changes: 2 additions & 1 deletion loopy/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def add_image(
scale: float = 1,
quality: int = 90,
translate: tuple[float, float] = (0, 0),
convert_to_8bit: bool = False,
defaultChannels: dict[Colors, str] | None = None,
save_uncompressed: bool = False,
) -> Self:
Expand All @@ -161,7 +162,7 @@ def add_image(
if not tiff.exists():
raise ValueError(f"Tiff file {tiff} not found")

geotiff = GeoTiff.from_tiff(tiff, scale=scale, translate=translate, rgb=channels == "rgb")
geotiff = GeoTiff.from_tiff(tiff, scale=scale, translate=translate, rgb=channels == "rgb", convert_to_8bit=convert_to_8bit)

if channels is None:
channels = [f"C{i}" for i in range(1, geotiff.chans + 1)]
Expand Down

0 comments on commit 58799f9

Please sign in to comment.