Skip to content

Commit

Permalink
Configurable image output type
Browse files Browse the repository at this point in the history
  • Loading branch information
VikParuchuri committed Dec 3, 2024
1 parent 619f5b0 commit ee96b54
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion marker/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def save_output(rendered: BaseModel, output_dir: str, fname_base: str):
f.write(json.dumps(rendered.metadata, indent=2))

for img_name, img in images.items():
img.save(os.path.join(output_dir, img_name), "PNG", optimize=False, compress_level=3)
img.save(os.path.join(output_dir, img_name), settings.OUTPUT_IMAGE_FORMAT)
2 changes: 1 addition & 1 deletion marker/renderers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def extract_image(document: Document, image_id, to_base64=False):
cropped = page_img.crop(image_box.bbox)
if to_base64:
image_buffer = io.BytesIO()
cropped.save(image_buffer, format='PNG')
cropped.save(image_buffer, format=settings.OUTPUT_IMAGE_FORMAT)
cropped = base64.b64encode(image_buffer.getvalue()).decode(settings.OUTPUT_ENCODING)
return cropped

Expand Down
5 changes: 4 additions & 1 deletion marker/renderers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

# Ignore beautifulsoup warnings
import warnings

from marker.settings import settings

warnings.filterwarnings("ignore", category=MarkupResemblesLocatorWarning)


Expand Down Expand Up @@ -53,7 +56,7 @@ def extract_html(self, document, document_output, level=0):
elif ref_block_id.block_type in self.image_blocks:
if self.extract_images:
image = self.extract_image(document, ref_block_id)
image_name = f"{ref_block_id.to_path()}.png"
image_name = f"{ref_block_id.to_path()}.{settings.OUTPUT_IMAGE_FORMAT.lower()}"
images[image_name] = image
ref.replace_with(BeautifulSoup(f"<p><img src='{image_name}'></p>", 'html.parser'))
else:
Expand Down
1 change: 1 addition & 0 deletions marker/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Settings(BaseSettings):

# General
OUTPUT_ENCODING: str = "utf-8"
OUTPUT_IMAGE_FORMAT: str = "JPEG"

# General models
TORCH_DEVICE: Optional[str] = None # Note: MPS device does not work for text detection, and will default to CPU
Expand Down
4 changes: 2 additions & 2 deletions marker_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def open_pdf(pdf_file):

def img_to_html(img, img_alt):
img_bytes = io.BytesIO()
img.save(img_bytes, format="PNG")
img.save(img_bytes, format=settings.OUTPUT_IMAGE_FORMAT)
img_bytes = img_bytes.getvalue()
encoded = base64.b64encode(img_bytes).decode()
img_html = f'<img src="data:image/png;base64,{encoded}" alt="{img_alt}" style="max-width: 100%;">'
img_html = f'<img src="data:image/{settings.OUTPUT_IMAGE_FORMAT.lower()};base64,{encoded}" alt="{img_alt}" style="max-width: 100%;">'
return img_html


Expand Down
2 changes: 1 addition & 1 deletion marker_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async def _convert_pdf(params: CommonParams):
encoded = {}
for k, v in images.items():
byte_stream = io.BytesIO()
v.save(byte_stream, format="PNG")
v.save(byte_stream, format=settings.OUTPUT_IMAGE_FORMAT)
encoded[k] = base64.b64encode(byte_stream.getvalue()).decode(settings.OUTPUT_ENCODING)

return {
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "marker-pdf"
version = "1.0.1"
version = "1.0.2"
description = "Convert PDF to markdown with high speed and accuracy."
authors = ["Vik Paruchuri <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit ee96b54

Please sign in to comment.