Marthee commited on
Commit
9e4e7cd
·
verified ·
1 Parent(s): 9df745c

Update pixelconversion.py

Browse files
Files changed (1) hide show
  1. pixelconversion.py +239 -238
pixelconversion.py CHANGED
@@ -1,239 +1,240 @@
1
- # -*- coding: utf-8 -*-
2
- """pixelconversion.ipynb
3
-
4
- Automatically generated by Colaboratory.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1hfdgkYOw8w6DdJqsZx8txmw8INUGxesl
8
- """
9
-
10
-
11
- # pip install pymupdf -q
12
-
13
- """### Imports"""
14
-
15
- import fitz
16
- from PIL import Image
17
- import numpy as np
18
- import cv2
19
- import db
20
- import tsadropboxretrieval
21
- import fitz
22
- import math
23
- from io import BytesIO
24
-
25
- ################################################
26
- ### Check if page is visually rotated or not
27
-
28
- def is_content_rotated(docpage):
29
- docpage.draw_rect([250,250,docpage.mediabox.width-500,docpage.mediabox.height-500], color = (0,0,0), width = 1,fill=(0,0,0),fill_opacity=1)
30
- pix = docpage.get_pixmap()
31
- image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
32
- img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
33
- kernel = np.ones((3,3),np.uint8)
34
- img = cv2.dilate(img,kernel, iterations=6)
35
- img=cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV)[1]
36
- # cv2_imshow(img)
37
- contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
38
- # Check the orientation of page through large shape drawn
39
- for contour in contours:
40
- rect = cv2.minAreaRect(contour)
41
- angle = rect[-1]
42
- # print(angle)
43
- if angle != 0.0 and angle != 90.0 and angle != 180.0 and angle!=-90.0 and angle!=-180.0:
44
- print("Page content appears visually rotated.")
45
- return True
46
- print("Page content does not appear visually rotated.")
47
- return False
48
-
49
- # def drawisrotated(data=0,pdf_content=0,dpi=0):
50
- # if pdf_content:
51
- # docPath = fitz.open(stream=pdf_content, filetype="pdf")
52
- # else:
53
- # docPath = fitz.open("pdf",data) #dropbox path
54
- # pageDocPath=docPath[0]
55
-
56
- # bool_T_F=is_content_rotated(pageDocPath)
57
-
58
- # if bool_T_F==True:
59
- # if pdf_content:
60
- # docPath = fitz.open(stream=pdf_content, filetype="pdf")
61
- # else:
62
- # docPath = fitz.open("pdf",data) #dropbox path
63
- # pageDocPath=docPath[0]
64
- # pageDocPath.draw_rect([250,250,pageDocPath.mediabox.width-500,pageDocPath.mediabox.height-500], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9)
65
- # if dpi:
66
- # pix=pageDocPath.get_pixmap(dpi=dpi)
67
-
68
- # newwidth_ratio=pix.width/pageDocPath.mediabox.width
69
- # newheight_ratio=pix.height/pageDocPath.mediabox.height
70
- # else:
71
- # newwidth_ratio=1
72
- # newheight_ratio=1
73
- # area=((pageDocPath.mediabox.height-500-250)*newheight_ratio) * ((pageDocPath.mediabox.width-500-250)*newwidth_ratio)
74
- # perimeter=(((pageDocPath.mediabox.width-500-250)*newwidth_ratio) *2) + ( ((pageDocPath.mediabox.height-500-250)*newheight_ratio) *2)
75
-
76
- # if pageDocPath.mediabox.height >pageDocPath.mediabox.width:
77
- # rectText=fitz.Rect(300, 200, (pageDocPath.mediabox.width-80),( pageDocPath.mediabox.width+80) )
78
- # else:
79
- # rectText=fitz.Rect(500, 200, (pageDocPath.mediabox.height-80),( pageDocPath.mediabox.height+80) )
80
-
81
- # text = """Scale Document"""
82
- # annot1=pageDocPath.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1,1,1), text_color=(1,1,1), rotate= pageDocPath.rotation, align=1)
83
- # annot1.update()
84
- # # docPath.save('drawnOn.pdf')
85
- # return docPath,area,perimeter
86
- # else:
87
- # if pdf_content:
88
- # if dpi:
89
- # doc,area,perimeter=openDrawPDF(pdf_content=pdf_content,dpi=dpi)
90
- # return doc,area,perimeter
91
- # else:
92
- # doc,area,perimeter=openDrawPDF(pdf_content=pdf_content)
93
- # return doc,area,perimeter
94
- # else:
95
- # if dpi:
96
- # doc,area,perimeter=openDrawPDF(data,dpi=dpi)
97
- # return doc,area,perimeter
98
- # else:
99
- # doc,area,perimeter=openDrawPDF(data)
100
- # return doc,area,perimeter
101
-
102
-
103
-
104
- def drawisrotated(data=0,pdf_content=0,dpi=0):
105
- if pdf_content:
106
- docPath = fitz.open(stream=pdf_content, filetype="pdf")
107
- else:
108
- docPath = fitz.open("pdf",data) #dropbox path
109
- if pdf_content:
110
- if dpi:
111
- doc,area,perimeter=openDrawPDF(pdf_content=pdf_content,dpi=dpi)
112
- return doc,area,perimeter
113
- else:
114
- doc,area,perimeter=openDrawPDF(pdf_content=pdf_content)
115
- return doc,area,perimeter
116
- else:
117
- if dpi:
118
- doc,area,perimeter=openDrawPDF(data,dpi=dpi)
119
- return doc,area,perimeter
120
- else:
121
- doc,area,perimeter=openDrawPDF(data)
122
- return doc,area,perimeter
123
-
124
-
125
-
126
- # def openDrawPDF(data=0,pdf_content=0,dpi=0):
127
-
128
- # if pdf_content:
129
- # doc = fitz.open(stream=pdf_content, filetype="pdf")
130
- # else:
131
- # doc = fitz.open("pdf",data) #dropbox path
132
- # page = doc[0]
133
- # print(page.rotation)
134
-
135
- # if page.rect.height > page.rect.width:
136
- # rectText = fitz.Rect(300, 200, (page.mediabox.width - 80), (page.mediabox.width + 80))
137
- # else:
138
- # rectText = fitz.Rect(500, 200, (page.mediabox.height - 80), (page.mediabox.height + 80))
139
- # page.draw_rect([0 + 10, 0 + 10, page.mediabox.width - 10, page.mediabox.height - 10],color=(75 / 255, 0, 130 / 255), width=1, fill=(75 / 255, 0, 130 / 255), fill_opacity=0.9)
140
-
141
- # if dpi:
142
- # pix = page.get_pixmap(dpi=dpi)
143
- # newwidth_ratio = pix.width / page.mediabox.width
144
- # newheight_ratio = pix.height / page.mediabox.height
145
- # else:
146
- # print('No DPI specified')
147
- # newwidth_ratio = 1
148
- # newheight_ratio = 1
149
-
150
- # # Apply the transformation to the rectangle's area and perimeter
151
- # area = ((page.mediabox.width - 10 - 10) * newwidth_ratio) * ((page.mediabox.height - 10 - 10) * newheight_ratio)
152
- # perimeter = (((page.mediabox.width - 10 - 10) * 2) * newwidth_ratio) + (((page.mediabox.height - 10 - 10) * 2) * newheight_ratio)
153
-
154
- # # Apply the transformation to the text annotation (shift and scale its position)
155
- # text = """Scale Document"""
156
- # annot1 = page.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1, 1, 1),text_color=(1, 1, 1), rotate=page.rotation, align=1)
157
- # annot1.update()
158
- # # print('fitzzzzzzzzzzzz',fitz.__version__)
159
- # # doc.save('drawnOn_transformed.pdf')
160
- # return doc, area, perimeter
161
-
162
- def openDrawPDF(data=0,pdf_content=0,dpi=0):
163
- # Open the document
164
- # doc = fitz.open(pdfOutput) # Dropbox
165
- if pdf_content:
166
- doc = fitz.open(stream=pdf_content, filetype="pdf")
167
- else:
168
- doc = fitz.open("pdf",data) #dropbox path
169
- page = doc[0]
170
- derotationMatrix=page.derotation_matrix
171
- print(page.rotation)
172
-
173
- if page.rect.height > page.rect.width:
174
- rectText = fitz.Rect(300, 200, (page.mediabox.width - 80), (page.mediabox.width + 80))
175
- else:
176
- rectText = fitz.Rect(500, 200, (page.mediabox.height - 80), (page.mediabox.height + 80))
177
-
178
- if dpi:
179
- pix = page.get_pixmap(dpi=dpi)
180
- newwidth_ratio = pix.width / page.mediabox.width
181
- newheight_ratio = pix.height / page.mediabox.height
182
- else:
183
- print('No DPI specified')
184
- newwidth_ratio = 1
185
- newheight_ratio = 1
186
-
187
- # Apply the transformation to the rectangle's area and perimeter
188
- area = ((page.mediabox.width) * newwidth_ratio) * ((page.mediabox.height ) * newheight_ratio)
189
- perimeter = (((page.mediabox.width ) * 2) * newwidth_ratio) + (((page.mediabox.height) * 2) * newheight_ratio)
190
- # full page rectangle
191
-
192
- # Original diagonal points
193
- startRect = fitz.Point(0, 0)
194
- endRect = fitz.Point(page.mediabox.width, page.mediabox.height)
195
-
196
- # Apply derotation if needed
197
- # startRect = startRect * derotationMatrix
198
- # endRect = endRect * derotationMatrix
199
-
200
- # Create rectangle corners from diagonal points
201
- top_left = fitz.Point(startRect.x, startRect.y)
202
- top_right = fitz.Point(endRect.x, startRect.y)
203
- bottom_right = fitz.Point(endRect.x, endRect.y)
204
- bottom_left = fitz.Point(startRect.x, endRect.y)
205
-
206
- # List of points for polyline (closing the rectangle)
207
- rect_points = [top_left, top_right, bottom_right, bottom_left, top_left]
208
-
209
- # Add polyline annotation
210
- annot = page.add_polyline_annot(rect_points)
211
- annot.set_colors(stroke=(75/255, 0, 130/255), fill=(75/255, 0, 130/255))
212
- annot.set_opacity(0.9)
213
- annot.set_border(width=20)
214
- annot.set_info(content=str(perimeter)+' m', subject='Perimeter Measurement', title="ADR Team")
215
- annot.update()
216
-
217
- page_width = page.rect.width
218
- page_height = page.rect.height
219
- startLength = fitz.Point(0, page_height / 2) *derotationMatrix # left side
220
- endLength = fitz.Point(page_width, page_height / 2) *derotationMatrix # right side
221
-
222
- print(f"Line length: {length} points")
223
- # startLength = startLength * derotationMatrix
224
- # endLength = endLength * derotationMatrix
225
-
226
- shapeLength=[startLength, endLength]
227
- line_annot = page.add_polyline_annot(shapeLength)
228
- line_annot.set_colors(stroke=(75/255, 0, 130/255)) # red line
229
- line_annot.set_border(width=10)
230
- line_annot.set_info(content=str(length)+' m',subject='Perimeter Measurement', title="ADR Team")
231
- line_annot.update()
232
-
233
- # Apply the transformation to the text annotation (shift and scale its position)
234
- text = """Scale Document"""
235
- annot1 = page.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1, 1, 1),text_color=(1, 1, 1), rotate=page.rotation, align=1)
236
- annot1.update()
237
-
238
- # doc.save('drawnOn_transformed.pdf')
 
