def make_cursor(iconpath, x, y): image = QImage() image.load(iconpath) pm = QPixmap(image) return QCursor(pm, x, y)
def make_cursor(self, 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', rendering_intent) if tr_key in icc_transform: # We have am in-core transform already for this (faster) image = convert_profile_pil_transform(image, icc_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'], rendering_intent) 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
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
except Exception, 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 def imload(self, filepath, kwds): return self._imload(filepath, kwds) def get_buffer(self, data_np, header, format, output=None):