def __init__(self, start_response, scaled_size, logger): self.start_response = start_response self.scaled_size = scaled_size self.outbuffer = Blob() self.content_length = None self.logger = logger self.scale = True
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 GetDecimalDegrees(self, image): blob = Blob() # Remove a bit of the anti-aliasing. After this, I got better # results distinguishing the 3 from the 8. image.sharpen() image.sharpen() image.sharpen() image.sharpen() image.magick("PNG") image.quality(100) image.write(blob) tesseract.ProcessPagesBuffer(blob.data, blob.length(), self._api) text = self._api.GetUTF8Text().replace(' ', '') coordinates = re.split("°|'|\"| ", text)[:3] return self._ConvertToDecimalDegrees(coordinates)
class ImageScalerResponse(object): def __init__(self, start_response, scaled_size, logger): self.start_response = start_response self.scaled_size = scaled_size self.outbuffer = Blob() self.content_length = None self.logger = logger self.scale = True def scaler_start_response(self, status, headers, exc_info=None): if status.startswith('304'): CONTENT_LENGTH.delete(self.headers) if not status.startswith('2'): self.scale = False self.headers = headers self.status = status return self.outbuffer.data def write(self): if self.scale: return [self.outbuffer.data] else: return self.output 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 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
def walk_menu(entry): if isinstance(entry, xdg.Menu.Menu) and entry.Show is True: map(walk_menu, entry.getEntries()) elif isinstance(entry, xdg.Menu.MenuEntry) and entry.Show is True: # byte 1 signals another entry conn.sendall('\x01') img_path = icon_attr(entry.DesktopEntry).encode('utf-8') if img_path and os.path.isfile(img_path): try: # Create an empty image and set the background color to # transparent. This is important to have transparent background # when converting from SVG img = Image() img.backgroundColor(Color(0, 0, 0, 0xffff)) img.read(img_path) # scale the image to 48x48 pixels img.scale(Geometry(48, 48)) # ensure the image is converted to ICO img.magick('ICO') b = Blob() img.write(b) # icon length plus data conn.sendall(struct.pack('i', len(b.data))) conn.sendall(b.data) except Exception: conn.sendall(struct.pack('i', 0)) else: conn.sendall(struct.pack('i', 0)) name = entry.DesktopEntry.getName() # name length plus data conn.sendall(struct.pack('i', len(name))) conn.sendall(name) command = re.sub(' -caption "%c"| -caption %c', ' -caption "%s"' % name, entry.DesktopEntry.getExec()) command = re.sub(' [^ ]*%[fFuUdDnNickvm]', '', command) if entry.DesktopEntry.getTerminal(): command = 'xterm -title "%s" -e %s' % (name, command) # command length plus data conn.sendall(struct.pack('i', len(command))) conn.sendall(command)
def to_numpy(img): '''Convert ImageMagick data to numpy array. :param img: image to convert :type img: PythonMagick.Image :rtype: Numpy ndarray ''' if not isinstance(img, np.ndarray): LOGGER.debug("Converting from ImageMagick to Numpy.") img.magick('RGB') blob = Blob() img.write(blob) out_img = np.fromstring(blob.data, dtype='uint' + str(img.depth())) height, width, chans = img.rows(), img.columns(), 3 if img.monochrome(): chans = 1 return out_img.reshape(height, width, chans) return img