Python Forum

Full Version: txt to image
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
Sometimes I am handed old files that I cannot OCR, but I can read them via a backdoor.
Simplified, I end up with a few 100.000 very small txt files, each describing a person, with dates, places etc.

I need to find a way to convert these txt files into an image format (tif or png), one to one .
Of course using python, without having to buy (expensive) commercial software, that seems to be available for this purpose.

What would your recommendation be ?
I am curretly looking, but have not found anything practical yet.
thx,
paul
This should work
from pathlib import Path
from pygments.formatters import ImageFormatter
import pygments.lexers

lexer = pygments.lexers.TextLexer()
png = pygments.highlight(Path('input.txt').read_text(), lexer, ImageFormatter(line_numbers=False))
Path('output.png').write_bytes(png)
(Aug-25-2023, 09:02 AM)Gribouillis Wrote: [ -> ]This should work
Yes , it does work !
The records are actually official wedding records, with 98 fields ! (people, places, parrish, dates, witness, occupation...etc)
But obviously not all fields are known for every wedding. This piece of software produces variable size pngs so economy in
kilobytes! (Important if you have zillions)
So thank you Gribouillis.
Paul