Spaces:
Sleeping
Sleeping
| # -*- 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(docpage): | |
| 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) | |
| pix = docpage.get_pixmap() | |
| image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) | |
| img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY) | |
| kernel = np.ones((3,3),np.uint8) | |
| img = cv2.dilate(img,kernel, iterations=6) | |
| img=cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV)[1] | |
| # cv2_imshow(img) | |
| contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
| # Check the orientation of page through large shape drawn | |
| 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 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,dpi=0): | |
| docPath = fitz.open("pdf",data) #dropbox path | |
| pageDocPath=docPath[0] | |
| bool_T_F=is_content_rotated(pageDocPath) | |
| if bool_T_F==True: | |
| docPath = fitz.open("pdf",data) #dropbox path | |
| pageDocPath=docPath[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) | |
| if dpi: | |
| pix=pageDocPath.get_pixmap(dpi=dpi) | |
| newwidth_ratio=pix.width/pageDocPath.mediabox.width | |
| newheight_ratio=pix.height/pageDocPath.mediabox.height | |
| else: | |
| newwidth_ratio=1 | |
| newheight_ratio=1 | |
| area=((pageDocPath.mediabox.height-500-250)*newheight_ratio) * ((pageDocPath.mediabox.width-500-250)*newwidth_ratio) | |
| perimeter=(((pageDocPath.mediabox.width-500-250)*newwidth_ratio) *2) + ( ((pageDocPath.mediabox.height-500-250)*newheight_ratio) *2) | |
| 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() | |
| # docPath.save('drawnOn.pdf') | |
| return docPath,area,perimeter | |
| else: | |
| if dpi: | |
| doc,area,perimeter=openDrawPDF(data,dpi=dpi) | |
| return doc,area,perimeter | |
| else: | |
| doc,area,perimeter=openDrawPDF(data) | |
| return doc,area,perimeter | |
| """### Open PDF and draw a rectangle on it using Fitz""" | |
| def openDrawPDF(data,dpi=0): | |
| doc=fitz.open("pdf", data) | |
| page = doc[0] | |
| print('piixelsize',page.mediabox) | |
| if page.rect.height >page.rect.width: | |
| rectText=fitz.Rect(300, 200, (page.mediabox.width-80),( page.mediabox.width+80) ) | |
| else: | |
| rectText=fitz.Rect(500, 200, (page.mediabox.height-80),( page.mediabox.height+80) ) | |
| 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 ) | |
| if dpi: | |
| pix=page.get_pixmap(dpi=dpi) | |
| newwidth_ratio=pix.width/page.mediabox.width | |
| newheight_ratio=pix.height/page.mediabox.height | |
| else: | |
| newwidth_ratio=1 | |
| newheight_ratio=1 | |
| area=((page.mediabox.width-10-10)*newwidth_ratio)*((page.mediabox.height-10-10)*newheight_ratio) | |
| perimeter=(((page.mediabox.width-10-10)*2)*newwidth_ratio)+(((page.mediabox.height-10-10)*2)*newheight_ratio) | |
| 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() | |
| print('fitzzzzzzzzzzzz',fitz.__version__) | |
| # doc.save('drawnOn.pdf')#, encryption=encrypt, permissions=perm) | |
| return doc,area,perimeter | |
| """### Extract color""" | |