raise RUNTIMERROR(f“目录“{Directory}”不存在”)RUNTIMERROR:导入fitz中不存在目录“static/”

2024-04-25 17:50:04 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行extract_img.py文件时,出现此错误:

RuntimeError(f“Directory '{directory}' does not exist”)
RuntimeError: Directory 'static/' does not exist from import fitz

我不明白为什么这会将此错误消息发回给我。 我以前看过关于这个话题的对话,但我不理解解决方案。你能帮我吗

此文件用于从PDF文件中提取某些图像文件中的某些图像

from os import chdir
import shutil, os
import io
from PIL import Image
import fitz
from unif_noun import unif_noun #other file python for change file noun.

def execute_func(rootdir):
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        filepath = subdir + os.sep + file
        if filepath.endswith(".pdf"):
            #extract(f"{filepath}")
            # open the file
            pdf_file = fitz.open(file)
            images = list()
            for page_index in range(len(pdf_file)):
                # get the page itself
                page = pdf_file[page_index]
                image_list = page.getImageList()
                # printing number of images found in this page
                # if image_list:
                #     print(f"[+] Found a total of {len(image_list)} images in page {page_index}")
                # else:
                #     print("[!] No images found on page", page_index)
                for image_index, img in enumerate(page.getImageList(), start=1):
                    images.append(img[0])
            for i, xref in enumerate(images, start=1):
                if 1 < i < len(images) - 3:
                    # extract the image bytes
                    base_image = pdf_file.extractImage(xref)
                    image_bytes = base_image["image"]
                    # get the image extension
                    image_ext = base_image["ext"]
                    # load it to PIL
                    image = Image.open(io.BytesIO(image_bytes))
                    # save it to local disk
                    image.save(open(f"{unif_noun(file)}.{image_ext}", "wb"))
                    # Déplacer un fichier du répertoire
                    for subdir, dirs, files in os.walk(rootdir):
                        for f in files:
                            source = subdir
                            destination = 'C:/Users/.../VS Projects/img'
                            filename = os.path.basename(source)
                            dest = os.path.join(destination,filename)
                            shutil.move(source + f"{unif_noun(file)}.{image_ext}", dest)
execute_func(r'C:/Users/Factoryz Amandine/OneDrive/Bureau/Python/CCOR02752150_3.pdf')[enter image description here][1]

Tags: infromimageimportimgforindexpdf