Converting PNG file to display equation on Kindle

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
LasCondes
Posts: 3
Joined: 2012-09-23T07:53:03-07:00
Authentication code: 67789

Converting PNG file to display equation on Kindle

Post by LasCondes »

Hi,

I am working on converting a long latex document with lots of equations to the modi file format to be displayed on a Kindle. I am using SimpleTex4ht to convert the latex document to html and create png files for all the equations.

The problem is the png files are set with a transparent background which does not work on the Kindle. I need to remove the transparent background. I am using the following arguments with the command line version of ImageMagick

convert filein.png -channel RGB -fill White -opaque None +channel -alpha Off -colorspace Gray fileout.png

which does work but the quality is not very good.

Is there a way that the final image can look as if it were displayed on a white background but without the transparency? For example convert the alpha blending to a grey scale?

Original
Image

After convert
Image

Any help on improving the result is greatly appreciated.

LasCondes
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Converting PNG file to display equation on Kindle

Post by glennrp »

Use "convert -draw ..." or "composite" to compose the PNG over a white canvas.
indiego
Posts: 75
Joined: 2010-10-16T06:35:10-07:00
Authentication code: 8675308

Re: Converting PNG file to display equation on Kindle

Post by indiego »

Why don't you make use of the 16(?) different gray scales of the e-ink display? Should result in a much smoother, less blocky look. And use the dpi of the e-ink display as density setting for your graphics. This way a 1:1 display (still depending on the zoom factor) is possible. Just my 2 cents.
LasCondes
Posts: 3
Joined: 2012-09-23T07:53:03-07:00
Authentication code: 67789

Re: Converting PNG file to display equation on Kindle

Post by LasCondes »

Hi indiego and glennrp,

Going from Latex -> Kindle remains a difficult, multistep, process.

@glennrp - I am trying the -draw command. I have hundreds of these equations, all different sizes. How do I get the correct white canvas to draw on?

@indiego - 16 or 256 different grey scales would be great. How do I convert the alpha channel used for the transparency to a grey scale. The Kindle draws these png images with transparency as solid black.

Appreciate any all ideas..

LasCondes
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Converting PNG file to display equation on Kindle

Post by whugemann »

glennrp wrote:Use "convert -draw ..." or "composite" to compose the PNG over a white canvas.
Yes, that should do the trick. I think that the problems originate from the fact that your equation is purely black and white (1-bit) in the original file and the smothing of the edges is done by a 4 bit alpha channel, which is a somewhat weird approach. So you could do this in a script:

Code: Select all

convert oringinal.png -format %wx%h info:
convert -size wxh xc:white temp.png
convert temp.png original.png -composite new.png
Of course, you would have to store w and h or rather wxh in a variable, such that the actual code would depend on the OS and script language that you use.

However, I guess that Fred and Anthony both know a trick to perform the entire operation in just on line of code ...
Wolfgang Hugemann
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting PNG file to display equation on Kindle

Post by fmw42 »

try this

convert Welded_Steel_Pulleys26x+original.png -background white -flatten Welded_Steel_Pulleys26x+original_flattened.png
indiego
Posts: 75
Joined: 2010-10-16T06:35:10-07:00
Authentication code: 8675308

Re: Converting PNG file to display equation on Kindle

Post by indiego »

Sorry, I'm too fixated on PDF. After reading 'LaTeX' I thought that you use a PDF as source for the conversion. :?

I donno anything about SimpleTex4ht and the way you produce your PNG files. But to get maximal quality you should try to use a density of 167dpi and 4bit-grayscale for the 'PDF to PNG' export. I just checked the data of the Kindle e-ink display.

Also: isn't it easier to directly convert the PDF output of LaTeX to PNG with the suggested settings? I don't see why the intermediate step is necessary. Can you explain how you produce your (source) PNG?
LasCondes
Posts: 3
Joined: 2012-09-23T07:53:03-07:00
Authentication code: 67789

Re: Converting PNG file to display equation on Kindle

Post by LasCondes »

Thank you all for your help. The examples by fmw42 and whugemann both significantly improve the quality of the equations converted to png than what I was doing.

While not elegant the steps I am using on a Mac to convert a latex document full of equations to a mobi document for the Kindle are:

a) Convert .tex file to html file with png files for the equations with SimpleTeX4ht with "XHTML" checked.
b) Convert transparency of png files used for the equations to grey scale with a simple script using the commands shown by fmw42 and ImageMagick.
c) Open html file of document in Sigil and add meta tags for the Author, Title, and Language. Save as an epub.
d) Open epub in Kindle Previewer and save as mobi file.

Regarding indiego´s question if I could just output a better png file from SimpleTeX4ht and skip step b. -Maybe, probably, I don´t know. SimpleTex4ht basically calls tex4ht as a post processor and I think in here the latex equations to png happens. You have options to output to MathML instead of png but I don´t see how to set more details about the exact format of the png file. The problem really isn´t the png file but more that the Kindle can´t handle the transparency in the original png files.

LasCondes
Post Reply