From a7f85ddd7ddb5507d89e5e6a5635de073d388311 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 4 Jan 2019 18:59:53 +0100 Subject: [PATCH] Inline some image-handling code for postscript. --- lib/matplotlib/backends/backend_ps.py | 30 +++++++-------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 3a01d9595602..9fa7ebc496bd 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -2,7 +2,6 @@ A PostScript backend, which can produce both PostScript .ps and .eps. """ -import binascii import datetime import glob from io import StringIO, TextIOWrapper @@ -13,6 +12,7 @@ import shutil import subprocess from tempfile import TemporaryDirectory +import textwrap import time import numpy as np @@ -392,20 +392,6 @@ def _get_font_ttf(self, prop): font.set_size(size, 72.0) return font - def _rgb(self, rgba): - h, w = rgba.shape[:2] - rgb = rgba[::-1, :, :3] - return h, w, rgb.tostring() - - def _hex_lines(self, s, chars_per_line=128): - s = binascii.b2a_hex(s) - nhex = len(s) - lines = [] - for i in range(0, nhex, chars_per_line): - limit = min(i+chars_per_line, nhex) - lines.append(s[i:limit]) - return lines - def get_image_magnification(self): """ Get the factor by which to magnify images passed to draw_image. @@ -422,17 +408,15 @@ def option_image_nocomposite(self): # docstring inherited return not rcParams['image.composite_image'] - def _get_image_h_w_bits_command(self, im): - h, w, bits = self._rgb(im) - imagecmd = "false 3 colorimage" - - return h, w, bits, imagecmd - def draw_image(self, gc, x, y, im, transform=None): # docstring inherited - h, w, bits, imagecmd = self._get_image_h_w_bits_command(im) - hexlines = b'\n'.join(self._hex_lines(bits)).decode('ascii') + h, w = im.shape[:2] + imagecmd = "false 3 colorimage" + data = im[::-1, :, :3] # Vertically flipped rgb values. + # data.tobytes().hex() has no spaces, so can be linewrapped by relying + # on textwrap.fill breaking long words. + hexlines = textwrap.fill(data.tobytes().hex(), 128) if transform is None: matrix = "1 0 0 1 0 0"