239
  return doc, area, perimeter
 
1
+ # -*- coding: utf-8 -*-
2
+ """pixelconversion.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1hfdgkYOw8w6DdJqsZx8txmw8INUGxesl
8
+ """
9
+
10
+
11
+ # pip install pymupdf -q
12
+
13
+ """### Imports"""
14
+
15
+ import fitz
16
+ from PIL import Image
17
+ import numpy as np
18
+ import cv2
19
+ import db
20
+ import tsadropboxretrieval
21
+ import fitz
22
+ import math
23
+ from io import BytesIO
24
+
25
+ ################################################
26
+ ### Check if page is visually rotated or not
27
+
28
+ def is_content_rotated(docpage):
29
+ docpage.draw_rect([250,250,docpage.mediabox.width-500,docpage.mediabox.height-500], color = (0,0,0), width = 1,fill=(0,0,0),fill_opacity=1)
30
+ pix = docpage.get_pixmap()
31
+ image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
32
+ img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
33
+ kernel = np.ones((3,3),np.uint8)
34
+ img = cv2.dilate(img,kernel, iterations=6)
35
+ img=cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV)[1]
36
+ # cv2_imshow(img)
37
+ contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
38
+ # Check the orientation of page through large shape drawn
39
+ for contour in contours:
40
+ rect = cv2.minAreaRect(contour)
41
+ angle = rect[-1]
42
+ # print(angle)
43
+ if angle != 0.0 and angle != 90.0 and angle != 180.0 and angle!=-90.0 and angle!=-180.0:
44
+ print("Page content appears visually rotated.")
45
+ return True
46
+ print("Page content does not appear visually rotated.")
47
+ return False
48
+
49
+ # def drawisrotated(data=0,pdf_content=0,dpi=0):
50
+ # if pdf_content:
51
+ # docPath = fitz.open(stream=pdf_content, filetype="pdf")
52
+ # else:
53
+ # docPath = fitz.open("pdf",data) #dropbox path
54
+ # pageDocPath=docPath[0]
55
+
56
+ # bool_T_F=is_content_rotated(pageDocPath)
57
+
58
+ # if bool_T_F==True:
59
+ # if pdf_content:
60
+ # docPath = fitz.open(stream=pdf_content, filetype="pdf")
61
+ # else:
62
+ # docPath = fitz.open("pdf",data) #dropbox path
63
+ # pageDocPath=docPath[0]
64
+ # pageDocPath.draw_rect([250,250,pageDocPath.mediabox.width-500,pageDocPath.mediabox.height-500], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9)
65
+ # if dpi:
66
+ # pix=pageDocPath.get_pixmap(dpi=dpi)
67
+
68
+ # newwidth_ratio=pix.width/pageDocPath.mediabox.width
69
+ # newheight_ratio=pix.height/pageDocPath.mediabox.height
70
+ # else:
71
+ # newwidth_ratio=1
72
+ # newheight_ratio=1
73
+ # area=((pageDocPath.mediabox.height-500-250)*newheight_ratio) * ((pageDocPath.mediabox.width-500-250)*newwidth_ratio)
74
+ # perimeter=(((pageDocPath.mediabox.width-500-250)*newwidth_ratio) *2) + ( ((pageDocPath.mediabox.height-500-250)*newheight_ratio) *2)
75
+
76
+ # if pageDocPath.mediabox.height >pageDocPath.mediabox.width:
77
+ # rectText=fitz.Rect(300, 200, (pageDocPath.mediabox.width-80),( pageDocPath.mediabox.width+80) )
78
+ # else:
79
+ # rectText=fitz.Rect(500, 200, (pageDocPath.mediabox.height-80),( pageDocPath.mediabox.height+80) )
80
+
81
+ # text = """Scale Document"""
82
+ # annot1=pageDocPath.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1,1,1), text_color=(1,1,1), rotate= pageDocPath.rotation, align=1)
83
+ # annot1.update()
84
+ # # docPath.save('drawnOn.pdf')
85
+ # return docPath,area,perimeter
86
+ # else:
87
+ # if pdf_content:
88
+ # if dpi:
89
+ # doc,area,perimeter=openDrawPDF(pdf_content=pdf_content,dpi=dpi)
90
+ # return doc,area,perimeter
91
+ # else:
92
+ # doc,area,perimeter=openDrawPDF(pdf_content=pdf_content)
93
+ # return doc,area,perimeter
94
+ # else:
95
+ # if dpi:
96
+ # doc,area,perimeter=openDrawPDF(data,dpi=dpi)
97
+ # return doc,area,perimeter
98
+ # else:
99
+ # doc,area,perimeter=openDrawPDF(data)
100
+ # return doc,area,perimeter
101
+
102
+
103
+
104
+ def drawisrotated(data=0,pdf_content=0,dpi=0):
105
+ if pdf_content:
106
+ docPath = fitz.open(stream=pdf_content, filetype="pdf")
107
+ else:
108
+ docPath = fitz.open("pdf",data) #dropbox path
109
+ if pdf_content:
110
+ if dpi:
111
+ doc,area,perimeter=openDrawPDF(pdf_content=pdf_content,dpi=dpi)
112
+ return doc,area,perimeter
113
+ else:
114
+ doc,area,perimeter=openDrawPDF(pdf_content=pdf_content)
115
+ return doc,area,perimeter
116
+ else:
117
+ if dpi:
118
+ doc,area,perimeter=openDrawPDF(data,dpi=dpi)
119
+ return doc,area,perimeter
120
+ else:
121
+ doc,area,perimeter=openDrawPDF(data)
122
+ return doc,area,perimeter
123
+
124
+
125
+
126
+ # def openDrawPDF(data=0,pdf_content=0,dpi=0):
127
+
128
+ # if pdf_content:
129
+ # doc = fitz.open(stream=pdf_content, filetype="pdf")
130
+ # else:
131
+ # doc = fitz.open("pdf",data) #dropbox path
132
+ # page = doc[0]
133
+ # print(page.rotation)
134
+
135
+ # if page.rect.height > page.rect.width:
136
+ # rectText = fitz.Rect(300, 200, (page.mediabox.width - 80), (page.mediabox.width + 80))
137
+ # else:
138
+ # rectText = fitz.Rect(500, 200, (page.mediabox.height - 80), (page.mediabox.height + 80))
139
+ # page.draw_rect([0 + 10, 0 + 10, page.mediabox.width - 10, page.mediabox.height - 10],color=(75 / 255, 0, 130 / 255), width=1, fill=(75 / 255, 0, 130 / 255), fill_opacity=0.9)
140
+
141
+ # if dpi:
142
+ # pix = page.get_pixmap(dpi=dpi)
143
+ # newwidth_ratio = pix.width / page.mediabox.width
144
+ # newheight_ratio = pix.height / page.mediabox.height
145
+ # else:
146
+ # print('No DPI specified')
147
+ # newwidth_ratio = 1
148
+ # newheight_ratio = 1
149
+
150
+ # # Apply the transformation to the rectangle's area and perimeter
151
+ # area = ((page.mediabox.width - 10 - 10) * newwidth_ratio) * ((page.mediabox.height - 10 - 10) * newheight_ratio)
152
+ # perimeter = (((page.mediabox.width - 10 - 10) * 2) * newwidth_ratio) + (((page.mediabox.height - 10 - 10) * 2) * newheight_ratio)
153
+
154
+ # # Apply the transformation to the text annotation (shift and scale its position)
155
+ # text = """Scale Document"""
156
+ # annot1 = page.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1, 1, 1),text_color=(1, 1, 1), rotate=page.rotation, align=1)
157
+ # annot1.update()
158
+ # # print('fitzzzzzzzzzzzz',fitz.__version__)
159
+ # # doc.save('drawnOn_transformed.pdf')
160
+ # return doc, area, perimeter
161
+
162
+ def openDrawPDF(data=0,pdf_content=0,dpi=0):
163
+ # Open the document
164
+ # doc = fitz.open(pdfOutput) # Dropbox
165
+ if pdf_content:
166
+ doc = fitz.open(stream=pdf_content, filetype="pdf")
167
+ else:
168
+ doc = fitz.open("pdf",data) #dropbox path
169
+ page = doc[0]
170
+ derotationMatrix=page.derotation_matrix
171
+ print(page.rotation)
172
+
173
+ if page.rect.height > page.rect.width:
174
+ rectText = fitz.Rect(300, 200, (page.mediabox.width - 80), (page.mediabox.width + 80))
175
+ else:
176
+ rectText = fitz.Rect(500, 200, (page.mediabox.height - 80), (page.mediabox.height + 80))
177
+
178
+ if dpi:
179
+ pix = page.get_pixmap(dpi=dpi)
180
+ newwidth_ratio = pix.width / page.mediabox.width
181
+ newheight_ratio = pix.height / page.mediabox.height
182
+ else:
183
+ print('No DPI specified')
184
+ newwidth_ratio = 1
185
+ newheight_ratio = 1
186
+
187
+ # Apply the transformation to the rectangle's area and perimeter
188
+ area = ((page.mediabox.width) * newwidth_ratio) * ((page.mediabox.height ) * newheight_ratio)
189
+ perimeter = (((page.mediabox.width ) * 2) * newwidth_ratio) + (((page.mediabox.height) * 2) * newheight_ratio)
190
+ # full page rectangle
191
+
192
+ # Original diagonal points
193
+ startRect = fitz.Point(0, 0)
194
+ endRect = fitz.Point(page.mediabox.width, page.mediabox.height)
195
+
196
+ # Apply derotation if needed
197
+ # startRect = startRect * derotationMatrix
198
+ # endRect = endRect * derotationMatrix
199
+
200
+ # Create rectangle corners from diagonal points
201
+ top_left = fitz.Point(startRect.x, startRect.y)
202
+ top_right = fitz.Point(endRect.x, startRect.y)
203
+ bottom_right = fitz.Point(endRect.x, endRect.y)
204
+ bottom_left = fitz.Point(startRect.x, endRect.y)
205
+
206
+ # List of points for polyline (closing the rectangle)
207
+ rect_points = [top_left, top_right, bottom_right, bottom_left, top_left]
208
+
209
+ # Add polyline annotation
210
+ annot = page.add_polyline_annot(rect_points)
211
+ annot.set_colors(stroke=(75/255, 0, 130/255), fill=(75/255, 0, 130/255))
212
+ annot.set_opacity(0.9)
213
+ annot.set_border(width=20)
214
+ annot.set_info(content=str(perimeter)+' m', subject='Perimeter Measurement', title="ADR Team")
215
+ annot.update()
216
+
217
+ page_width = page.rect.width
218
+ page_height = page.rect.height
219
+ startLength = fitz.Point(0, page_height / 2) *derotationMatrix # left side
220
+ endLength = fitz.Point(page_width, page_height / 2) *derotationMatrix # right side
221
+
222
+ length = math.sqrt((endLength.x - startLength.x)**2 + (endLength.y - startLength.y)**2)
223
+ print(f"Line length: {length} points")
224
+ # startLength = startLength * derotationMatrix
225
+ # endLength = endLength * derotationMatrix
226
+
227
+ shapeLength=[startLength, endLength]
228
+ line_annot = page.add_polyline_annot(shapeLength)
229
+ line_annot.set_colors(stroke=(75/255, 0, 130/255)) # red line
230
+ line_annot.set_border(width=10)
231
+ line_annot.set_info(content=str(length)+' m',subject='Perimeter Measurement', title="ADR Team")
232
+ line_annot.update()
233
+
234
+ # Apply the transformation to the text annotation (shift and scale its position)
235
+ text = """Scale Document"""
236
+ annot1 = page.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1, 1, 1),text_color=(1, 1, 1), rotate=page.rotation, align=1)
237
+ annot1.update()
238
+
239
+ # doc.save('drawnOn_transformed.pdf')
240
  return doc, area, perimeter