Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PS backend optionally jpeg-compresses the embedded images #1959

Closed
wants to merge 2 commits into from

Conversation

leejjoon
Copy link
Contributor

This PR is to enable the postscript backend to jpeg-compress the embedded images (other artists like texts are untouched, i.e., they are still in vector format). This can significantly reduce the file size of postscript output (of course the image quality will be degraded), which will be useful for sites like arxiv.org.

    plt.savefig("file.eps", compress_images=True)

The compression is done when the eps file is created by matplolib (it uses PIL), or when the postscript file is distilled (only "xpdf" distiller is supported, I could not figure out how to do this with "ghostscript distiller").

@@ -196,7 +199,8 @@ class RendererPS(RendererBase):
fontd = maxdict(50)
afmfontd = maxdict(50)

def __init__(self, width, height, pswriter, imagedpi=72):
def __init__(self, width, height, pswriter, imagedpi=72,
compress_images=False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually prefer this as a kwarg, but I wonder if we shouldn't also make it an rcParam. There are other rcParams that control PS output (ps.usedistiller for example).

@pelson
Copy link
Member

pelson commented May 1, 2013

Good work @leejjoon - I can see a really massive benefit to having compressed images in ths PS output. I've added a couple of comments, but generally I think this is a good improvement. 😄

@leejjoon
Copy link
Contributor Author

leejjoon commented May 2, 2013

@mdboom: yes, it will be natural to introduce a new rcParam. On the other hand, this kind of lossy compression of images are supported by other backends (pdf and svg) as far as I know. So I am more inclined to introduce a rather general parameter, rather than a ps-specific one. For example, "savefig.compress_images"?

@pelson : yes, we'd better use the savefig.jpeg_quality. However, one issue is that a different parameter is used for jpeg-compression in the xpdf-distiller (and I do not know of any easy conversion between the twos). Any suggestion?

@mdboom
Copy link
Member

mdboom commented May 3, 2013

@leejjoon: Yes, let's introduce a general parameter, and over time we can add support for this sort of thing to other backends.

What is the different parameter used for xpdf-distiller? I can't find it. Do you mean a matplotlib rcParam or a parameter to the pdftops tool?

@leejjoon
Copy link
Contributor Author

leejjoon commented May 5, 2013

When "xpdf" distiller is used, the original ps file created by matplotlib is converted to a pdf file using ghostscript (ps2pdf) and then converted back to ps file using "pdftops". During the 1st step, the images are decompressed (if necessary) and then recompressed. During the 2nd step, the original compression seems to be reserved.

  1. it would make things simpler if image compression is preserved during the 1st step, but I could not find how.
  2. ps2pdf (i.e., pdf driver of ghostscript) uses "QFactor" to control quality (0~1) of compression, which is different from what PIL uses. On the other hand, the jpeg output driver of ghostscript support both parameters.
  3. I could not figure out how to control the image compression during the 2nd step.

@tacaswell
Copy link
Member

@leejjoon, @mdboom is this ready to go, or do we want to wait for the rcparam in this PR?

@tacaswell tacaswell modified the milestones: v1.5.x, v1.4.0 Feb 27, 2014
@jkseppan
Copy link
Member

Looks potentially very useful, but I would prefer to make the lossy compression an image-by-image option, with an rcParam or keyword argument to set it for all images at once. I think it's quite reasonable to want to compress a photograph lossily and keep other bitmaps (e.g. results of MixedModeBackend rasterization) lossless.

@tacaswell tacaswell modified the milestones: proposed next point release, next point release Feb 19, 2015
@tacaswell tacaswell modified the milestones: next point release, proposed next point release Jun 18, 2015
@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Aug 29, 2017
@jklymak jklymak removed this from the needs sorting milestone May 9, 2018
@jklymak jklymak added this to the v3.0 milestone May 9, 2018
@jklymak
Copy link
Member

jklymak commented May 9, 2018

This seems useful (though not to me personally).

@tacaswell tacaswell modified the milestones: v3.0, v3.1 Jul 7, 2018
@jklymak
Copy link
Member

jklymak commented Oct 5, 2018

Closing as abandoned. Feel free to re-open; does seem potentially useful (though pdf does this by default - do any journals bork at PDF these days?)

@jklymak jklymak closed this Oct 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants