Skip to content

Commit a9c177d

Browse files
authored
Fixing jpg handling in ParsedMedia.to_image_url (#1150)
1 parent 208d9dd commit a9c177d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/paperqa/types.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,11 @@ def __eq__(self, other) -> bool:
549549
== json.dumps(other.info, sort_keys=True)
550550
)
551551

552-
def to_image_url(self, image_type: str = "png") -> str:
552+
def to_image_url(self) -> str:
553553
"""Convert the image data to an RFC 2397 data URL format."""
554+
image_type = cast(str, self.info.get("suffix", "png")).removeprefix(".")
555+
if image_type == "jpg": # SEE: https://stackoverflow.com/a/54488403
556+
image_type = "jpeg"
554557
return encode_image_as_url(image_type, self.data)
555558

556559
def save(self, path: str | os.PathLike) -> None:

tests/test_paperqa.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,28 @@ async def test_chunk_metadata_reader(
14481448
)
14491449

14501450

1451+
def test_media_to_image_url(subtests: SubTests) -> None:
1452+
with subtests.test(msg="jpg"):
1453+
media = ParsedMedia(index=0, data=b"fake_jpg", info={"suffix": ".jpg"})
1454+
url = media.to_image_url()
1455+
assert "image/jpeg" in url
1456+
1457+
with subtests.test(msg="jpeg"):
1458+
media = ParsedMedia(index=0, data=b"fake_jpeg", info={"suffix": ".jpeg"})
1459+
url = media.to_image_url()
1460+
assert "image/jpeg" in url
1461+
1462+
with subtests.test(msg="png"):
1463+
media = ParsedMedia(index=0, data=b"fake_png", info={"suffix": ".png"})
1464+
url = media.to_image_url()
1465+
assert "image/png" in url
1466+
1467+
with subtests.test(msg="default"):
1468+
media = ParsedMedia(index=0, data=b"fake_png")
1469+
url = media.to_image_url()
1470+
assert "image/png" in url
1471+
1472+
14511473
@pytest.mark.asyncio
14521474
async def test_image_aggregation(stub_data_dir: Path) -> None:
14531475
png_path = stub_data_dir / "sf_districts.png"

0 commit comments

Comments
 (0)