Marthee commited on
Commit
dc350ba
·
1 Parent(s): 761774a

Update pixelconversion.py

Browse files
Files changed (1) hide show
  1. pixelconversion.py +31 -14
pixelconversion.py CHANGED
@@ -21,21 +21,45 @@ import db
21
  """### Open PDF and draw a rectangle on it using Fitz"""
22
  def openDrawPDF(path):
23
  doc = fitz.open(path)
 
24
  page = doc[0]
25
- pix = page.get_pixmap(dpi=200) # render page to an image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  rotate=0
27
- if page.rect.height > page.rect.width:
28
- rect = fitz.Rect(80,80, (page.rect.width-80),( page.rect.width+80))
29
  rectText=fitz.Rect(300, 200, (page.rect.width-80),( page.rect.width+80) )
30
  rotate=0
31
-
32
  else:
33
-
34
- rect = fitz.Rect(80,80, (page.rect.height-80),( page.rect.height+80))
35
  rectText=fitz.Rect(500, 200, (page.rect.height-80),( page.rect.height+80) )
36
  rotate=90
37
 
38
- annot = page.add_rect_annot(rect)
 
 
 
 
 
39
  annot.set_colors( fill=(75/255,0,130/255) ,stroke=(75/255,0,130/255) )
40
  annot.set_opacity(0.9)
41
  annot.set_border(border=None, width=0)
@@ -44,13 +68,6 @@ def openDrawPDF(path):
44
  text = """Scale Document"""
45
  annot1=page.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1,1,1), text_color=(1,1,1), rotate=rotate, align=1)
46
  annot1.update()
47
- perm = int( # permissions bit flags
48
- fitz.PDF_PERM_ACCESSIBILITY #always use this
49
- | fitz.PDF_PERM_PRINT # permits printing
50
-
51
- )
52
- encrypt = fitz.PDF_ENCRYPT_AES_256 # strongest algorithm
53
- # Ndoc=doc.write(encryption=encrypt,permissions=perm)
54
 
55
  return doc
56
  """### Extract color"""
 
21
  """### Open PDF and draw a rectangle on it using Fitz"""
22
  def openDrawPDF(path):
23
  doc = fitz.open(path)
24
+ out = fitz.open() # output PDF
25
  page = doc[0]
26
+ pix = page.get_pixmap() # render page to an image
27
+ pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
28
+ img=np.array(pl)
29
+
30
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
31
+ ratio = pix.width / img.shape[1]
32
+
33
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
34
+ imgBW=cv2.threshold(img, 250, 255, cv2.THRESH_BINARY_INV)[1]
35
+
36
+ contours, hierarchy = cv2.findContours(imgBW, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
37
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
38
+
39
+ contour=contours[2]
40
+ perimeter = cv2.arcLength(contour, True)
41
+ approx = cv2.approxPolyDP(contour, 0.01* perimeter, True)
42
+ shape=[]
43
+ for point in approx:
44
+ x1, y1 = point[0]
45
+
46
+ shape.append([(int(x1*ratio)),(int(y1*ratio))])
47
+ cv2.circle (img, (x1, y1), 5, 255, 5)
48
+
49
  rotate=0
50
+ if page.rect.height > page.rect.width:
 
51
  rectText=fitz.Rect(300, 200, (page.rect.width-80),( page.rect.width+80) )
52
  rotate=0
 
53
  else:
 
 
54
  rectText=fitz.Rect(500, 200, (page.rect.height-80),( page.rect.height+80) )
55
  rotate=90
56
 
57
+
58
+ if page.rotation !=0:
59
+ rect = fitz.Rect(shape[0][1],shape[0][0],shape[2][1],shape[2][0])
60
+ else:
61
+ rect= fitz.Rect(shape[0][0],shape[0][1],shape[2][0],shape[2][1])
62
+ annot = page.add_rect_annot( rect=rect ) # 'rectangle'
63
  annot.set_colors( fill=(75/255,0,130/255) ,stroke=(75/255,0,130/255) )
64
  annot.set_opacity(0.9)
65
  annot.set_border(border=None, width=0)
 
68
  text = """Scale Document"""
69
  annot1=page.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1,1,1), text_color=(1,1,1), rotate=rotate, align=1)
70
  annot1.update()
 
 
 
 
 
 
 
71
 
72
  return doc
73
  """### Extract color"""