Search code examples
pythonimagepathlibdata-preprocessingabbyy

IsADirectoryError: [Errno 21] Is a directory: How to use Pathlib correctly


image_folder = Path("path_to_image_folder")

def get_preprocessed_image(image_path: pathlib.Path) -> PIL.Image.Image:
    with image_path.open("rb") as image_input:
        return PIL.Image.open(io.BytesIO(pyabbyy.preprocess(image_input.read())))


def preprocess_image_folder_(image_folder: pathlib.Path) -> None:
    for image_path in tqdm.tqdm(list(image_folder.rglob("*.pdf"))):
        get_preprocessed_image(image_path).save(image_path)

pyabbyy.initialize_engine(
        "h8KSSy98TGLEint",
        "path_to_activation_token",
        "password",
        "path_to_profile.ini",
)

# Press the green button in the gutter to run the script.
if __name__ == '__main__':

    get_preprocessed_image(image_folder)

What is to be altered here? I want to preprocess folder of images. It is using ABBYY service.


Solution

  • Please include the full traceback of the error, to make it easier to see the issue for us. My guess would be that you are calling get_preprocessed_image on image_folder, but that function expects a path to an image, not a folder. Instead, call the other function:

    if __name__ == '__main__':
    
        preprocess_image_folder_(image_folder)