├── .gitignore ├── 0outt.png ├── 1outt.png ├── myfont ├── 0.png ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png ├── 8.png ├── 9.png ├── a.png ├── b.png ├── bg.png ├── c.png ├── d.png ├── e.png ├── f.png ├── g.png ├── h.png ├── i.png ├── j.png ├── k.png ├── l.png ├── m.png ├── n.png ├── o.png ├── p.png ├── q.png ├── r.png ├── s.png ├── t.png ├── u.png ├── v.png ├── w.png ├── x.png ├── y.png ├── z.png ├── comma.png ├── space.png ├── aupper.png ├── braketcl.png ├── braketop.png ├── bupper.png ├── cupper.png ├── dupper.png ├── eupper.png ├── fullstop.png ├── fupper.png ├── gupper.png ├── hiphen.png ├── hupper.png ├── iupper.png ├── jupper.png ├── kupper.png ├── lupper.png ├── mupper.png ├── nupper.png ├── oupper.png ├── pupper.png ├── question.png ├── qupper.png ├── rupper.png ├── supper.png ├── tupper.png ├── uupper.png ├── vupper.png ├── wupper.png ├── xupper.png ├── yupper.png ├── zupper.png └── exclamation.png ├── requirements.txt ├── LICENSE ├── README.md ├── boom.txt ├── txttohandwriting.py └── ScannedImages-to-PDF.py /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | final_output.pdf 3 | *.png 4 | -------------------------------------------------------------------------------- /0outt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/0outt.png -------------------------------------------------------------------------------- /1outt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/1outt.png -------------------------------------------------------------------------------- /myfont/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/0.png -------------------------------------------------------------------------------- /myfont/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/1.png -------------------------------------------------------------------------------- /myfont/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/2.png -------------------------------------------------------------------------------- /myfont/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/3.png -------------------------------------------------------------------------------- /myfont/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/4.png -------------------------------------------------------------------------------- /myfont/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/5.png -------------------------------------------------------------------------------- /myfont/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/6.png -------------------------------------------------------------------------------- /myfont/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/7.png -------------------------------------------------------------------------------- /myfont/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/8.png -------------------------------------------------------------------------------- /myfont/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/9.png -------------------------------------------------------------------------------- /myfont/a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/a.png -------------------------------------------------------------------------------- /myfont/b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/b.png -------------------------------------------------------------------------------- /myfont/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/bg.png -------------------------------------------------------------------------------- /myfont/c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/c.png -------------------------------------------------------------------------------- /myfont/d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/d.png -------------------------------------------------------------------------------- /myfont/e.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/e.png -------------------------------------------------------------------------------- /myfont/f.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/f.png -------------------------------------------------------------------------------- /myfont/g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/g.png -------------------------------------------------------------------------------- /myfont/h.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/h.png -------------------------------------------------------------------------------- /myfont/i.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/i.png -------------------------------------------------------------------------------- /myfont/j.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/j.png -------------------------------------------------------------------------------- /myfont/k.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/k.png -------------------------------------------------------------------------------- /myfont/l.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/l.png -------------------------------------------------------------------------------- /myfont/m.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/m.png -------------------------------------------------------------------------------- /myfont/n.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/n.png -------------------------------------------------------------------------------- /myfont/o.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/o.png -------------------------------------------------------------------------------- /myfont/p.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/p.png -------------------------------------------------------------------------------- /myfont/q.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/q.png -------------------------------------------------------------------------------- /myfont/r.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/r.png -------------------------------------------------------------------------------- /myfont/s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/s.png -------------------------------------------------------------------------------- /myfont/t.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/t.png -------------------------------------------------------------------------------- /myfont/u.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/u.png -------------------------------------------------------------------------------- /myfont/v.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/v.png -------------------------------------------------------------------------------- /myfont/w.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/w.png -------------------------------------------------------------------------------- /myfont/x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/x.png -------------------------------------------------------------------------------- /myfont/y.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/y.png -------------------------------------------------------------------------------- /myfont/z.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/z.png -------------------------------------------------------------------------------- /myfont/comma.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/comma.png -------------------------------------------------------------------------------- /myfont/space.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/space.png -------------------------------------------------------------------------------- /myfont/aupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/aupper.png -------------------------------------------------------------------------------- /myfont/braketcl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/braketcl.png -------------------------------------------------------------------------------- /myfont/braketop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/braketop.png -------------------------------------------------------------------------------- /myfont/bupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/bupper.png -------------------------------------------------------------------------------- /myfont/cupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/cupper.png -------------------------------------------------------------------------------- /myfont/dupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/dupper.png -------------------------------------------------------------------------------- /myfont/eupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/eupper.png -------------------------------------------------------------------------------- /myfont/fullstop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/fullstop.png -------------------------------------------------------------------------------- /myfont/fupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/fupper.png -------------------------------------------------------------------------------- /myfont/gupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/gupper.png -------------------------------------------------------------------------------- /myfont/hiphen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/hiphen.png -------------------------------------------------------------------------------- /myfont/hupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/hupper.png -------------------------------------------------------------------------------- /myfont/iupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/iupper.png -------------------------------------------------------------------------------- /myfont/jupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/jupper.png -------------------------------------------------------------------------------- /myfont/kupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/kupper.png -------------------------------------------------------------------------------- /myfont/lupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/lupper.png -------------------------------------------------------------------------------- /myfont/mupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/mupper.png -------------------------------------------------------------------------------- /myfont/nupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/nupper.png -------------------------------------------------------------------------------- /myfont/oupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/oupper.png -------------------------------------------------------------------------------- /myfont/pupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/pupper.png -------------------------------------------------------------------------------- /myfont/question.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/question.png -------------------------------------------------------------------------------- /myfont/qupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/qupper.png -------------------------------------------------------------------------------- /myfont/rupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/rupper.png -------------------------------------------------------------------------------- /myfont/supper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/supper.png -------------------------------------------------------------------------------- /myfont/tupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/tupper.png -------------------------------------------------------------------------------- /myfont/uupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/uupper.png -------------------------------------------------------------------------------- /myfont/vupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/vupper.png -------------------------------------------------------------------------------- /myfont/wupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/wupper.png -------------------------------------------------------------------------------- /myfont/xupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/xupper.png -------------------------------------------------------------------------------- /myfont/yupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/yupper.png -------------------------------------------------------------------------------- /myfont/zupper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/zupper.png -------------------------------------------------------------------------------- /myfont/exclamation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharanya02/Text-file-to-handwritten-pdf-file/HEAD/myfont/exclamation.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | fpdf==1.7.2 2 | numpy==1.19.0 3 | opencv-python==4.2.0.34 4 | Pillow==8.2.0 5 | pytesseract==0.3.4 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020 Sharanya Mukherjee 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 |

