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

Added DPI parameter to control PDF resolution. #53

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
change the pdf_images dpi
  • Loading branch information
Embracex1998 committed Sep 20, 2024
commit 5e8fb5bd7c725b63cdc31040deb29fb2b8a2d772
14 changes: 9 additions & 5 deletions gptpdf/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def _parse_rects(page: fitz.Page) -> List[Tuple[float, float, float, float]]:

return [rect.bounds for rect in merged_rects]


def _parse_pdf_to_images(pdf_path: str, output_dir: str = './') -> List[Tuple[str, List[str]]]:
#增加了PDF图片解析的dpi参数,默认值设为300,根据需求设置300-1000可以满足清晰度需求
def _parse_pdf_to_images(pdf_path: str,dpi:int,output_dir: str = './') -> List[Tuple[str, List[str]]]:
"""
Parse PDF to images and save to output_dir.
"""
Expand All @@ -143,11 +143,15 @@ def _parse_pdf_to_images(pdf_path: str, output_dir: str = './') -> List[Tuple[st
logging.info(f'parse page: {page_index}')
rect_images = []
rects = _parse_rects(page)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for index, rect in enumerate(rects):
fitz_rect = fitz.Rect(rect)
# 保存页面为图片
pix = page.get_pixmap(clip=fitz_rect, matrix=fitz.Matrix(4, 4))
pix = page.get_pixmap(clip=fitz_rect, matrix=fitz.Matrix(dpi/72, dpi/72))
name = f'{page_index}_{index}.png'


pix.save(os.path.join(output_dir, name))
rect_images.append(name)
# # 在页面上绘制红色矩形
Expand All @@ -172,7 +176,6 @@ def _parse_pdf_to_images(pdf_path: str, output_dir: str = './') -> List[Tuple[st
pdf_document.close()
return image_infos


def _gpt_parse_images(
image_infos: List[Tuple[str, List[str]]],
prompt_dict: Optional[Dict] = None,
Expand Down Expand Up @@ -249,6 +252,7 @@ def parse_pdf(
model: str = 'gpt-4o',
verbose: bool = False,
gpt_worker: int = 1,
dpi:int = 300,
**args
) -> Tuple[str, List[str]]:
"""
Expand All @@ -257,7 +261,7 @@ def parse_pdf(
if not os.path.exists(output_dir):
os.makedirs(output_dir)

image_infos = _parse_pdf_to_images(pdf_path, output_dir=output_dir)
image_infos = _parse_pdf_to_images(pdf_path, output_dir=output_dir,dpi=dpi)
content = _gpt_parse_images(
image_infos=image_infos,
output_dir=output_dir,
Expand Down
4 changes: 2 additions & 2 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_use_api_key():
api_key = os.getenv('OPENAI_API_KEY')
base_url = os.getenv('OPENAI_API_BASE')
# Manually provide OPENAI_API_KEY and OPEN_API_BASE
content, image_paths = parse_pdf(pdf_path, output_dir=output_dir, api_key=api_key, base_url=base_url, model='gpt-4o', gpt_worker=6)
content, image_paths = parse_pdf(pdf_path, output_dir=output_dir, api_key=api_key, base_url=base_url, model='gpt-4o', gpt_worker=6,dpi=1000)
print(content)
print(image_paths)
# also output_dir/output.md is generated
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_qwen_vl_max():
base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"
# Refer to: https://help.aliyun.com/zh/dashscope/developer-reference/compatibility-of-openai-with-dashscope
model = 'qwen-vl-max'
content, image_paths = parse_pdf(pdf_path, output_dir=output_dir, api_key=api_key, base_url=base_url, model=model, verbose=True, temperature=0.5, max_tokens=1000, top_p=0.9, frequency_penalty=1)
content, image_paths = parse_pdf(pdf_path, output_dir=output_dir, api_key=api_key, base_url=base_url, model=model, verbose=True, temperature=0.5, max_tokens=1000, top_p=0.9, frequency_penalty=1,dpi=1000)
print(content)
print(image_paths)

Expand Down