Marthee commited on
Commit
8552136
·
verified ·
1 Parent(s): fa71b37

Update pixelconversion.py

Browse files
Files changed (1) hide show
  1. pixelconversion.py +48 -5
pixelconversion.py CHANGED
@@ -18,21 +18,64 @@ import numpy as np
18
  import cv2
19
  import db
20
  import tsadropboxretrieval
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  """### Open PDF and draw a rectangle on it using Fitz"""
23
  def openDrawPDF(data):
24
 
25
  doc=fitz.open("pdf", data)
26
  page = doc[0]
27
-
28
-
29
  if page.rect.height > page.rect.width:
30
  rectText=fitz.Rect(300, 200, (page.rect.width-80),( page.rect.width+80) )
31
- rotate=0
32
  else:
33
  rectText=fitz.Rect(500, 200, (page.rect.height-80),( page.rect.height+80) )
34
- rotate=90
35
-
36
  if page.rotation !=0:
37
  page.draw_rect([0+10,0+10,page.rect.height-10,page.rect.width-10], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9 )
38
  else:
 
18
  import cv2
19
  import db
20
  import tsadropboxretrieval
21
+ import fitz
22
+ from io import BytesIO
23
+
24
+ ################################################
25
+ ### Check if page is visually rotated or not
26
+
27
+ def is_content_rotated(mask):
28
+ contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
29
+ # Check the orientation of the purple shape
30
+ for contour in contours:
31
+ rect = cv2.minAreaRect(contour)
32
+ angle = rect[-1]
33
+ print(angle)
34
+ if angle != 0.0 and angle != 90.0 and angle != 180.0:
35
+ print("Page content appears visually rotated.")
36
+ return True
37
+ print("Page content does not appear visually rotated.")
38
+ return False
39
+
40
+ def drawisrotated(data):
41
+ docPath = fitz.open("pdf",data) #dropbox path
42
+ pageDocPath=docPath[0]
43
+ if pageDocPath.rotation !=0:
44
+ 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)
45
+ else:
46
+ pageDocPath.draw_rect([0+250,0+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)
47
+
48
+ pix = pageDocPath.get_pixmap()
49
+ image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
50
+ img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
51
+ mask=DetectColor(img,color=(75,0,130))
52
+ bool_T_F=is_content_rotated(mask)
53
+ print(bool_T_F)
54
+ if bool_T_F==True:
55
+ if pageDocPath.mediabox.height >pageDocPath.mediabox.width:
56
+ rectText=fitz.Rect(300, 200, (pageDocPath.mediabox.width-80),( pageDocPath.mediabox.width+80) )
57
+ else:
58
+ rectText=fitz.Rect(500, 200, (pageDocPath.mediabox.height-80),( pageDocPath.mediabox.height+80) )
59
+
60
+ text = """Scale Document"""
61
+ 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)
62
+ annot1.update()
63
+ return docPath
64
+ else:
65
+ doc=openDrawPDF(data)
66
+ return doc
67
+
68
 
69
  """### Open PDF and draw a rectangle on it using Fitz"""
70
  def openDrawPDF(data):
71
 
72
  doc=fitz.open("pdf", data)
73
  page = doc[0]
74
+
 
75
  if page.rect.height > page.rect.width:
76
  rectText=fitz.Rect(300, 200, (page.rect.width-80),( page.rect.width+80) )
 
77
  else:
78
  rectText=fitz.Rect(500, 200, (page.rect.height-80),( page.rect.height+80) )
 
 
79
  if page.rotation !=0:
80
  page.draw_rect([0+10,0+10,page.rect.height-10,page.rect.width-10], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9 )
81
  else: