1212from matplotlib import get_home , get_data_path
1313from matplotlib ._image import readpng , fromarray
1414from matplotlib .numerix import ravel , where , array , \
15- zeros , Float , absolute , nonzero , put , putmask
15+ zeros , Float , absolute , nonzero , sqrt
1616class TexManager :
1717 """
1818 Convert strings to dvi files using TeX, caching the results to a
@@ -21,13 +21,15 @@ class TexManager:
2121 path = get_home ()
2222 if path is None : path = get_data_path ()
2323 texcache = os .path .join (path , '.tex.cache' )
24+
2425
2526 def __init__ (self ):
2627 if not os .path .isdir (self .texcache ):
2728 os .mkdir (self .texcache )
2829 self .imaged = {}
2930 self .postscriptd = {}
3031 self .pscnt = 0
32+ self .dvipngVersion = None
3133
3234 def make_dvi (self , tex ):
3335 prefix = self .get_prefix (tex )
@@ -215,31 +217,36 @@ def get_image(self, tex, fontsize=10, dpi=80, rgb=(0,0,0)):
215217
216218
217219 if im is None :
218- # skip cacheing while debugging
220+ # force=True to skip cacheing while debugging
219221 pngfile = self .make_png (tex , dpi , force = False )
220222 X = readpng (pngfile )
221- # To compare my results with the result of using dvipng's,
222- # change this to 'if 0:' and uncomment the third "command"
223- # variable in make_png. The black text for the tick
224- # labels on the gray background look better with the
225- # dvipng rendering, so I must be making some error in how
226- # I try and recover the alpha information
227- if 1 :
228- alpha = 1 - X [:,:,0 ]
229- #visible = alpha>0
230- #print 'min/max', min(ravel(alpha)), max(ravel(alpha))
231- Z = zeros (X .shape , Float )
232- Z [:,:,0 ] = r
233- Z [:,:,1 ] = g
234- Z [:,:,2 ] = b
235- Z [:,:,3 ] = alpha
236- im = fromarray (Z , 1 )
223+
224+ vers = self .get_dvipng_version ()
225+ if vers < '1.6' :
226+ alpha = sqrt (1 - X [:,:,0 ])
237227 else :
238- alpha = X [:,:,- 1 ]
239- from matplotlib .mlab import prctile
240- print 'ptile' , prctile (ravel (alpha ))
241- im = fromarray (X , 1 )
228+ # 1.6 has the alpha channel right
229+ alpha = sqrt (X [:,:,- 1 ])
230+
231+ #from matplotlib.mlab import prctile
232+ #print 'ptile', prctile(ravel(X[:,:,0])), prctile(ravel(X[:,:,-1]))
233+
234+ Z = zeros (X .shape , Float )
235+ Z [:,:,0 ] = r
236+ Z [:,:,1 ] = g
237+ Z [:,:,2 ] = b
238+ Z [:,:,3 ] = alpha
239+ im = fromarray (Z , 1 )
242240
243241 self .imaged [key ] = im
244242 return im
245-
243+
244+ def get_dvipng_version (self ):
245+ if self .dvipngVersion is not None : return self .dvipngVersion
246+ sin , sout = os .popen2 ('dvipng --version' )
247+ for line in sout .readlines ():
248+ if line .startswith ('dvipng ' ):
249+ self .dvipngVersion = line .split ()[- 1 ]
250+ return self .dvipngVersion
251+ raise RuntimeError ('Could not obtain dvipng version' )
252+
0 commit comments