I'd recently found one issuse from converting PDF into JPG file and It will be stopped at File Number 52 without continuous on.
I'd tried neither issues on PdfReader from causing this problem. Hope anyone could help. Thanks
I'd tried neither issues on PdfReader from causing this problem. Hope anyone could help. Thanks
#!/usr/bin/python
import os
import base64
import warnings
import pandas as pd
import pypdfium2 as pdfium
from pypdf import PdfReader
from PIL import Image
def into_jpg(file, file_loc):
try:
write_loc = '/home/pi/code/images/train'
new_file_name = str(base64.b64encode(str.encode(file.split('.')[0]))[:15], 'utf-8')
new_file_loc = os.path.join(write_loc, new_file_name + '.jpg')
new_count = 1
pdfs = pdfium.PdfDocument(file_loc)
n_pages = len(pdfs)
for page_num in range(len(pdfs)):
page = pdfs.get_page(page_num)
pil_image = page.render(
scale = 1,
rotation = 0,
crop = (0, 0, 0, 0)
#optimise_mode = pdfium.OptimiseMode.NONE
).to_pil()
pil_image.save(os.path.join(write_loc, f'{new_file_name}_{page_num+1}.jpg'))
new_file_loc = os.path.join(write_loc, new_file_name + '_' + str(page_num) + '.jpg')
except Exception as exp:
print(exp)
return
file_dir = '/home/pi/code/ma_1'
file_loc = None
count = 1
for file in os.listdir(file_dir):
if os.path.isdir(os.path.join(file_dir, file)):
for file_ in os.listdir(os.path.join(file_dir, file)):
file_loc = os.path.join(os.path.join(file_dir, file), file_)
IsProcess = True
while IsProcess:
if os.path.isfile(file_loc):
try:
PdfReader(file_loc)
print('File Location_{0}:'.format(count), file_loc)
into_jpg(file_, file_loc)
IsProcess = False
except Exception as exp:
print(exp)
count = count + 1
