-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_maker.py
147 lines (94 loc) · 4.05 KB
/
pdf_maker.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import numpy as np
from glob import glob
import os
import shutil
import logging
from constants import IMAGE_FOLDER, IMAGE_NAME, PROCESSED_IMAGES,PDF_NAME, ROOT, UPLOAD_FOLDER, WATER_MARK_PATH
from helper import checker,number, oneD_to_threeD
import fpdf
from pathlib import Path
import imageio.v3 as iio
import imageio
from PIL import Image
class Pdf_maker:
def __init__(self):
self.errors = []
def take_images(self):
images_folder = IMAGE_FOLDER
cwd = ROOT
all_images = os.path.join(cwd,images_folder)
processed_images = PROCESSED_IMAGES
checker(processed_images)
directory = os.path.join(cwd,processed_images)
paths = os.listdir(all_images)
paths.sort()
print(paths)
for i,path in enumerate(paths):
path = os.path.join(all_images,path)
if path.split('.')[-1] not in ['png', 'jpg', 'jpeg']:
continue
image = iio.imread(path)[:,:,:3]
row,col,_ =image.shape
print(image.shape)
if row < 800 and col < 650:
color = (255,255,255)
pad = np.full((1128,800, 3), color, dtype=np.uint8)
center_x = abs(1128 - row )//2
center_y = abs(800 - col)//2
logging.debug((row,col))
pad[center_x:center_x + row, center_y:center_y + col] = image
image = pad
else:
image = Image.fromarray(image).resize((800, 1128))
extension = path.strip().split('.')[-1]
imageio.imwrite(f'{directory}/{IMAGE_NAME}-{i}.{extension}',image)
print('All images taken successfully')
return directory
def qualityoptimizer(self):
directory = self.take_images()
watermark_path = WATER_MARK_PATH
waterMarkImage = iio.imread(watermark_path)[:,:,0]
waterMarkImage =Image.fromarray(waterMarkImage).resize((800, 1128))
paths = os.listdir(directory)
paths.sort(key = number)
for path in paths:
img_path = os.path.join(directory,path)
raw_image = iio.imread(img_path)[:,:,:3]
gray_mark = 255-np.array(waterMarkImage)
gray_mark =gray_mark//10
gray = oneD_to_threeD(gray_mark)
b,g,r = raw_image[:,:,0],raw_image[:,:,1],raw_image[:,:,2]
red = oneD_to_threeD(r)
blue = oneD_to_threeD(b)
green = oneD_to_threeD(g)
filtered_image = np.where(((red > gray) & (green > gray) & (blue > gray)),raw_image - gray,raw_image)
imageio.imwrite(img_path,filtered_image)
return glob(os.path.join(ROOT,f"{PROCESSED_IMAGES}/*"))
def generate_file_structure(self) -> None:
try:
print('Creating structure ... ')
os.mkdir(f'/{ROOT}/processed_images')
except:
pass
finally:
print('Structure already there')
def pdfmaker(self) -> bool:
try:
self.generate_file_structure()
print('Generating pdf wait for a while')
pdf = fpdf.FPDF()
paths = self.qualityoptimizer()
paths.sort(key = number)
for i in range(len(paths)):
pdf.add_page()
pdf.image(paths[i],0,0,210,290)
path = UPLOAD_FOLDER
pdf.output(f"{path}/{PDF_NAME}","F")
print('Pdf generation done')
return True
except Exception as ex:
print('Sorry Pdf could not be generated')
self.errors.append(ex)
return False
maker = Pdf_maker()
maker.pdfmaker()