change: Implement 5: Save selected images to the caller directory and downscale only behind the resize flag
Repository Validate / repository-checks (pull_request) Successful in 4s
Repository Validate / repository-checks (pull_request) Successful in 4s
Implemented image saving into the caller directory and exact-size downscaling behind --resize.
tools/images.py: added stdlib-only helpers `png_chunks`, `decode_png`, `encode_png`, `ico_wrap`, `ico_unwrap`, plus `decode_image`, `resample_rows`, `encode_image` and the `DecodedImage` dataclass; also fixed a real pixel-stride bug in RGBA upsampling (channel step was used as pixel stride, corrupting 8-bit pixels).
tools/example.py: removed the inlined codec (now delegated to tools.images); `--download` writes each selected image into the caller-specified directory; each stored file is read back and decoded, so a reported path only names a file that exists and opens as the requested format (`format`/`width`/`height`/`bytes` reported per item, no path for a failed item); `--download` without `--output-dir` is now an `invalid_request` (`request_field`, field=output_dir) instead of a hidden default, and DEFAULT_OUTPUT_DIR is gone; `--resize` stores exactly the requested width/height (one open dimension keeps the source value), re-encodes in the requested format, never upscales and leaves a fitting image's bytes untouched; a per-item save failure is reported in that item and ends the run with `partial_download` (exit 6), including `saved_paths` in the error details.
tests/test_download_resize.py: new focused suite over tmp_path dirs and locally built png/ico fixtures (save paths exist and decode, requested-format check, missing --output-dir invalid_request, failing save => partial_download, exact resize dims for png and ico, no upscaling, byte/dimension preservation without --resize, plus codec round-trip tests).
tests/test_cli.py: updated the aspect-ratio resize expectation to the required exact 100x100 stored size.
Target branch task/1-task is already the current head (d5bb24f), so the tested revision contains the target.
Tests:
Facade commands run from /workspace with .venv/bin on PATH (prepared via prepare_environment tools restore): `python -m compileall tools tests` (tools.validate) OK; `python -m tools.example examples/input.json` (tools.smoke) exit 0 with status dry_run; `python -m tools.example --help` (acceptance help-command) exit 0; `python -c "import tools, tools.example, tools.images"` (package_import) OK; `pytest` (tools.test, the exact facade entrypoint) => 142 passed. New tests: tests/test_download_resize.py (13 tests). No network-dependent live-source check was run (sandbox has no egress); all fixtures are locally built real png/ico payloads.
Progress:
Completed issue 7: stdlib PNG/ICO decode/encode/wrap/unwrap plus resample helpers live in tools/images.py; tools/example.py saves selected images into the caller directory, requires --output-dir with --download (invalid_request otherwise), verifies every stored file by decoding it before reporting its path, downscales to exactly the requested width/height in the requested format only when --resize is set (never upscaling, otherwise byte-identical), and reports a per-item save failure with the partial_download status/exit code. New tests in tests/test_download_resize.py cover all of that; tests/test_cli.py resize expectation aligned with the exact-size requirement. Facade validate/smoke/help/import all pass and `pytest` reports 142 passed on the revision that already contains target task/1-task.
Published by developer-agent through Tool Backend.
Closes #7
This commit was merged in pull request #15.
This commit is contained in:
+368
@@ -22,6 +22,11 @@ It deliberately depends on nothing but the standard library:
|
||||
* :func:`inspect_image` combines both into an :class:`ImageInfo`.
|
||||
* :func:`png_bytes` and :func:`ico_bytes` *build* the same formats so tests (and
|
||||
callers) can produce real payloads without Pillow or any other dependency.
|
||||
* :func:`decode_png`, :func:`encode_png`, :func:`ico_wrap` and
|
||||
:func:`ico_unwrap` decode and re-encode those containers, and
|
||||
:func:`decode_image`, :func:`resample_rows` and :func:`encode_image` are the
|
||||
building blocks the CLI uses to downscale a downloaded image and store it in
|
||||
the requested format -- still with nothing but the standard library.
|
||||
|
||||
:class:`ImageFormatError` -- a :class:`~tools.errors.ToolkitError` subclass with
|
||||
a stable ``code`` -- is reserved for bytes that are genuinely unusable: an
|
||||
@@ -63,6 +68,15 @@ __all__ = [
|
||||
"png_bytes",
|
||||
"read_ico_dimensions",
|
||||
"read_png_dimensions",
|
||||
"DecodedImage",
|
||||
"decode_image",
|
||||
"decode_png",
|
||||
"encode_image",
|
||||
"encode_png",
|
||||
"ico_unwrap",
|
||||
"ico_wrap",
|
||||
"png_chunks",
|
||||
"resample_rows",
|
||||
]
|
||||
|
||||
#: The two formats the CLI accepts as search requests.
|
||||
@@ -401,3 +415,357 @@ def ico_bytes(
|
||||
)
|
||||
offset += len(payload)
|
||||
return header + bytes(entries) + b"".join(payloads)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Decoding and re-encoding: PNG pixels and ICO containers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
#: Channels per PNG colour type (0 grey, 2 rgb, 3 palette, 4 grey+alpha, 6 rgba).
|
||||
_PNG_CHANNELS = {0: 1, 2: 3, 3: 1, 4: 2, 6: 4}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DecodedImage:
|
||||
"""The container format, pixel size and 8-bit RGBA scanlines of a payload."""
|
||||
|
||||
format: str
|
||||
width: int
|
||||
height: int
|
||||
rows: tuple[bytes, ...]
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
"""JSON-ready projection: format plus the real pixel size."""
|
||||
|
||||
return {"format": self.format, "width": self.width, "height": self.height}
|
||||
|
||||
|
||||
def png_chunks(payload: bytes) -> tuple[tuple[bytes, bytes], ...]:
|
||||
"""Split a PNG payload into ``(chunk_type, chunk_data)`` pairs.
|
||||
|
||||
Raises :class:`ImageFormatError` when the signature is missing, a chunk is
|
||||
truncated or the stream never reaches ``IEND``: those bytes cannot be
|
||||
decoded, so stitching a half image would be worse than reporting it.
|
||||
"""
|
||||
|
||||
data = bytes(payload)
|
||||
if not data.startswith(PNG_SIGNATURE):
|
||||
raise ImageFormatError(
|
||||
"payload does not start with the PNG signature",
|
||||
details={"signature": _probe(data).hex()},
|
||||
)
|
||||
chunks: list[tuple[bytes, bytes]] = []
|
||||
offset = len(PNG_SIGNATURE)
|
||||
while offset + 8 <= len(data):
|
||||
(length,) = struct.unpack(">I", data[offset : offset + 4])
|
||||
chunk_type = data[offset + 4 : offset + 8]
|
||||
chunk_data = data[offset + 8 : offset + 8 + length]
|
||||
if len(chunk_data) != length:
|
||||
raise ImageFormatError(
|
||||
"PNG chunk is truncated",
|
||||
details={"chunk": chunk_type.decode("latin-1"), "length": length},
|
||||
)
|
||||
chunks.append((chunk_type, chunk_data))
|
||||
offset += 12 + length
|
||||
if chunk_type == b"IEND":
|
||||
return tuple(chunks)
|
||||
raise ImageFormatError("PNG image has no IEND chunk")
|
||||
|
||||
|
||||
def _unfilter_rows(raw: bytes, width: int, height: int, bpp: int) -> list[bytearray]:
|
||||
"""Undo the per-scanline PNG filters, returning one row per pixel line."""
|
||||
|
||||
stride = width * bpp
|
||||
if len(raw) < height * (stride + 1):
|
||||
raise ImageFormatError(
|
||||
"PNG image data is truncated",
|
||||
details={"width": width, "height": height, "bytes": len(raw)},
|
||||
)
|
||||
rows: list[bytearray] = []
|
||||
previous = bytearray(stride)
|
||||
position = 0
|
||||
for _ in range(height):
|
||||
filter_type = raw[position]
|
||||
position += 1
|
||||
line = bytearray(raw[position : position + stride])
|
||||
position += stride
|
||||
if filter_type == 1:
|
||||
for index in range(bpp, stride):
|
||||
line[index] = (line[index] + line[index - bpp]) & 0xFF
|
||||
elif filter_type == 2:
|
||||
for index in range(stride):
|
||||
line[index] = (line[index] + previous[index]) & 0xFF
|
||||
elif filter_type == 3:
|
||||
for index in range(stride):
|
||||
left = line[index - bpp] if index >= bpp else 0
|
||||
line[index] = (line[index] + ((left + previous[index]) >> 1)) & 0xFF
|
||||
elif filter_type == 4:
|
||||
for index in range(stride):
|
||||
left = line[index - bpp] if index >= bpp else 0
|
||||
up = previous[index]
|
||||
up_left = previous[index - bpp] if index >= bpp else 0
|
||||
estimate = left + up - up_left
|
||||
to_left = abs(estimate - left)
|
||||
to_up = abs(estimate - up)
|
||||
to_up_left = abs(estimate - up_left)
|
||||
if to_left <= to_up and to_left <= to_up_left:
|
||||
predictor = left
|
||||
elif to_up <= to_up_left:
|
||||
predictor = up
|
||||
else:
|
||||
predictor = up_left
|
||||
line[index] = (line[index] + predictor) & 0xFF
|
||||
elif filter_type != 0:
|
||||
raise ImageFormatError(
|
||||
"unsupported PNG filter type",
|
||||
details={"filter_type": int(filter_type)},
|
||||
)
|
||||
previous = line
|
||||
rows.append(line)
|
||||
return rows
|
||||
|
||||
|
||||
def _row_to_rgba(
|
||||
row: bytearray,
|
||||
width: int,
|
||||
color_type: int,
|
||||
depth: int,
|
||||
palette: bytes,
|
||||
transparency: bytes | None,
|
||||
) -> bytes:
|
||||
"""Convert one unfiltered scanline into 8-bit RGBA samples."""
|
||||
|
||||
step = depth // 8
|
||||
# Bytes per pixel: one sample per channel, so the channel offsets inside a
|
||||
# pixel are ``step`` apart while pixels are ``channels * step`` apart.
|
||||
pixel_stride = _PNG_CHANNELS[color_type] * step
|
||||
out = bytearray()
|
||||
if color_type == 3:
|
||||
for x in range(width):
|
||||
index = row[x]
|
||||
base = index * 3
|
||||
if base + 2 >= len(palette):
|
||||
raise ImageFormatError(
|
||||
"PNG palette index is out of range",
|
||||
details={"index": int(index), "palette_bytes": len(palette)},
|
||||
)
|
||||
alpha = transparency[index] if transparency and index < len(transparency) else 255
|
||||
out += bytes((palette[base], palette[base + 1], palette[base + 2], alpha))
|
||||
return bytes(out)
|
||||
for x in range(width):
|
||||
offset = x * pixel_stride
|
||||
if color_type == 0:
|
||||
value = row[offset]
|
||||
out += bytes((value, value, value, 255))
|
||||
elif color_type == 4:
|
||||
value = row[offset]
|
||||
out += bytes((value, value, value, row[offset + step]))
|
||||
elif color_type == 2:
|
||||
out += bytes((row[offset], row[offset + step], row[offset + 2 * step], 255))
|
||||
else:
|
||||
out += bytes(
|
||||
(
|
||||
row[offset],
|
||||
row[offset + step],
|
||||
row[offset + 2 * step],
|
||||
row[offset + 3 * step],
|
||||
)
|
||||
)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def decode_png(payload: bytes) -> tuple[int, int, tuple[bytes, ...]]:
|
||||
"""Decode a PNG payload into ``(width, height, rgba_rows)``.
|
||||
|
||||
Supports the non-interlaced 8-bit and 16-bit PNG colour types ImageMagick,
|
||||
GIMP and the Wikimedia thumbnailers produce; anything else raises
|
||||
:class:`ImageFormatError` instead of guessing at pixels.
|
||||
"""
|
||||
|
||||
header: tuple[int, ...] | None = None
|
||||
palette = b""
|
||||
transparency: bytes | None = None
|
||||
data = bytearray()
|
||||
for chunk_type, chunk_data in png_chunks(payload):
|
||||
if chunk_type == b"IHDR":
|
||||
if len(chunk_data) < 13:
|
||||
raise ImageFormatError("PNG header is truncated")
|
||||
header = struct.unpack(">IIBBBBB", chunk_data[:13])
|
||||
elif chunk_type == b"PLTE":
|
||||
palette = chunk_data
|
||||
elif chunk_type == b"tRNS":
|
||||
transparency = chunk_data
|
||||
elif chunk_type == b"IDAT":
|
||||
data += chunk_data
|
||||
if header is None:
|
||||
raise ImageFormatError("PNG image has no IHDR chunk")
|
||||
width, height, depth, color_type, compression, filter_method, interlace = header
|
||||
if width < 1 or height < 1:
|
||||
raise ImageFormatError("PNG image declares no pixels")
|
||||
if compression != 0 or filter_method != 0:
|
||||
raise ImageFormatError(
|
||||
"unsupported PNG compression or filter method",
|
||||
details={"compression": compression, "filter_method": filter_method},
|
||||
)
|
||||
if interlace != 0:
|
||||
raise ImageFormatError(
|
||||
"interlaced PNG images are not supported",
|
||||
details={"interlace": interlace},
|
||||
)
|
||||
channels = _PNG_CHANNELS.get(color_type)
|
||||
if channels is None:
|
||||
raise ImageFormatError(
|
||||
"unsupported PNG colour type",
|
||||
details={"color_type": color_type},
|
||||
)
|
||||
if depth not in (8, 16):
|
||||
raise ImageFormatError(
|
||||
"unsupported PNG bit depth",
|
||||
details={"bit_depth": depth},
|
||||
)
|
||||
if color_type == 3 and depth > 8:
|
||||
raise ImageFormatError(
|
||||
"unsupported PNG palette bit depth",
|
||||
details={"bit_depth": depth},
|
||||
)
|
||||
if color_type == 3 and not palette:
|
||||
raise ImageFormatError("PNG palette image has no PLTE chunk")
|
||||
try:
|
||||
raw = zlib.decompress(bytes(data))
|
||||
except zlib.error as exc:
|
||||
raise ImageFormatError(
|
||||
"PNG image data is corrupt",
|
||||
details={"reason": str(exc)},
|
||||
) from exc
|
||||
rows = _unfilter_rows(raw, width, height, channels * (depth // 8))
|
||||
rgba_rows = tuple(
|
||||
_row_to_rgba(row, width, color_type, depth, palette, transparency) for row in rows
|
||||
)
|
||||
return int(width), int(height), rgba_rows
|
||||
|
||||
|
||||
def encode_png(width: int, height: int, rows: Sequence[bytes]) -> bytes:
|
||||
"""Encode 8-bit RGBA ``rows`` as a real PNG with correct chunk CRCs."""
|
||||
|
||||
return png_bytes(width, height, rows=list(rows))
|
||||
|
||||
|
||||
def ico_wrap(embedded_png: bytes, width: int, height: int) -> bytes:
|
||||
"""Wrap a PNG payload in a single-entry ICO container of that size."""
|
||||
|
||||
if not 1 <= width <= ICO_MAX_DIMENSION:
|
||||
raise ImageFormatError(
|
||||
"ICO widths must be between 1 and 256 pixels",
|
||||
details={"width": width},
|
||||
)
|
||||
if not 1 <= height <= ICO_MAX_DIMENSION:
|
||||
raise ImageFormatError(
|
||||
"ICO heights must be between 1 and 256 pixels",
|
||||
details={"height": height},
|
||||
)
|
||||
png_payload = bytes(embedded_png)
|
||||
if not png_payload.startswith(PNG_SIGNATURE):
|
||||
raise ImageFormatError("an ICO container can only wrap a PNG payload here")
|
||||
header = struct.pack("<HHH", 0, 1, 1)
|
||||
entry = struct.pack(
|
||||
"<BBBBHHII",
|
||||
width % ICO_MAX_DIMENSION,
|
||||
height % ICO_MAX_DIMENSION,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
32,
|
||||
len(png_payload),
|
||||
ICO_HEADER_SIZE + ICO_ENTRY_SIZE,
|
||||
)
|
||||
return header + entry + png_payload
|
||||
|
||||
|
||||
def ico_unwrap(payload: bytes) -> tuple[int, int, bytes]:
|
||||
"""Return the ``(width, height, embedded_png)`` of an ICO's largest frame."""
|
||||
|
||||
entries = ico_entries(payload)
|
||||
width, height, size, offset = max(entries, key=lambda entry: entry[0] * entry[1])
|
||||
embedded = bytes(payload)[offset : offset + size]
|
||||
if not embedded.startswith(PNG_SIGNATURE):
|
||||
raise ImageFormatError(
|
||||
"ICO frames stored as raw bitmaps are not supported",
|
||||
details={"width": width, "height": height},
|
||||
)
|
||||
return int(width), int(height), embedded
|
||||
|
||||
|
||||
def decode_image(payload: bytes) -> DecodedImage:
|
||||
"""Decode a PNG or ICO payload into :class:`DecodedImage` RGBA scanlines."""
|
||||
|
||||
data = bytes(payload)
|
||||
image_format = detect_format(data)
|
||||
if image_format == PNG:
|
||||
width, height, rows = decode_png(data)
|
||||
return DecodedImage(format=PNG, width=width, height=height, rows=rows)
|
||||
if image_format == ICO:
|
||||
width, height, embedded = ico_unwrap(data)
|
||||
inner_width, inner_height, rows = decode_png(embedded)
|
||||
if (inner_width, inner_height) != (width, height):
|
||||
# The directory is authoritative for the reported size, but the
|
||||
# pixels must agree or the container is inconsistent.
|
||||
raise ImageFormatError(
|
||||
"ICO directory and embedded PNG disagree about the size",
|
||||
details={
|
||||
"directory": {"width": width, "height": height},
|
||||
"embedded": {"width": inner_width, "height": inner_height},
|
||||
},
|
||||
)
|
||||
return DecodedImage(format=ICO, width=width, height=height, rows=rows)
|
||||
raise ImageFormatError(
|
||||
"pixel data is only decodable for png and ico",
|
||||
details={"format": image_format},
|
||||
)
|
||||
|
||||
|
||||
def resample_rows(
|
||||
rows: Sequence[bytes],
|
||||
width: int,
|
||||
height: int,
|
||||
target_width: int,
|
||||
target_height: int,
|
||||
) -> tuple[bytes, ...]:
|
||||
"""Nearest-neighbour resample of RGBA ``rows`` to exactly the target size."""
|
||||
|
||||
if target_width < 1 or target_height < 1:
|
||||
raise ImageFormatError("resampled dimensions must be at least 1x1")
|
||||
if len(rows) != height:
|
||||
raise ImageFormatError(
|
||||
"row count does not match the source height",
|
||||
details={"rows": len(rows), "height": height},
|
||||
)
|
||||
if width < 1 or height < 1:
|
||||
raise ImageFormatError("source image has no pixels")
|
||||
columns = [min(width - 1, (x * width) // target_width) * 4 for x in range(target_width)]
|
||||
resized: list[bytes] = []
|
||||
for y in range(target_height):
|
||||
source = rows[min(height - 1, (y * height) // target_height)]
|
||||
line = bytearray()
|
||||
for offset in columns:
|
||||
line += source[offset : offset + 4]
|
||||
resized.append(bytes(line))
|
||||
return tuple(resized)
|
||||
|
||||
|
||||
def encode_image(
|
||||
image_format: str,
|
||||
width: int,
|
||||
height: int,
|
||||
rows: Sequence[bytes],
|
||||
) -> bytes:
|
||||
"""Encode RGBA ``rows`` in ``image_format`` (``png`` or ``ico``)."""
|
||||
|
||||
png_payload = encode_png(width, height, rows)
|
||||
if image_format == PNG:
|
||||
return png_payload
|
||||
if image_format == ICO:
|
||||
return ico_wrap(png_payload, width, height)
|
||||
raise ImageFormatError(
|
||||
"only png and ico can be encoded",
|
||||
details={"format": image_format},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user