当我运行 extract_img.py 文件时出现此错误:
\n\n\n运行时错误(f\xe2\x80\x9c目录'{目录}'不存在\xe2\x80\x9d)
\n
\n运行时错误:导入fitz时目录'static/'不存在
我不明白为什么这会向我发回此错误消息。\n我之前看到过处理此主题的对话,但我不明白解决方案。你可以帮帮我吗?
\n此文件旨在从 PDF 文件中提取某些图像(某些文件中的图像)。
\nfrom os import chdir\nimport shutil, os\nimport io\nfrom PIL import Image\nimport fitz\nfrom unif_noun import unif_noun #other file python for change file noun.\n\ndef execute_func(rootdir):\nfor subdir, dirs, files in os.walk(rootdir):\n for file in files:\n filepath = subdir + os.sep + file\n if filepath.endswith(".pdf"):\n #extract(f"{filepath}")\n # open the file\n pdf_file = fitz.open(file)\n images = list()\n for page_index in range(len(pdf_file)):\n # get the page itself\n page = pdf_file[page_index]\n image_list = page.getImageList()\n # printing number of images found in this page\n # if image_list:\n # print(f"[+] Found a total of {len(image_list)} images in page {page_index}")\n # else:\n # print("[!] No images found on page", page_index)\n for image_index, img in enumerate(page.getImageList(), start=1):\n images.append(img[0])\n for i, xref in enumerate(images, start=1):\n if 1 < i < len(images) - 3:\n # extract the image bytes\n base_image = pdf_file.extractImage(xref)\n image_bytes = base_image["image"]\n # get the image extension\n image_ext = base_image["ext"]\n # load it to PIL\n image = Image.open(io.BytesIO(image_bytes))\n # save it to local disk\n image.save(open(f"{unif_noun(file)}.{image_ext}", "wb"))\n # D\xc3\xa9placer un fichier du r\xc3\xa9pertoire\n for subdir, dirs, files in os.walk(rootdir):\n for f in files:\n source = subdir\n destination = 'C:/Users/.../VS Projects/img'\n filename = os.path.basename(source)\n dest = os.path.join(destination,filename)\n shutil.move(source + f"{unif_noun(file)}.{image_ext}", dest)\nexecute_func(r'C:/Users/Factoryz Amandine/OneDrive/Bureau/Python/CCOR02752150_3.pdf')[enter image description here][1]\nRun Code Online (Sandbox Code Playgroud)\n
thr*_*dhn 47
卸载现有的 fitz 模块/包
pip uninstall fitz
Run Code Online (Sandbox Code Playgroud)
安装 pymupdf 模块
pip install pymupdf
Run Code Online (Sandbox Code Playgroud)
Raf*_*ek 3
我遇到了同样的问题并PyMuPDF为我安装修复了它。(我猜这不是第一次安装PyMuPDF也用fitz解决其他问题)
pip3 install PyMuPDF
Run Code Online (Sandbox Code Playgroud)