Text to Handwriting converter

3 |

Convert text files into handwritten PDF

4 |

5 |
6 | 7 | ## Functionalities 8 | - [X] Convert a text document (.txt file) into a PDF file with the text content handwritten 9 | 10 | ## Pre-requisites: 11 | - [X] python 3.5 or 3.6 12 | - [X] [Install Tesseract: an open source text recognition (OCR) Engine](https://github.com/tesseract-ocr/tessdoc/blob/master/Home.md) 13 | - [X] dependencies from requirements.txt 14 | 15 | ``` 16 | $ pip install -r requirements.txt 17 | ``` 18 | 19 | ## Installing required python dependencies 20 | - Clone this repository onto your system 21 | ```bash 22 | $ git clone https://github.com/sharanya02/Text-file-to-handwritten-pdf-file 23 | ``` 24 | - Then, create a virtual environment and install the packages from requirements.txt. 25 | ```bash 26 | $ python3 -m venv env 27 | $ source env/bin/activate 28 | $ pip install -r requirements.txt 29 | ``` 30 | ## Directions to run 31 | - Run the following command in the project directory:- 32 | ``` 33 | $ python txttohandwriting.py 34 | ``` 35 | 36 | ## Demo 37 | 38 |

39 | -------------------------------------------------------------------------------- /boom.txt: -------------------------------------------------------------------------------- 1 | What is a coronavirus? 2 | The coronavirus family causes illnesses ranging from the common cold to more severe diseases such as severe acute respiratory syndrome SARS and Middle East respiratory syndrome MERS, according to the WHO. 3 | 4 | They circulate in animals and some can be transmitted between animals and humans. Several coronaviruses are circulating in animals that have not yet infected humans. 5 | 6 | The new coronavirus, the seventh known to affect humans, 7 | 8 | MORE ON HEALTH 9 | Peru COVID-19 outbreak: Health workers demand more protection 10 | last week 11 | Grim India record with 2,000 dead in a day: Live updates 12 | last week 13 | Chile sees spike in COVID-19 deaths as lockdown falters 14 | last week 15 | Beijing shuts schools over new coronavirus outbreak: Live updates 16 | last week 17 | The World Health Organization (WHO) has declared the new coronavirus outbreak, which originated in Wuhan, China, a pandemic. 18 | 19 | As of June 25, the global death toll surpassed 482,000 amid more than 9.4 million cases. Over 4.7 million people have recovered from the disease worldwide, according to the data collected by the Johns Hopkins University in the United States. 20 | 21 | More: 22 | Coronavirus: All you need to know about the symptoms and risks 23 | How does coronavirus spread and how can you protect yourself? 24 | Coronavirus: Which countries have confirmed new cases? 25 | Here is what you need to know: 26 | 27 | What is a coronavirus? 28 | The coronavirus family causes illnesses ranging from the common cold to more severe diseases such as severe acute respiratory syndrome (SARS) and Middle East respiratory syndrome (MERS), according to the WHO. 29 | 30 | They circulate in animals and some can be transmitted between animals and humans. Several coronaviruses are circulating in animals that have not yet infected humans. 31 | 32 | The new coronavirus, the seventh known to affect humans, has been named COVID-19. 33 | 34 | 35 | Coronavirus: Italy to close all schools and universities (2:19) 36 | 37 | What are the symptoms? 38 | Common signs of infection include fever, coughing and breathing difficulties. In severe cases, it can cause pneumonia, multiple organ failure and death. 39 | The WHO recommends basic hygiene such as regularly washing hands with soap and water, and covering your mouth with your elbow when sneezing or coughing. -------------------------------------------------------------------------------- /txttohandwriting.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | from fpdf import FPDF 3 | from PIL import Image 4 | import os 5 | BG = Image.open("myfont/bg.png") 6 | sizeOfSheet = BG.width 7 | gap, _ = 0, 0 8 | allowedChars = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM,.-?!() 1234567890' 9 | 10 | def writee(char): 11 | global gap, _ 12 | if char != '\n': 13 | cases = Image.open("myfont/%s.png" % char.lower()) 14 | BG.paste(cases, (gap, _)) 15 | size = cases.width 16 | gap += size 17 | 18 | def letterwrite(word): 19 | global gap, _ 20 | if gap > sizeOfSheet - 95 * (len(word)): 21 | gap = 0 22 | _ += 200 23 | special_char = {'.':'fullstop','!':'exclamation','?':'question',',':'comma','(':'braketop', ')':'braketcl','-':'hiphen'} 24 | for letter in word: 25 | if letter in allowedChars: 26 | if letter.islower(): 27 | pass 28 | elif letter.isupper(): 29 | letter = letter.lower() 30 | letter += 'upper' 31 | elif special_char[letter] != None: 32 | letter = special_char[letter] 33 | writee(letter) 34 | 35 | 36 | def worddd(Input): 37 | wordlist = Input.split(' ') 38 | for i in wordlist: 39 | letterwrite(i) 40 | writee('space') 41 | 42 | 43 | if __name__ == '__main__': 44 | try: 45 | with open('boom.txt', 'r') as file: 46 | data = file.read().replace('\n', '') 47 | 48 | with open('final_output.pdf', 'w') as file: 49 | pass 50 | 51 | l = len(data) 52 | nn = len(data) // 600 53 | chunks, chunk_size = len(data), len(data) // (nn + 1) 54 | p = [data[i:i + chunk_size] for i in range(0, chunks, chunk_size)] 55 | 56 | for i in range(0, len(p)): 57 | worddd(p[i]) 58 | writee('\n') 59 | BG.save('%doutt.png' % i) 60 | BG1 = Image.open("myfont/bg.png") 61 | BG = BG1 62 | gap = 0 63 | _ = 0 64 | except ValueError as E: 65 | print("{}\nTry again".format(E)) 66 | 67 | imagelist = [] 68 | for i in range(0, len(p)): 69 | imagelist.append('%doutt.png' % i) 70 | 71 | #Converting images to pdf 72 | #Source:https://datatofish.com/images-to-pdf-python/ 73 | 74 | 75 | def pdf_creation(PNG_FILE, flag=False): 76 | rgba = Image.open(PNG_FILE) 77 | rgb = Image.new('RGB', rgba.size, (255, 255, 255)) # white background 78 | rgb.paste(rgba, mask=rgba.split()[3]) # paste using alpha channel as mask 79 | rgb.save('final_output.pdf', 80 | append=flag) #Now save multiple images in same pdf file 81 | 82 | 83 | #First create a pdf file if not created 84 | pdf_creation(imagelist.pop(0)) 85 | 86 | #Now I am opening each images and converting them to pdf 87 | #Appending them to pdfs 88 | for PNG_FILE in imagelist: 89 | pdf_creation(PNG_FILE, flag=True) 90 | -------------------------------------------------------------------------------- /ScannedImages-to-PDF.py: -------------------------------------------------------------------------------- 1 | # Usage 2 | # python ScannedImages-to-PDF.py -i image_path 3 | 4 | from PIL import Image 5 | import pytesseract 6 | import argparse 7 | import cv2 8 | import os 9 | 10 | # construct the argument parse and parse the arguments 11 | ap = argparse.ArgumentParser() 12 | ap.add_argument("-i", 13 | "--image", 14 | required=True, 15 | help="path to input image to be OCR'd") 16 | ap.add_argument("-p", 17 | "--preprocess", 18 | type=str, 19 | default="thresh", 20 | help="type of preprocessing to be done") 21 | args = vars(ap.parse_args()) 22 | 23 | # load the example image and convert it to grayscale 24 | image = cv2.imread(args["image"]) 25 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 26 | 27 | #cv2.imshow("Image", gray) 28 | 29 | # check to see if we should apply thresholding to preprocess the 30 | # image 31 | if args["preprocess"] == "thresh": 32 | gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] 33 | 34 | # make a check to see if median blurring should be done to remove 35 | # noise 36 | elif args["preprocess"] == "blur": 37 | gray = cv2.medianBlur(gray, 3) 38 | 39 | # write the grayscale image to disk as a temporary file so we can 40 | # apply OCR to it 41 | filename = "{}.png".format(os.getpid()) 42 | cv2.imwrite(filename, gray) 43 | 44 | # load the image as a PIL/Pillow image, apply OCR, and then delete 45 | # the temporary file 46 | pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' 47 | text = pytesseract.image_to_string(Image.open(filename)) 48 | os.remove(filename) 49 | print(text.encode("utf-8")) 50 | 51 | file = open("testfile.txt", "w+") 52 | 53 | file.write(text) 54 | file.close() 55 | 56 | # show the output images 57 | # cv2.imshow("Image", image) 58 | #cv2.imshow("Output", gray) 59 | cv2.waitKey(0) 60 | print("------------------- processed OCR -------------------") 61 | BG = Image.open("myfont\\bg.png") 62 | sizeOfSheet = BG.width 63 | gap, _ = 0, 0 64 | allowedChars = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM,.-?!() 1234567890' 65 | 66 | 67 | def writee(char): 68 | global gap, _ 69 | if char == '\n': 70 | pass 71 | else: 72 | char.lower() 73 | cases = Image.open("myfont/%s.png" % char) 74 | BG.paste(cases, (gap, _)) 75 | size = cases.width 76 | gap += size 77 | del cases 78 | 79 | 80 | def letterwrite(word): 81 | global gap, _ 82 | if gap > sizeOfSheet - 95 * (len(word)): 83 | gap = 0 84 | _ += 200 85 | for letter in word: 86 | if letter in allowedChars: 87 | if letter.islower(): 88 | pass 89 | elif letter.isupper(): 90 | letter = letter.lower() 91 | letter += 'upper' 92 | elif letter == '.': 93 | letter = "fullstop" 94 | elif letter == '!': 95 | letter = 'exclamation' 96 | elif letter == '?': 97 | letter = 'question' 98 | elif letter == ',': 99 | letter = 'comma' 100 | elif letter == '(': 101 | letter = 'braketop' 102 | elif letter == ')': 103 | letter = 'braketclose' 104 | elif letter == '-': 105 | letter = 'hiphen' 106 | writee(letter) 107 | 108 | 109 | def worddd(Input): 110 | wordlist = Input.split(' ') 111 | for i in wordlist: 112 | letterwrite(i) 113 | writee('space') 114 | 115 | 116 | if __name__ == '__main__': 117 | try: 118 | with open('testfile.txt', 'r') as file: 119 | data = file.read().replace('\n', '') 120 | l = len(data) 121 | nn = len(data) // 600 122 | chunks, chunk_size = len(data), len(data) // (nn + 1) 123 | p = [data[i:i + chunk_size] for i in range(0, chunks, chunk_size)] 124 | 125 | for i in range(0, len(p)): 126 | worddd(p[i]) 127 | writee('\n') 128 | BG.save('myfont/%doutt.png' % i) 129 | BG1 = Image.open("myfont/bg.png") 130 | BG = BG1 131 | gap = 0 132 | _ = 0 133 | except ValueError as E: 134 | print("{}\nTry again".format(E)) 135 | 136 | #for conversion of img to pdf 137 | from fpdf import FPDF 138 | from PIL import Image 139 | print("------------------- Converting to PDF -------------------") 140 | imagelist = [] 141 | for i in range(0, len(p)): 142 | imagelist.append('myfont/%doutt.png' % i) 143 | cover = Image.open(imagelist[0]) 144 | width, height = cover.size 145 | pdf = FPDF(unit="pt", format=[width, height]) 146 | for i in range(0, len(imagelist)): 147 | pdf.add_page() 148 | pdf.image(imagelist[i], 0, 0) 149 | pdf.output("output.pdf", "F") 150 | --------------------------------------------------------------------------------