def rgb2qimage(rgb): """Convert the 3D numpy array `rgb` into a 32-bit QImage. `rgb` must have three dimensions with the vertical, horizontal and RGB image axes. ATTENTION: This QImage carries an attribute `ndarray` with a reference to the underlying numpy array that holds the data. On Windows, the conversion into a QPixmap does not copy the data, so that you have to take care that the QImage does not get garbage collected (otherwise PyQt will throw away the wrapper, effectively freeing the underlying memory - boom!).""" if len(rgb.shape) != 3: raise ValueError("rgb2QImage can only convert 3D arrays") if rgb.shape[2] not in (3, 4): raise ValueError("rgb2QImage can expects the last dimension to contain exactly three (R,G,B) or four (R,G,B,A) channels") h, w, channels = rgb.shape # Qt expects 32bit BGRA data for color images: bgra = numpy.empty((h, w, 4), numpy.uint8, 'C') bgra[...,0] = rgb[...,2] bgra[...,1] = rgb[...,1] bgra[...,2] = rgb[...,0] if rgb.shape[2] == 3: bgra[...,3].fill(255) fmt = QImage.Format_RGB32 else: bgra[...,3] = rgb[...,3] fmt = QImage.Format_ARGB32 result = QImage(bgra.data, w, h, fmt) result.ndarray = bgra return result
def _get_qimage(self, rgb_data): ht, wd, channels = rgb_data.shape result = QImage(rgb_data.data, wd, ht, self.qimg_fmt) # Need to hang on to a reference to the array result.ndarray = rgb_data return result
def make_button(self, name, wtyp, icon=None, tooltip=None): picon = None if icon: iconfile = os.path.join(self.iconpath, '%s.png' % icon) try: image = QImage(iconfile) pixmap = QPixmap.fromImage(image) picon = QIcon(pixmap) qsize = QtCore.QSize(24, 24) except Exception as e: self.logger.error("Error loading icon '%s': %s" % (iconfile, str(e))) if wtyp == 'button': if picon: w = Widgets.Button() _w = w.get_widget() _w.setIconSize(qsize) _w.setIcon(picon) else: w = Widgets.Button(name) elif wtyp == 'toggle': if picon: w = Widgets.ToggleButton() _w = w.get_widget() _w.setIconSize(qsize) _w.setIcon(picon) else: w = Widgets.ToggleButton() return w
def _get_qimage(self, bgra): h, w, channels = bgra.shape fmt = QImage.Format_ARGB32 result = QImage(bgra.data, w, h, fmt) # Need to hang on to a reference to the array result.ndarray = bgra return result
def _get_qimage(self, rgb_data, format): rgb_data = np.ascontiguousarray(rgb_data) ht, wd, channels = rgb_data.shape result = QImage(rgb_data.data, wd, ht, format) # Need to hang on to a reference to the array result.ndarray = rgb_data return result
def resize(self, dims): """Resize our drawing area to encompass a space defined by the given dimensions. """ width, height = dims[:2] self.logger.debug("renderer reconfigured to %dx%d" % (width, height)) if self.surface_type == 'qpixmap': self.surface = QPixmap(width, height) else: self.surface = QImage(width, height, self.qimg_fmt)
def __init__(self, fv, fitsimage): # superclass is common subset outside of toolkits super(FBrowser, self).__init__(fv, fitsimage) # Make icons icondir = self.fv.iconpath foldericon = os.path.join(icondir, 'folder.png') image = QImage(foldericon) pixmap = QPixmap.fromImage(image) self.folderpb = QIcon(pixmap) fileicon = os.path.join(icondir, 'file.png') image = QImage(fileicon) pixmap = QPixmap.fromImage(image) self.filepb = QIcon(pixmap) fitsicon = os.path.join(icondir, 'fits.png') image = QImage(fitsicon) pixmap = QPixmap.fromImage(image) self.fitspb = QIcon(pixmap)
def add_action(self, text, toggle=False, iconpath=None): child = ToolbarAction() if iconpath: image = QImage(iconpath) qsize = QtCore.QSize(24, 24) image = image.scaled(qsize) pixmap = QPixmap.fromImage(image) iconw = QIcon(pixmap) action = self.widget.addAction(iconw, text, child._cb_redirect) else: action = self.widget.addAction(text, child._cb_redirect) action.setCheckable(toggle) child.widget = action self.add_ref(child) return child
def resize(self, dims): """Resize our drawing area to encompass a space defined by the given dimensions. """ width, height = dims[:2] self.logger.debug("renderer reconfigured to %dx%d" % (width, height)) if self.surface_type == 'qpixmap': self.surface = QPixmap(width, height) else: self.surface = QImage(width, height, self.qimg_fmt) # fill surface with background color; # this reduces unwanted garbage in the resizing window painter = QPainter(self.surface) size = self.surface.size() sf_wd, sf_ht = size.width(), size.height() bg = self.viewer.img_bg bgclr = self._get_color(*bg) painter.fillRect(QtCore.QRect(0, 0, sf_wd, sf_ht), bgclr)
def gray2qimage(gray): """Convert the 2D numpy array `gray` into a 8-bit QImage with a gray colormap. The first dimension represents the vertical image axis. ATTENTION: This QImage carries an attribute `ndarray` with a reference to the underlying numpy array that holds the data. On Windows, the conversion into a QPixmap does not copy the data, so that you have to take care that the QImage does not get garbage collected (otherwise PyQt will throw away the wrapper, effectively freeing the underlying memory - boom!).""" if len(gray.shape) != 2: raise ValueError("gray2QImage can only convert 2D arrays") h, w = gray.shape bgra = numpy.empty((h, w, 4), numpy.uint8, 'C') bgra[...,0] = gray bgra[...,1] = gray bgra[...,2] = gray bgra[...,3].fill(255) fmt = QImage.Format_RGB32 result = QImage(bgra.data, w, h, fmt) result.ndarray = bgra return result
def make_cursor(iconpath, x, y): image = QImage() image.load(iconpath) pm = QPixmap(image) return QCursor(pm, x, y)
def _imload(self, filepath, kwds): """Load an image file, guessing the format, and return a numpy array containing an RGB image. If EXIF keywords can be read they are returned in the dict _kwds_. """ start_time = time.time() typ, enc = mimetypes.guess_type(filepath) if not typ: typ = 'image/jpeg' typ, subtyp = typ.split('/') self.logger.debug("MIME type is %s/%s" % (typ, subtyp)) if (typ == 'image') and (subtyp in ('x-portable-pixmap', 'x-portable-greymap')): # Special opener for PPM files, preserves high bit depth means = 'built-in' data_np = open_ppm(filepath) elif have_pil: # PIL seems to be the faster loader than QImage, and can # return EXIF info, where QImage will not. means = 'PIL' image = PILimage.open(filepath) try: info = image._getexif() for tag, value in info.items(): kwd = TAGS.get(tag, tag) kwds[kwd] = value except Exception as e: self.logger.warn("Failed to get image metadata: %s" % (str(e))) # If we have a working color profile then handle any embedded # profile or color space information, if possible if have_cms and os.path.exists(profile['working']): # Assume sRGB image, unless we learn to the contrary in_profile = 'sRGB' try: if 'icc_profile' in image.info: self.logger.debug("image has embedded color profile") buf_profile = image.info['icc_profile'] # Write out embedded profile (if needed) prof_md5 = hashlib.md5(buf_profile).hexdigest() in_profile = "/tmp/_image_%d_%s.icc" % ( os.getpid(), prof_md5) if not os.path.exists(in_profile): with open(in_profile, 'w') as icc_f: icc_f.write(buf_profile) # see if there is any EXIF tag about the colorspace elif 'ColorSpace' in kwds: csp = kwds['ColorSpace'] iop = kwds.get('InteroperabilityIndex', None) if (csp == 0x2) or (csp == 0xffff): # NOTE: 0xffff is really "undefined" and should be # combined with a test of EXIF tag 0x0001 # ('InteropIndex') == 'R03', but PIL _getexif() # does not return the InteropIndex in_profile = 'AdobeRGB' self.logger.debug("hmm..this looks like an AdobeRGB image") elif csp == 0x1: self.logger.debug("hmm..this looks like a sRGB image") in_profile = 'sRGB' else: self.logger.debug("no color space metadata, assuming this is an sRGB image") # if we have a valid profile, try the conversion tr_key = (in_profile, 'working') if tr_key in transform: # We have am in-core transform already for this (faster) image = convert_profile_pil_transform(image, transform[tr_key], inPlace=True) else: # Convert using profiles on disk (slower) if in_profile in profile: in_profile = profile[in_profile] image = convert_profile_pil(image, in_profile, profile['working']) self.logger.info("converted from profile (%s) to profile (%s)" % ( in_profile, profile['working'])) except Exception as e: self.logger.error("Error converting from embedded color profile: %s" % (str(e))) self.logger.warn("Leaving image unprofiled.") data_np = numpy.array(image) elif have_qtimage: # QImage doesn't give EXIF info, so use 3rd-party lib if available if have_exif: with open(filepath, 'rb') as in_f: d = EXIF.process_file(in_f) kwds.update(d) means = 'QImage' qimage = QImage() qimage.load(filepath) data_np = qimage2numpy(qimage) else: raise ImageError("No way to load image format '%s/%s'" % ( typ, subtyp)) end_time = time.time() self.logger.debug("loading (%s) time %.4f sec" % ( means, end_time - start_time)) return data_np