Marthee commited on
Commit
9049c2d
·
verified ·
1 Parent(s): 27687d5

Update pixelconversion.py

Browse files
Files changed (1) hide show
  1. pixelconversion.py +4 -35
pixelconversion.py CHANGED
@@ -24,37 +24,8 @@ def openDrawPDF(data):
24
 
25
  doc=fitz.open("pdf", data)
26
  page = doc[0]
27
- pix = page.get_pixmap() # render page to an image
28
- pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
29
- img=np.array(pl)
30
-
31
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
32
- ratio = pix.width / img.shape[1]
33
-
34
- imgGry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
35
- imgBW=cv2.threshold(imgGry, 254, 255, cv2.THRESH_BINARY_INV)[1]
36
-
37
- contours, hierarchy = cv2.findContours(imgBW, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
38
-
39
- newcontours = contours[2:]
40
- areas = [cv2.contourArea(c) for c in newcontours]
41
- max_index2 = np.argmax(areas)
42
- # print(max_index)
43
- cnt=newcontours[max_index2]
44
- contour=cnt
45
-
46
- perimeter = cv2.arcLength(contour, True)
47
- approx = cv2.approxPolyDP(contour, 0.01* perimeter, True)
48
-
49
- shape=[]
50
- for point in approx:
51
- x1, y1 = point[0]
52
- p1 = fitz.Point(x1*ratio,y1*ratio)
53
- p1=p1*page.derotation_matrix
54
- shape.append([p1[0],p1[1]])
55
- # cv2.circle (img, (x1, y1), 5, 255, 5)
56
- print(shape)
57
- # rotate=0
58
  if page.rect.height > page.rect.width:
59
  rectText=fitz.Rect(300, 200, (page.rect.width-80),( page.rect.width+80) )
60
  rotate=0
@@ -63,16 +34,14 @@ def openDrawPDF(data):
63
  rotate=90
64
 
65
  if page.rotation !=0:
66
- page.draw_rect([shape[3][0]-20,shape[3][1]-20,shape[1][0]+20,shape[1][1]+20], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.95 )
67
  else:
68
- page.draw_rect([shape[0][0]-20,shape[0][1]-20,shape[2][0]+20,shape[2][1]+20], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.95 )
69
 
70
  text = """Scale Document"""
71
  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)
72
  annot1.update()
73
 
74
- doc.save('2kk.pdf')#, encryption=encrypt, permissions=perm)
75
-
76
  return doc#,encrypt,perm
77
  """### Extract color"""
78
 
 
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
 
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:
39
+ 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 )
40
 
41
  text = """Scale Document"""
42
  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)
43
  annot1.update()
44
 
 
 
45
  return doc#,encrypt,perm
46
  """### Extract color"""
47