def finish_response(self, app_iter): if not self.scale: self.start_response(self.status, self.headers) self.output = app_iter return CONTENT_LENGTH.delete(self.headers) inBuffer = StringIO() try: for s in app_iter: self.logger.debug("Writing %d bytes into image buffer." % len(s)) inBuffer.write(s) finally: if hasattr(app_iter, 'close'): app_iter.close() rawImg = Blob() rawImg.data = inBuffer.getvalue() inBuffer.close() image = Image(rawImg) self.logger.debug("Scaling image to %s" % self.scaled_size) image.scale(self.scaled_size[0]) image.write(self.outbuffer) content_length = self.outbuffer.length() CONTENT_LENGTH.update(self.headers, content_length) CACHE_CONTROL.update(self.headers, s_maxage=CACHE_CONTROL.ONE_HOUR) self.start_response(self.status, self.headers)
def NumpytoIM(img, usm=None, verbose=False): if verbose: print "Converting numpy array to ImageMagick" out_img = PMImage() if img.dtype == 'uint16': out_img.depth(16) else: out_img.depth(8) out_img.magick('RGB') h,w,c = img.shape size_str = str(w)+'x'+str(h) out_img.size(size_str) b = Blob() b.data = img.tostring() out_img.read(b) out_img.magick('PNG') # Check if USM sharpening should be used if usm != None: if verbose: print "Running unsharp mask filter" r,s,a,t = (usm) out_img.unsharpmask(r,s,a,t) return out_img
def to_imagemagick(img, bits=16): '''Convert numpy array to Imagemagick format. :param img: image to convert :type img: Numpy ndarray :rtype: PythonMagick.Image ''' if not isinstance(img, PMImage): img = _scale(img, bits=bits) LOGGER.debug("Converting from Numpy to ImageMagick.") out_img = PMImage() if img.dtype == np.uint8: out_img.depth(8) else: out_img.depth(16) shape = img.shape # Convert also B&W images to 3-channel arrays if len(shape) == 2: tmp = np.empty((shape[0], shape[1], 3), dtype=img.dtype) tmp[:, :, 0] = img tmp[:, :, 1] = img tmp[:, :, 2] = img img = tmp out_img.magick('RGB') out_img.size(str(shape[1]) + 'x' + str(shape[0])) blob = Blob() blob.data = img.tostring() out_img.read(blob) out_img.magick('PNG') return out_img return img