File size: 5,054 Bytes
154e666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e39b35
8552136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154e666
 
1e39b35
 
 
154e666
8552136
dc350ba
4d1320d
154e666
4d1320d
dc350ba
9049c2d
dc350ba
9049c2d
1ef0a20
154e666
1ef0a20
154e666
1ef0a20
 
154e666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e39b35
 
 
154e666
 
1e39b35
 
154e666
c92b933
154e666
 
 
 
 
1e39b35
154e666
 
 
 
c88bdab
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
# -*- coding: utf-8 -*-
"""pixelconversion.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1hfdgkYOw8w6DdJqsZx8txmw8INUGxesl
"""


# pip install pymupdf -q

"""### Imports"""

import fitz
from PIL import Image
import numpy as np
import cv2
import db
import tsadropboxretrieval
import fitz
from io import BytesIO

################################################
### Check if page is visually rotated or not 

def is_content_rotated(mask):
      contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
      # Check the orientation of the purple shape 
      for contour in contours:
          rect = cv2.minAreaRect(contour)
          angle = rect[-1]
          print(angle)
          if angle != 0.0 and angle != 90.0 and angle != 180.0:
              print("Page content appears visually rotated.")
              return True
      print("Page content does not appear visually rotated.")
      return False

def drawisrotated(data):
  docPath = fitz.open("pdf",data) #dropbox path
  pageDocPath=docPath[0]
  if pageDocPath.rotation !=0:
   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)
  else:
   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)
  
  pix = pageDocPath.get_pixmap()
  image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
  mask=DetectColor(img,color=(75,0,130))
  bool_T_F=is_content_rotated(mask)
  print(bool_T_F)
  if bool_T_F==True:
    if pageDocPath.mediabox.height >pageDocPath.mediabox.width:
      rectText=fitz.Rect(300, 200, (pageDocPath.mediabox.width-80),( pageDocPath.mediabox.width+80) )
    else:
      rectText=fitz.Rect(500, 200, (pageDocPath.mediabox.height-80),( pageDocPath.mediabox.height+80) )

    text = """Scale Document"""
    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)
    annot1.update()
    return docPath
  else:
    doc=openDrawPDF(data)
    return doc


"""### Open PDF and draw a rectangle on it using Fitz"""
def openDrawPDF(data):
  
  doc=fitz.open("pdf", data)
  page = doc[0]
  
  if page.rect.height > page.rect.width:
    rectText=fitz.Rect(300, 200, (page.rect.width-80),( page.rect.width+80) )
  else:
    rectText=fitz.Rect(500, 200, (page.rect.height-80),( page.rect.height+80) )
  if page.rotation !=0:
    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 )
  else:
    page.draw_rect([0+10,0+10,page.rect.width-10, page.rect.height-10],  color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9 )
 
  text = """Scale Document"""
  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)
  annot1.update()

  return doc#,encrypt,perm
"""### Extract color"""

def DetectColor(img,color=0):

  imgCopy=img.copy()
  imgCopy=cv2.cvtColor(imgCopy,cv2.COLOR_BGR2HSV)
  tol=5 #tolerance
  # color=hexRGB(color)
  h,s,v = cv2.cvtColor(np.uint8([[[color[2],color[1],color[0]]]]),cv2.COLOR_BGR2HSV)[0][0]

  lower =np.array( [h- tol, 100, 100 ], dtype='uint8')
  upper = np.array( [h + tol, 255, 255],dtype='uint8')

  mask = cv2.inRange(imgCopy, lower , upper)

  detectedColors = cv2.bitwise_and(imgCopy,imgCopy, mask= mask)   # Bitwise-AND mask and original image

  kernel=np.ones((3,3),np.uint8)
  mask=cv2.dilate(mask,kernel, iterations=5)
  mask=cv2.erode(mask,kernel, iterations=4)

  detectedColors=cv2.dilate(detectedColors,kernel, iterations=5)
  detectedColors=cv2.erode(detectedColors,kernel, iterations=4)

  detectedColors=cv2.cvtColor(detectedColors,cv2.COLOR_HSV2BGR)
  detectedColors=cv2.medianBlur(detectedColors,7)
  # cv2_imshow(detectedColors)

  return mask

"""### For backend - calc area and perim"""

def getAreaPerimeter(path,name):
  dbxTeam=tsadropboxretrieval.ADR_Access_DropboxTeam('user')
  md, res =dbxTeam.files_download(path= path+name)
  data = res.content
  doc=fitz.open("pdf", data)
  area=0
  perimeter=0
  for page in doc:
      pix = page.get_pixmap(dpi=300)  # render page to an image
      pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
      img=np.array(pl)
      print(img.shape)
      img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
  mask=DetectColor(img,color=(73,0,130)) #detect colored rect drawn on the pdf
  # cv2.imwrite('maskk.png',mask)
  contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  for contour in contours:
      area = cv2.contourArea(contour)
      perimeter = cv2.arcLength(contour, True)
  return area,perimeter