MCPcopy Create free account
hub / github.com/RASAAS/docmcp-knowledge / _compress_image

Function _compress_image

scripts/fetch_imdrf_fulltext.py:65–100  ·  view source on GitHub ↗

Compress image and return as base64 data URI HTML tag.

(image_bytes: bytes, content_type: str, alt_text: str = "")

Source from the content-addressed store, hash-verified

63
64
65def _compress_image(image_bytes: bytes, content_type: str, alt_text: str = "") -> dict:
66 """Compress image and return as base64 data URI HTML <img> tag."""
67 global _image_counter
68 _image_counter += 1
69
70 alt_lower = alt_text.lower().strip().replace(" ", "_") if alt_text else ""
71 if alt_lower in SKIP_LOGO_ALTS:
72 return {}
73
74 try:
75 img = Image.open(io.BytesIO(image_bytes))
76 img.load()
77 except Exception:
78 return {}
79
80 if img.width < 10 or img.height < 10:
81 return {}
82
83 try:
84 if img.width > MAX_IMAGE_WIDTH or img.height > MAX_IMAGE_HEIGHT:
85 img.thumbnail((MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT), Image.Resampling.LANCZOS)
86
87 buf = io.BytesIO()
88 if img.mode in ("RGBA", "P"):
89 bg = Image.new("RGB", img.size, (255, 255, 255))
90 bg.paste(img, mask=img.split()[-1] if img.mode == "RGBA" else None)
91 img = bg
92 elif img.mode != "RGB":
93 img = img.convert("RGB")
94
95 img.save(buf, format="JPEG", quality=JPEG_QUALITY, optimize=True)
96 b64 = base64.b64encode(buf.getvalue()).decode("ascii")
97
98 return {"src": f"data:image/jpeg;base64,{b64}"}
99 except Exception:
100 return {}
101
102
103def _mammoth_convert_image(image):

Callers 1

_mammoth_convert_imageFunction · 0.85

Calls 1

saveMethod · 0.80

Tested by

no test coverage detected