Skip to content

Commit 18a8389

Browse files
anntzertimhoffm
authored andcommitted
Inline some image-handling code for postscript. (#13102)
1 parent 3c41a77 commit 18a8389

File tree

1 file changed

+7
-23
lines changed

1 file changed

+7
-23
lines changed

lib/matplotlib/backends/backend_ps.py

+7-23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
A PostScript backend, which can produce both PostScript .ps and .eps.
33
"""
44

5-
import binascii
65
import datetime
76
import glob
87
from io import StringIO, TextIOWrapper
@@ -13,6 +12,7 @@
1312
import shutil
1413
import subprocess
1514
from tempfile import TemporaryDirectory
15+
import textwrap
1616
import time
1717

1818
import numpy as np
@@ -392,20 +392,6 @@ def _get_font_ttf(self, prop):
392392
font.set_size(size, 72.0)
393393
return font
394394

395-
def _rgb(self, rgba):
396-
h, w = rgba.shape[:2]
397-
rgb = rgba[::-1, :, :3]
398-
return h, w, rgb.tostring()
399-
400-
def _hex_lines(self, s, chars_per_line=128):
401-
s = binascii.b2a_hex(s)
402-
nhex = len(s)
403-
lines = []
404-
for i in range(0, nhex, chars_per_line):
405-
limit = min(i+chars_per_line, nhex)
406-
lines.append(s[i:limit])
407-
return lines
408-
409395
def get_image_magnification(self):
410396
"""
411397
Get the factor by which to magnify images passed to draw_image.
@@ -422,17 +408,15 @@ def option_image_nocomposite(self):
422408
# docstring inherited
423409
return not rcParams['image.composite_image']
424410

425-
def _get_image_h_w_bits_command(self, im):
426-
h, w, bits = self._rgb(im)
427-
imagecmd = "false 3 colorimage"
428-
429-
return h, w, bits, imagecmd
430-
431411
def draw_image(self, gc, x, y, im, transform=None):
432412
# docstring inherited
433413

434-
h, w, bits, imagecmd = self._get_image_h_w_bits_command(im)
435-
hexlines = b'\n'.join(self._hex_lines(bits)).decode('ascii')
414+
h, w = im.shape[:2]
415+
imagecmd = "false 3 colorimage"
416+
data = im[::-1, :, :3] # Vertically flipped rgb values.
417+
# data.tobytes().hex() has no spaces, so can be linewrapped by relying
418+
# on textwrap.fill breaking long words.
419+
hexlines = textwrap.fill(data.tobytes().hex(), 128)
436420

437421
if transform is None:
438422
matrix = "1 0 0 1 0 0"

0 commit comments

Comments
 (0)