File size: 3,445 Bytes
f8afc9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e709d2a
 
f8afc9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fitz
from PIL import Image

class doc_processing:

    def __init__(self, name, id_type, doc_type, f_path):
 
        self.name = name
        self.id_type = id_type
        self.doc_type = doc_type
        self.f_path = f_path
        # self.o_path = o_path
        
        
    def pdf_to_image_scale(self):
        pdf_document = fitz.open(self.f_path)
        if self.id_type == "gst":
            page_num = 2
        else:
            page_num = 0
        
        page = pdf_document.load_page(page_num)
        pix = page.get_pixmap()  # Render page as a pixmap (image)
        
        # Convert pixmap to PIL Image
        image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
        
        original_width, original_height = image.size
        
        print("original_width",original_width)
        print("original_height",original_height)


        new_width = (1000 / original_width) * original_width
        new_height = (1000 / original_height) * original_height
        
        print("new_width",new_width)
        print("new_height",new_height)
        # new_width = 
        # new_height =  
        image.resize((int(new_width), int(new_height)), Image.Resampling.LANCZOS)
        output_path = "processed_images/{}/{}.jpeg".format(self.id_type,self.name)
        image.save(output_path)
        return  {"success":200,"output_p":output_path}
    

    def scale_img(self):
        

        print("path of file",self.f_path)
        image = Image.open(self.f_path).convert("RGB")
        original_width, original_height = image.size
        
        print("original_width",original_width)
        print("original_height",original_height)


        new_width = (1000 / original_width) * original_width
        new_height = (1000 / original_height) * original_height
        
        print("new_width",new_width)
        print("new_height",new_height)
        # new_width = 
        # new_height =  
        image.resize((int(new_width), int(new_height)), Image.Resampling.LANCZOS)
        output_path = "processed_images/{}/{}.jpeg".format(self.id_type,self.name)
        image.save(output_path)
        return {"success":200,"output_p":output_path}

    def process(self):
        if self.doc_type == "pdf":
            response = self.pdf_to_image_scale()
        else:
            response = self.scale_img()

        return response




        
# files = {
#     "aadhar_file": "/home/javmulla/model_one/test_images_aadhar/test_two.jpg",
#     "pan_file": "/home/javmulla/model_one/test_images_pan/6ea33087.jpeg", 
#     "cheque_file": "/home/javmulla/model_one/test_images_cheque/0f81678a.jpeg",
#     "gst_file": "/home/javmulla/model_one/test_images_gst/0a52fbcb_page3_image_0.jpg"
# }


# files = {
#     "aadhar_file": "/home/javmulla/model_one/test_images_aadhar/test_two.jpg",
#     "pan_file": "/home/javmulla/model_one/test_images_pan/6ea33087.jpeg", 
#     "cheque_file": "/home/javmulla/model_one/test_images_cheque/0f81678a.jpeg",
#     "gst_file": "test_Images_folder/gst/e.pdf"
# }

# for key, value in files.items():
#     name = value.split("/")[-1].split(".")[0]
#     id_type = key.split("_")[0]
#     doc_type = value.split("/")[-1].split(".")[1]
#     f_path = value
#     preprocessing = doc_processing(name,id_type,doc_type,f_path)
#     response = preprocessing.process()
#     print("response",response)
    
    
    
    
    
    # id_type, doc_type, f_path