22A PostScript backend, which can produce both PostScript .ps and .eps.
33"""
44
5- import binascii
65import datetime
76import glob
87from io import StringIO , TextIOWrapper
1312import shutil
1413import subprocess
1514from tempfile import TemporaryDirectory
15+ import textwrap
1616import time
1717
1818import 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