首页 > 解决方案 > Python OCR 函数减小图像的大小,我该如何解决?

问题描述

我是遍历文件夹并查找 pdf 的路径。然后我将这些 PDF 更改为文本。在通过 OCR 功能传递图像之前,我正在通过转换为灰度进行一些图像处理,并裁剪图像以使某些美学元素不存在。每个 pdf 的第一页与 PDF 的第二页(最后一页)略有不同,因此每个 PDF 页面都通过 if - else 语句进行过滤。

通过 OCR 函数传递第一个 JPEG 可以完美地跨不同的文档,但是每次我通过 OCR 函数传递 JPEG 时,它只会再次传递第一个文档图像。它创建第二个,第三个......但只通过函数传递第一个 jpeg。我整个早上都在尝试调试这个,所以请原谅所有额外的信息。任何帮助,将不胜感激。

以下是使用 OCR 传递函数的结果。

executing first page number loop
(3000, 2064)
(2064, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_1.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_2.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_3.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_4.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_5.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_6.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_7.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_8.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_9.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_10.jpeg
(1714, 2064)
11
0
executing first page number loop
(3000, 2064)
(2064, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_12.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_13.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_14.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_15.jpeg
(1714, 2064)
executing this chunky piece of code
<class 'PIL.PpmImagePlugin.PpmImageFile'>
jpegs/file_16.jpeg
(1714, 2064)
6
0```





              article_number = 0
saved_image_num = 0
text_file = 'txt_files/' + 'article'

print(saved_image_num)


for root, dirs, files in os.walk('articles'):
    for file_ in files:
        if file_.endswith('.pdf'):
            article_path = str(root) + '/' + str(file_)
            pages = convert_from_path(article_path, dpi=300)
            length_of_article = len(pages)
            page_number = 0
            for page in pages:
                if page_number == 0:
                    print('executing first page number loop')
                    name = 'jpegs/file_' + str(saved_image_num) + '.jpeg'
                    page.save(name, 'JPEG')
                    saved_image_num += 1
                    page_number += 1
                    image = image_2_gray(name)
                    print(image.shape)
                    img = crop_page_1(image)
                    print(img.shape)
                    image_ocr(img, text_file + str(article_number) + '.txt')
                    if page_number == length_of_article:
                        article_number += 1
                        print(page_number)
                        page_number = page_number - length_of_article
                        print(page_number)

                elif page_number >= 1:
                    print('executing this chunky piece of code')
                    name_ = 'jpegs/file_' + str(saved_image_num) + '.jpeg'
                    page.save(name_, 'JPEG')
                    print(type(page))
                    saved_image_num += 1
                    page_number += 1
                    print(name_)
                    img1 = crop_page_2_through_end(name_)
                    print(img1.shape)
                    image_ocr2(img1, text_file + str(article_number) + '.txt')
                    if page_number == length_of_article:
                        article_number += 1
                        print(page_number)
                        page_number = page_number - length_of_article
                        print(page_number)

标签: python-3.xocrcv2

解决方案


我的条件一团糟。更改最终 if 条件的顺序。


推荐阅读