I have long journal files that I would like to view as images. I don't want to have to screencap my text editor. I am using LibreOffice to Export them to jpg, but I have to do everything ONE. STEP. AT. A. TIME. I am looking for some kind of command line layout program that would ostensibly look like this:

$ txt2jpg -i myfile.txt -o mynewfile --format=jpg --height=872 \
--width=600 --margins=40px --font=monospace

This would take myfile.txt as input and create a series of files named mynewfile01.jpg, mynewfile02.jpg, ..., mynewfileNN.jpg as output. The images would be 872 pixels tall, 600 wide, have a 40 pixel margin all around and use a font named monospace. Obviously, the actual program would differ in the particulars, but should be capable of taking one text file and outputting a series of jpgs (or pngs or bmps or whatever) as output with the ability to format the output image fontsize and so on.

  • How do you want the text presented? Do you want to do word wrapping or do you want to convert line by line? What about page breaks? Paragraphs? etc. – Gilles 'SO- stop being evil' Jul 10 '16 at 19:43
  • Word wrapping. There are no page breaks. Paragraphs are represented by two CRLFs (Unix style). There's not a lot of formatting. It's a text file. – Oswald Roswell Jul 12 '16 at 4:44

A traditional tool for printing simple text files is enscript. This produces PostScript output. You can convert this to an image in many ways. One that gives good results is pdftopng which needs the PostScript packaged into a pdf as input (eg with ps2pdf) and produces png image files. So, using convert from ImageMagick to convert to jpg you end up with:

enscript < myfile.txt --media A4 -f CourierBold10 -o - |
ps2pdf - >out.pdf
pdftopng -mono out.pdf out # creates files out-000001.png etc
for f in out-*.png
do convert -resize 600x872 "$f" "${f/png/jpg}"
   rm "$f"
rm out.pdf

Your output is in files out*jpg.

  • Does going via png confer any advantage over, say, piping the postscript straight to ghostscript with the jpeg/jfif driver? e.g. enscript blah blah -o - myfile.txt | gs -sDEVICE=jpeg -sOutputFile=myfile.jpg - – steeldriver Jul 10 '16 at 16:33
  • @steeldriver Your suggestion sounds like a much better solution. please post as a full answer. – meuh Jul 10 '16 at 16:55
  • Actually I hadn't noticed the OP's scaling requirements - I'd have to think about that before attempting a full solution. Probably gs can do it ... – steeldriver Jul 10 '16 at 17:00
  • Why are you converting to JPEG? JPEG is bad at representing text: it's good for smooth color transitions (common in photos) and bad at sharp transitions. PNG is a lot better for an image of text. – Gilles 'SO- stop being evil' Jul 10 '16 at 19:40
  • Also, convert can take PDF as input, why are you using pdftopng first? – Gilles 'SO- stop being evil' Jul 10 '16 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.