Hi,

I've just moved from MATLAB to matplotlib, and I'm really impressed
with the quality of the PS figures it generates with usetex and the
xpdf distiller. I've hit a couple of problems though, one I've manged
to solve (patch against 0.87.4 attached) and the other I'd be greatful
if you could help me with please.

I've been using imshow to basically put a set of axes round an image
produced my simulation code. Here's a minimal version of my script:

----------------------------------------------
from pylab import *

rc('text', usetex=True)
rc('ps', usedistiller="xpdf")

figure(1,figsize=(6, 4))
im=imread('image.png') # a 301x318 image
imshow(im,interpolation='nearest',extent=[0.98, 20, 0.01, 0.5])
axis('normal');
savefig('image.eps')
--------------------------------------------

The first problem I noticed is that the distilling process was causing
some of my images to have (lossy) compression applied and others not.
It turns out that it is a feature of ps2pdf that it tries to detect
the content of the image and apply appropriate compression.  You can
over ride this distiller options. My patch adds a new rc option
ps.image_compression that can be set to auto (preserves the current
behaviour), DCTEncode (applies lossy JPEG compression), and
FlateEncode (lossless compression). The distiller commands are
embedded in the ps file. I looked at making it a flag on each image,
but couldn't get it to work. Another way to do it is to pass extra
command line options to ps2pdf (-dAutoFilterColorImages=false
-sColorImageFilter=FlateEncode should do it for colour images). I
thought embedding it in the PS file would be more flexible.


My second problem involved the resolutions of the image.  I'd like to
preserve the resolution of my image in the PS output, but I can't
figure out how to stop the image being resized and interpolated.
Obviously you need to do this for the bitmap backends, but for vector
ones surely you can just scale the original image in the vector
output.

Thanks in advance for you help and some great software!

JIM
---
--- matplotlib-0.87.4/lib/matplotlib/backends/backend_ps.py     2006-07-10 18:55:59.000000000 +0100
+++ matplotlib-0.87.4.new/lib/matplotlib/backends/backend_ps.py 2006-07-27 13:00:42.000000000 +0100
@@ -367,9 +367,11 @@ class RendererPS(RendererBase):
         if im.is_grayscale:
             h, w, bits = self._gray(im)
             imagecmd = "image"
+            imagetype="Gray"
         else:
             h, w, bits = self._rgb(im)
             imagecmd = "false 3 colorimage"
+            imagetype="Color"
         hexlines = '\n'.join(self._hex_lines(bits))

         xscale, yscale = w, h
@@ -381,7 +383,21 @@ class RendererPS(RendererBase):
             clipx,clipy,clipw,cliph = bbox.get_bounds()
             clip = '%s clipbox' % _nums_to_str(clipw, cliph, clipx, clipy)
         #y = figh-(y+h)
-        ps = """gsave
+
+        distilleroptions_pre = ''
+        distilleroptions_post = ''
+        compressiontype=rcParams['ps.image_compression']
+        if compressiontype != 'auto':
+            distilleroptions_pre="""
+/setdistillerparams where {pop} {userdict /setdistillerparams {pop} put} ifelse
+<< /AutoFilter%(imagetype)sImages false
+/%(imagetype)sImageFilter /%(compressiontype)s
+>> setdistillerparams
+""" % locals()
+            distilleroptions_post="""<< /AutoFilter%(imagetype)sImages true >> setdistillerparams
+""" % locals()
+
+        ps = """gsave%(distilleroptions_pre)s
 %(clip)s
 %(x)s %(y)s translate
 %(xscale)s %(yscale)s scale
@@ -391,7 +407,7 @@ class RendererPS(RendererBase):
 currentfile DataString readhexstring pop
 } bind %(imagecmd)s
 %(hexlines)s
-grestore
+%(distilleroptions_post)sgrestore
 """ % locals()
         self._pswriter.write(ps)

--- matplotlib-0.87.4/lib/matplotlib/__init__.py        2006-07-12 03:23:59.000000000 +0100
+++ matplotlib-0.87.4.new/lib/matplotlib/__init__.py    2006-07-27 12:08:40.000000000 +0100
@@ -646,6 +646,13 @@ xpdf unless xpdf-%s or later is installe
         raise ValueError('matplotlibrc ps.usedistiller must either be none, \
 ghostscript or xpdf')

+def validate_ps_image_compression(s):
+    if s in ('auto', 'FlateEncode', 'DCTEncode'):
+        return s
+    else:
+        raise ValueError('not a valid Image compression type for PS backend \
+        must be one of auto, DCTEncode FlateEncode')
+
 def validate_usetex(s):
     bl = validate_bool(s)
     if bl:
@@ -860,6 +867,7 @@ defaultParams = {
     'ps.papersize'      : [ 'letter', validate_ps_papersize], # Set the papersize/type
     'ps.useafm'   : [ False, validate_bool],  # Set PYTHONINSPECT
     'ps.usedistiller'   : [ False, validate_ps_distiller],  # use ghostscript or xpdf to distill ps output
+    'ps.image_compression' : [ 'auto', validate_ps_image_compression], # type of image compression for the distiller to use.
     'ps.distiller.res'  : [6000, validate_int],       # dpi
     'pdf.compression'   : [6, validate_int],            # compression level from 0 to 9; 0 to disable
     'plugins.directory' : ['.matplotlib_plugins', str], # where plugin directory is locate

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to