Adding justified text to image in Python - python

I already can add text to an image using Pillow in Python. However, I want to know how I can add formatted text. In particular, I want to add a box of text to an image such that the text is center justified.
If this isn't possible using Pillow, I am open to other image manipulation libraries (including in other languages) that make overlaying formatted text on images easier.

refer to the function in this link - http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html#PIL.ImageDraw.PIL.ImageDraw.Draw.text
the first argument is location. you can give it based on the size of your image on which you want to add text.

Here is a simple library which does the job of text alignment using PIL:
https://gist.github.com/turicas/1455973

Related

SVG tspan find the pixel width and height

I have some text paragraphs in Unicode.
What I want to do is to convert these paragraphs into images.
The process flow I chose was to convert the text into SVG first and then to png.
I am using python for this and do not use any third-party library at the moment as it's just simple text processing. (tspan element for each line)
The issue I face is some sentences are lengthy and go out of the view box.
Is there a way to figure out a consistent font size that would work for all paragraphs/images.?
Or in other words, is there a way to figure out the height and width in pixels of a given string?
Thanks
This question is related but uses javascript instead.

OCR for Bank Receipts

I am working on OCR problem for Bank receipts and I need to extract details like the Date and Account Number for the same. After processing the input,I am using Tessaract-OCR (using pyteserract in python) for the same.I have obtained the hocr output file however I am not able to make sense of it.How do we extract information from the HOCR output file?Note that the receipt has numbers filled in Boxes like the normal forms.
I used the below text for extraction.Should I use a different Encoding?
import os
if os.path.isfile('output.hocr'):
fp=open('output.hocr','r',encoding='UTF-8')
text=fp.read()
fp.close()
Note:The attached image is one example of data.These images are available in pdf files which I am converting programmatically into images.
I personally would use something more like tesseract to do the OCR and then perhaps something like opencv with surf for the tick boxes...
or even do edge detection with opencv and surf for each section and ocr that specific area to make it more robust by analyzing that specific area rather than the whole document..
You can simply provide the image as input, instead of processing and creating an HOCR output file.
Try:-
from PIL import Image
import pytesseract
im = Image.open("reciept.jpg")
text = pytesseract.image_to_string(im, lang = 'eng')
print(text)
This program takes in the location of your image which is to be run through OCR, and extracts text from it, stores it in a variable text, and prints it out. If you want you can store the data in text in a separate file too.
P.S.:- The Image that you are trying to process, is way too complex as compared to images that tesseract is made to deal with. Due to this you may get incorrect results, after the text is processed. I would definitely recommend you to optimize it before using, like reducing the character set used, processing the image before passing it to OCR, upsampling image, having dpi over 250 etc.

Tesseract OCR: image to text containing 2 columns of text

I have an article in PNG format with 2 columns of text that I'm trying to read using Python and Tesseract OCR. However, by default, Tesseract reads from left to right in a horizontal wa. Is there an option to automatically detect the columns in the text and read from left to right column by column?
As far as I know your only chance is that one of the page segment mode available will work with your image
docs here: https://github.com/tesseract-ocr/tesseract/wiki/Command-Line-Usage#using-different-page-segmentation-modes

Capturing screenshot and parsing data from the captured image

I need to write a desktop application that performs the following operations. I'm thinking of using Python as the programming language, but I'd be more than glad to switch, if there's an appropriate approach or library in any other languages.
The file I wish to capture is an HWP file, that only certain word processors can run.
Capture the entire HWP document in an image, might span multiple pages (>10 and <15)
The HWP file contains an MCQ formatted quiz
Parse the data from the image that is separate out the questions and answers and save them as separate image files.
I have looked into the following python library, but am still not able to figure out how to perform both 1 and 3.
https://pypi.python.org/pypi/pyscreenshot
Any help would be appreciated.
If i got it correctly , you need to extract text from image.
For this one you should use an OCR like tesseract.
Before using an OCR, try to clear noises from image.
To split the image try to add some unique strings to distinguish between the quiz Q/A

Is there a way to extract text information from a postscript file? (.ps .eps)

I want to extract the text information contained in a postscript image file (the captions to my axis labels).
These images were generated with pgplot. I have tried ps2ascii and ps2txt on Ubuntu but they didn't produce any useful results. Does anyone know of another method?
Thanks
It's likely that pgplot drew the fonts in the text directly with lines rather than using text. Especially since pgplot is designed to output to a huge range of devices including plotters where you would have to do this.
Edit:
If you have enough plots to be worth
the effort than it's a very simple
image processing task. Convert each
page to something like tiff, in mono
chrome Threshold the image to binary,
the text will be max pixel value.
Use a template matching technique.
If you have a limited set of
possible labels then just match the
entire label, you can even start
with a template of the correct size
and rotation. Then just flag each
plot as containing label[1-n], no
need to read the actual text.
If you
don't know the label then you can
still do OCR fairly easily, just
extract the region around the axis,
rotate it for the vertical - and use
Google's free OCR lib
If you have pgplot you can even
build the training set for OCR or
the template images directly rather
than having to harvest them from the
image list

Categories