Created
June 10, 2022 09:09
-
-
Save HoloborodkoBohdan/91539103a5e2efb2eb0c07023fd5d496 to your computer and use it in GitHub Desktop.
Convert pdf to jpeg from folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import tempfile | |
from pdf2image import convert_from_path | |
folder = 'pdf' # older with pdfs. You need to create it manually. | |
save_dir = 'saved' # folder dor images. You need to create it manually. | |
log = open("log.txt", "a") | |
for r, d, f in os.walk(folder): | |
for filename in f: | |
print(f'./{folder}/{filename}') | |
log.write(filename + "\n") | |
with tempfile.TemporaryDirectory() as path: | |
images_from_path = convert_from_path(f'./{folder}/{filename}', dpi=200, output_folder=path) | |
base_filename = os.path.splitext(os.path.basename(filename))[0] + '.jpg' | |
for page in images_from_path: | |
index = images_from_path.index(page) | |
name = f'{base_filename}_{index}.jpg' | |
print(name) | |
page.save(os.path.join(save_dir, name), 'JPEG') | |
log.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment