Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to process jpg file, instead of pdf #42

Open
wei-ma opened this issue Feb 27, 2025 · 1 comment
Open

how to process jpg file, instead of pdf #42

wei-ma opened this issue Feb 27, 2025 · 1 comment

Comments

@wei-ma
Copy link

wei-ma commented Feb 27, 2025

if request input is jpg, png,?

@jakep-allenai
Copy link
Collaborator

We use the following code in the web demo, sorry it's not in the repo yet, we should add it in v2.

import os
import tempfile


async def convert_image_to_pdf(png_file: tempfile.NamedTemporaryFile) -> tempfile.NamedTemporaryFile:
    import img2pdf

    new_file = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
    new_file.write(img2pdf.convert([png_file.name]))
    new_file.close()

    os.remove(png_file.name)

    return new_file


def is_png(file_path):
    try:
        with open(file_path, "rb") as f:
            header = f.read(8)
            return header == b"\x89PNG\r\n\x1a\n"
    except Exception as e:
        print(f"Error: {e}")
        return False


def is_jpeg(file_path):
    try:
        with open(file_path, 'rb') as f:
            header = f.read(2)
            return header == b'\xff\xd8'
    except Exception as e:
        print(f"Error: {e}")
        return False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants