decomposer is now working
This commit is contained in:
@@ -4,16 +4,24 @@
|
|||||||
"""Bits Decomposer Analyzer for Image Submissions."""
|
"""Bits Decomposer Analyzer for Image Submissions."""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from .utils import update_data
|
from .base_analyzer import SubprocessAnalyzer
|
||||||
|
|
||||||
|
|
||||||
def analyze_decomposer(input_img: Path, output_dir: Path) -> None:
|
class DecomposerAnalyzer(SubprocessAnalyzer):
|
||||||
|
"""Analyzer for bits decomposer."""
|
||||||
|
|
||||||
|
def __init__(self, *args):
|
||||||
|
super().__init__("decomposer", *args)
|
||||||
|
|
||||||
|
def get_results(self, _: Optional[str] = None) -> dict[str, Any]:
|
||||||
"""Analyze an image submission using bits decomposition."""
|
"""Analyze an image submission using bits decomposition."""
|
||||||
img = Image.open(input_img)
|
try:
|
||||||
|
img = Image.open(self.input_img)
|
||||||
converted = False
|
converted = False
|
||||||
|
|
||||||
if img.mode == "P":
|
if img.mode == "P":
|
||||||
@@ -44,8 +52,8 @@ def analyze_decomposer(input_img: Path, output_dir: Path) -> None:
|
|||||||
rgb_image = np.stack(rgb_planes, axis=-1).astype(np.uint8)
|
rgb_image = np.stack(rgb_planes, axis=-1).astype(np.uint8)
|
||||||
rgb_img = Image.fromarray(rgb_image, mode="RGB")
|
rgb_img = Image.fromarray(rgb_image, mode="RGB")
|
||||||
img_name = f"superimposed_bit_{bit}.png"
|
img_name = f"superimposed_bit_{bit}.png"
|
||||||
out_path = output_dir / img_name
|
out_path = self.output_dir / img_name
|
||||||
dl_path = Path(output_dir.name) / img_name
|
dl_path = Path(self.output_dir.name) / img_name
|
||||||
superimposed_json.append("/image/" + str(dl_path))
|
superimposed_json.append("/image/" + str(dl_path))
|
||||||
rgb_img.save(out_path)
|
rgb_img.save(out_path)
|
||||||
|
|
||||||
@@ -61,21 +69,27 @@ def analyze_decomposer(input_img: Path, output_dir: Path) -> None:
|
|||||||
bit_plane = ((channel_data & bit_mask) >> bit) * 255
|
bit_plane = ((channel_data & bit_mask) >> bit) * 255
|
||||||
bit_img = Image.fromarray(bit_plane.astype(np.uint8), mode="L")
|
bit_img = Image.fromarray(bit_plane.astype(np.uint8), mode="L")
|
||||||
img_name = f"{channel_label}_bit_{bit}.png"
|
img_name = f"{channel_label}_bit_{bit}.png"
|
||||||
out_path = output_dir / img_name
|
out_path = self.output_dir / img_name
|
||||||
dl_path = Path(output_dir.name) / img_name
|
dl_path = Path(self.output_dir.name) / img_name
|
||||||
channel_json.append("/image/" + str(dl_path))
|
channel_json.append("/image/" + str(dl_path))
|
||||||
bit_img.save(out_path)
|
bit_img.save(out_path)
|
||||||
image_json[channel_label] = channel_json
|
image_json[channel_label] = channel_json
|
||||||
|
|
||||||
output = {
|
output = {
|
||||||
"decomposer": {
|
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"images": image_json,
|
"images": image_json,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if converted:
|
if converted:
|
||||||
output["decomposer"][
|
output["note"] = (
|
||||||
"note"
|
"Image contains a color palette and was converted to RGB for processing."
|
||||||
] = "Image contains a color palette and was converted to RGB for processing."
|
)
|
||||||
|
|
||||||
update_data(output_dir, output)
|
return output
|
||||||
|
except Exception as e:
|
||||||
|
return {"status": "error", "error": str(e)}
|
||||||
|
|
||||||
|
|
||||||
|
def analyze_decomposer(input_img: Path, output_dir: Path) -> None:
|
||||||
|
"""Analyze an image submission using bits decomposition."""
|
||||||
|
analyzer = DecomposerAnalyzer(input_img, output_dir)
|
||||||
|
analyzer.analyze()
|
||||||
|
|||||||
Reference in New Issue
Block a user