[Bug]: PDF images saved with save_images_locally=True come back with an empty src in result.media

未关闭 适合新手
#2,469 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

从 PDF 处理器中的 PDFContentScrapingStrategy.scrap() 开始,检查已保存的图像字典如何传入 ScrapingResult(media={"images": [...]});然后查看 MediaItem schema,了解它保留了哪些字段。使用 issue 中的 Pillow PDF 示例复现。当 result.media.images 中的每张已保存图像都能通过某种方式定位到 image_save_dir 下的文件,同时内联图像行为保持不变时,即为完成。

由索引模型根据 Issue 内容生成。

描述

crawl4ai version

0.9.5 (develop at 78405d6)

Expected Behavior

With PDFContentScrapingStrategy(extract_images=True, save_images_locally=True), each entry in result.media.images should point at the image file that was written to image_save_dir.

Current Behavior

The image is written to disk, but the MediaItem in result.media.images has src == "" and data == "", so there is no way to find the file from the result.

The PDF processor reports a saved image as {"path": ..., "format": ..., "width": ..., ...}. scrap() passes these dicts straight into ScrapingResult(media={"images": [...]}), and MediaItem has no path field, so pydantic drops it. Same kind of schema mismatch as #2374 for links. Inline images (save_images_locally=False) are fine because data is a MediaItem field.

Is this reproducible?

Yes

Inputs Causing the Bug
Any PDF with an embedded image, e.g. one made by Pillow (below).
Steps to Reproduce
1. Make a one-page PDF containing an image.
2. Scrape it with PDFContentScrapingStrategy(extract_images=True, save_images_locally=True, image_save_dir=...).
3. Look at result.media.images.
Code snippets
import os, tempfile
from PIL import Image
from crawl4ai.processors.pdf import PDFContentScrapingStrategy

d = tempfile.mkdtemp()
pdf = os.path.join(d, "doc.pdf")
Image.new("RGB", (40, 30), "red").save(pdf)

strategy = PDFContentScrapingStrategy(
    extract_images=True, save_images_locally=True, image_save_dir=os.path.join(d, "imgs")
)
result = strategy.scrap(pdf, "")
print(os.listdir(os.path.join(d, "imgs")))
print(result.media.images[0].model_dump())

Output:

['page_1_img_1.jpg']
{'src': '', 'data': '', 'alt': '', 'desc': '', 'score': 0, 'type': 'image', 'group_id': 0, 'format': 'jpeg', 'width': 40}
OS

Linux

Python version

3.12

Browser

N/A (PDF scraping strategy called directly)

Browser version

N/A

Error logs & Screenshots (if applicable)

No error; success is True.

主要语言
Python
星标
86.4k
派生
9k
平均合并
2 天 12 小时
30 天内合并 PR
21

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

unclecode/crawl4ai 的其他 Issue

查看 unclecode/crawl4ai 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。