/usr/lib/python3/dist-packages/ginga/pilw/ImageViewPil.py is in python3-ginga 2.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #
# ImageViewPil.py -- a backend for Ginga using Python Imaging Library
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
import numpy
from io import BytesIO
from PIL import Image
from . import PilHelp
from .CanvasRenderPil import CanvasRenderer
from ginga import ImageView
class ImageViewPilError(ImageView.ImageViewError):
pass
class ImageViewPil(ImageView.ImageViewBase):
def __init__(self, logger=None, rgbmap=None, settings=None):
ImageView.ImageViewBase.__init__(self, logger=logger,
rgbmap=rgbmap,
settings=settings)
self.surface = None
self._rgb_order = 'RGBA'
self.renderer = CanvasRenderer(self)
def get_surface(self):
return self.surface
def render_image(self, rgbobj, dst_x, dst_y):
"""Render the image represented by (rgbobj) at dst_x, dst_y
in the pixel space.
"""
if self.surface is None:
return
canvas = self.surface
self.logger.debug("redraw surface")
# get window contents as a buffer and paste it into the PIL surface
rgb_arr = self.getwin_array(order=self._rgb_order)
p_image = Image.fromarray(rgb_arr)
if p_image.size != canvas.size:
# window size must have changed out from underneath us!
width, height = self.get_window_size()
canvas = Image.new("RGB", (width, height), color=0)
assert p_image.size == canvas.size, \
ImageViewPilError("Rendered image does not match window size")
self.surface = canvas
canvas.paste(p_image)
def configure_surface(self, width, height):
# create PIL surface the size of the window
# NOTE: pillow needs an RGB image in order to draw with alpha
# blending, not RGBA
#self.surface = Image.new(self._rgb_order, (width, height), color=0)
self.surface = Image.new("RGB", (width, height), color=0)
# inform the base class about the actual window size
self.configure(width, height)
def get_image_as_array(self):
if self.surface is None:
raise ImageViewPilError("No PIL surface defined")
# TODO: could these have changed between the time that self.surface
# was last updated?
wd, ht = self.get_window_size()
# Get PIL surface
p_image = self.get_surface()
arr8 = numpy.array(p_image, dtype=numpy.uint8)
arr8 = arr8.reshape((ht, wd, 3))
return arr8
def get_rgb_image_as_buffer(self, output=None, format='png', quality=90):
if self.surface is None:
raise ImageViewPilError("No PIL surface defined")
obuf = output
if obuf is None:
obuf = BytesIO()
# Get PIL surface
p_image = self.get_surface()
p_image.save(obuf, format=format, quality=quality)
if output is not None:
return None
return obuf
def update_image(self):
# subclass implements this method to actually update a widget
# from the PIL surface
self.logger.warning("Subclass should override this method")
return False
def set_cursor(self, cursor):
# subclass implements this method to actually set a defined
# cursor on a widget
self.logger.warning("Subclass should override this method")
def reschedule_redraw(self, time_sec):
# subclass implements this method to call delayed_redraw() after
# time_sec
self.delayed_redraw()
def get_rgb_order(self):
return self._rgb_order
class CanvasView(ImageViewPil):
def __init__(self, logger=None, settings=None, rgbmap=None,
bindmap=None, bindings=None):
ImageViewPil.__init__(self, logger=logger, settings=settings,
rgbmap=rgbmap)
# Needed for UIMixin to propagate events correctly
self.objects = [self.private_canvas]
def set_canvas(self, canvas, private_canvas=None):
super(CanvasView, self).set_canvas(canvas,
private_canvas=private_canvas)
self.objects[0] = self.private_canvas
def update_image(self):
# no widget to update
pass
#END
|