-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf2img.py
58 lines (47 loc) · 1.85 KB
/
pdf2img.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
import io
import os
import random
import cv2
from shutil import copyfile
from PIL import Image
from pdf2image import convert_from_path, convert_from_bytes
input_folder = ''
output_folder = ''
img_format = 'png'
pdf_list = []
def record(folder):
# print(os.listdir(folder))
# global folderin
for name in os.listdir(folder):
if os.path.isdir(folder+'/'+name):
record(folder+'/'+name)
elif name.endswith('pdf'):
# file_name = os.path.join(output_folder, os.path.relpath(os.path.join(folder, name), folderin))
# path_out_list.append(file_name)
pdf_list.append('{}'.format(folder+'/'+name))
record(input_folder)
path_out_list = []
for f in pdf_list:
file_name = output_folder+'/'+os.path.relpath(f, input_folder)
# fname,ext = os.path.splitext(fullflname)
# file_name = os.path.join(filepath, fname + '_'+str(i)+'.'+oext)
path_out_list.append(file_name)
filepath,fullflname = os.path.split(file_name)
# print(path_out_list)
# fname,ext = os.path.splitext(fullflname)
# file_name = os.path.join(filepath, fname + '_'+str(i)+'.'+oext)
if not os.path.exists(filepath):
os.makedirs(filepath)
for idx, pdfname in enumerate(pdf_list):
print(pdfname)
filepath,fullflname = os.path.split(path_out_list[idx])
fname, ext = os.path.splitext(fullflname)
# images = convert_from_path(pdfname, dpi=200, output_folder=filepath, fmt='png')
images = convert_from_path(pdfname, dpi=50, output_folder=filepath, output_file=fname, fmt=img_format)
save_name = os.path.join(filepath, fname+'0001-1.'+img_format)
change_name = os.path.join(filepath, fname+'.'+img_format)
os.system('mv %s %s' % (save_name, change_name))
# print(images.shape)
# print(images.shape)
# cv2.imwrite(,images,[cv2.IMWRITE_JPEG_QUALITY,70])