Text to Images [Almost Solved]

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Text to Images [Almost Solved]

Post by jmac698 »

Hi,
I can't find any solution to the simple problem of converting text to a set of image pages. What I want is to convert a plain text file into images of size 720x480 or 1920x1080 etc. The lines must word-wrap and the pages must be generated.

Method 1
convert -page 720x480 text:short.txt text.jpg

This will create text.jpg for one page or text-0.jpg, text-1.jpg for multiple pages

Problem: it doesn't wrap long lines

Method 2
Use a2ps to format the text, and imagemagick to convert the .ps file to images.

Problem: I can't specify 720x480 as a size in a2ps. It's geared to page sizes like Letter, A4. Even if I set the DPI to give me close sizes in pixels, it won't be the correct aspect ratio of 720x480.

Method 3
convert -size 720x480 pango:@short.txt test.jpg

Problem: This wraps lines but overflows the canvas vertically and doesn't creat multiple pages

Also had a problem saving as ANSI text. Use utf-8 as the encoding in
Windows Notepad.

So I wish, either text: could wrap lines or pango: would paginate.
I thought there might be a way to see how much text was "consumed" in creating a page and then forcefully make one page at a time, but it would be slow and I don't see how to read that info. I could make one huge image vertically and graphically cut it up somehow, but very awkward. Any manual options like screenshotting the pages in a wordprocessor won't work, the text is a full book. Any ideas?
Last edited by jmac698 on 2013-12-20T06:20:26-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Text to Images

Post by snibgo »

... the text is a full book. Any ideas?
I would use PDF. When the book is in pdf format, then ...

Code: Select all

convert book.pdf p.png
... will write each page as an image. See also the "-density" option.
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: Text to Images

Post by jmac698 »

That would be like option 2 but slightly better. It won't reflow the text to the proper aspect ratio. I'll have a skinny page in the middle of a wider canvas. But, it would at least be usable, thanks - except that my test crashed:
convert.exe: `%s' (%d) "gswin32c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOP
ROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha"
-dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72" "-sOutputFile=C:/Users/me/App
Data/Local/Temp/magick-79767kLIFzY7bp_L-1" "-fC:/Users/me/AppData/Local/Temp/mag
ick-797691QvLW3kHQbS" "-fC:/Users/me/AppData/Local/Temp/magick-7976wK6RfM7LdVBm"
@ error/utility.c/SystemCommand/2033.
convert.exe: Postscript delegate failed `SimpleLensImaging_Heide2013.pdf': No su
ch file or directory @ error/pdf.c/ReadPDFImage/682.
convert.exe: no images defined `pdf.png' @ error/convert.c/ConvertImageCommand/3
144.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Text to Images

Post by snibgo »

As a general rule, each page should look exactly as it does in Adobe Reader, without being a skinny page.

On your crash: that command is very strange. Perhaps there is a problem with your delegates.xml file. What version of IM are you running? On Windows 7 I suppose?

Can you run the command again, but with the "-verbose" option and post the output here?
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: Text to Images

Post by jmac698 »

C:\text2img>convert -verbose SimpleLensImaging_Heide2013.pdf pdf.png
"gswin32c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=50000
0000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGra
phicsAlphaBits=4 "-r72x72" "-sOutputFile=C:/Users/me/AppData/Local/Temp/magick-
10036Zd3YzVsX2lAX%d" "-fC:/Users/me/AppData/Local/Temp/magick-10036vxw92wK-fsD0"
"-fC:/Users/me/AppData/Local/Temp/magick-10036jeCVfzczYfeo"
convert.exe: `%s' (%d) "gswin32c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOP
ROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha"
-dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72" "-sOutputFile=C:/Users/me/App
Data/Local/Temp/magick-10036Zd3YzVsX2lAX-1" "-fC:/Users/me/AppData/Local/Temp/ma
gick-10036vxw92wK-fsD0" "-fC:/Users/me/AppData/Local/Temp/magick-10036jeCVfzczYf
eo" @ error/utility.c/SystemCommand/2033.
convert.exe: Postscript delegate failed `SimpleLensImaging_Heide2013.pdf': No su
ch file or directory @ error/pdf.c/ReadPDFImage/682.
convert.exe: no images defined `pdf.png' @ error/convert.c/ConvertImageCommand/3
144.
Sorry, I haven't expressed myself well. Open up a PDF on your widescreen monitor, and display the full page. It is skinny and only shows in the middle of your screen, because a Letter/A4 page has a narrower aspect ratio than the monitor. That's what I'm getting at - there's no way to map a page to 1920x1080 aspect. If you zoom it, text is cutoff at the bottom.

ps, I would assume that gs simply can't render that PDF, but it doesn't matter.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Text to Images

Post by snibgo »

The command looks okay. Have you installed Ghostscript? What happens when you type "gswin32c".

I would set the page aspect ratio (width:height) and pagination with a word processor like openoffice. That will handle 16:9 or whatever you want, and take care of line breaks, page breaks, justification, text decoration and so on.
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: Text to Images

Post by jmac698 »

Ok, I installed ghostscript (32bit) and there's no errors now. I also made a new page type of 16"x9" and 0" margins in LibreOffice, and was able to paste the ebook from it's HTML format. Then I ran

Code: Select all

convert -density 120 short.pdf short.jpg
And it produced 1920x1080 images. The problem now is there's no antialiasing at all and the text is purely blocky black and unreadable.
So then I tried the supersampling trick:

Code: Select all

convert -density 480 short.pdf -resample 120 short.jpg
But it didn't work - all pages turned black. The table of contents was underlined in blue (like a hyperlink), and only that showed.
ps
I was able to render to large files then resample them in a separate pass. This is getting close - it works and looks ok, but takes a huge amount of space and is really slow. Thanks for the help so far, I'm onto something!
Last edited by jmac698 on 2013-12-20T06:14:06-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Text to Images

Post by snibgo »

A larger density will probably cure that. If needed, follow that with a resize. For example:

Code: Select all

convert -density 480x480 short.pdf -resize 25% short.jpg
snibgo's IM pages: im.snibgo.com
jmac698
Posts: 48
Joined: 2013-12-20T01:57:16-07:00
Authentication code: 6789

Re: Text to Images

Post by jmac698 »

Amazing! That worked. Only -resize 25% worked, not -resample 120 or -resize 1920x1080. Also -density 480 is sufficient. I can also select pages with short.pdf[0-30]. One more question: can I have the output files named with 3 digits so they sort in order? eg short-000.jpg, short-001.jpg instead of short-0.jpg

ps
Nope: I'm back to the black output problem again. The inline resize didn't work on another file. I'll test it all over again to make sure.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Text to Images [Almost Solved]

Post by snibgo »

Write the output file as "short-%03d.jpg".
snibgo's IM pages: im.snibgo.com
Post Reply