def unpickle_mlp(model): import cPickle from smartNN.utils.image import tile_raster_images from PIL.Image import fromarray from smartNN.datasets.preprocessor import GCN, Standardize with open(os.environ['smartNN_SAVE_PATH'] + '/log/' + model + '/model.pkl', 'rb') as f: mlp = cPickle.load(f) data = Mnist(train_valid_test_ratio = [5,1,1], iter_class = 'SequentialSubsetIterator', rng = None) test = data.get_test() # prep = Standardize() # prep = GCN(use_std = False) # test.X = prep.apply(test.X) orig_array = tile_raster_images(X = test.X[-1001:-1], img_shape=(28,28), tile_shape=(50,20), tile_spacing=(5, 5), scale_rows_to_unit_interval=True, output_pixel_vals=True) orig_im = fromarray(orig_array) orig_im.save(NNdir + '/save/images/' + model + '_orig.jpeg') print('orig image saved. Opening image..') # orig_im.show() new_X = mlp.fprop(test.X) new_array = tile_raster_images(X = new_X[-1001:-1], img_shape=(28,28), tile_shape=(50,20), tile_spacing=(0, 0), scale_rows_to_unit_interval=True, output_pixel_vals=True) new_im = fromarray(new_array) new_im.save(NNdir + '/save/images/' + model + '_reconstruct.jpeg') print('reconstruct image saved. Opening image..')
def nbimage(data, vmin=None, vmax=None, vsym=False, saveas=None): ''' Display raw data as a notebook inline image. Parameters: data: array-like object, two or three dimensions. If three dimensional, first or last dimension must have length 3 or 4 and will be interpreted as color (RGB or RGBA). vmin, vmax, vsym: refer to rerange() saveas: Save image file to disk (optional). Proper file name extension will be appended to the pathname given. [ None ] ''' from IPython.display import display, Image from PIL.Image import fromarray from StringIO import StringIO data = rerange(data, vmin, vmax, vsym) data = data.squeeze() # try to be smart if 3 <= data.shape[0] <= 4: data = data.transpose((1, 2, 0)) s = StringIO() fromarray(data).save(s, 'png') if saveas is not None: open(saveas + '.png', 'wb').write(s) display(Image(s.getvalue()))
def storeX_TrainWithLabels(x_trainArray, y_trainArray, x_validArray, y_validArray, trainPath, validPath): print("Storing image and labels") for length in range(len(x_trainArray)): for y_range in range(23): if int(y_trainArray[length]) == y_range: img = fromarray(x_trainArray[length]) createLabelFolders(str(trainPath) + str(y_range)) if os.path.isfile( str(trainPath) + str(y_range) + "/" + str(length) + ".jpg"): print("File already exists, jump to next file") else: img.save( str(trainPath) + str(y_range) + "/" + str(length) + ".jpg") for length in range(len(x_validArray)): for y_range in range(23): if int(y_validArray[length]) == y_range: img = fromarray(x_validArray[length]) createLabelFolders(str(validPath) + str(y_range)) if os.path.isfile( str(validPath) + str(y_range) + "/" + str(length) + ".jpg"): print("File already exists, jump to next file") else: img.save( str(validPath) + str(y_range) + "/" + str(length) + ".jpg") print("Finished")
def nbimage( data, vmin = None, vmax = None, vsym = False, saveas = None ): ''' Display raw data as a notebook inline image. Parameters: data: array-like object, two or three dimensions. If three dimensional, first or last dimension must have length 3 or 4 and will be interpreted as color (RGB or RGBA). vmin, vmax, vsym: refer to rerange() saveas: Save image file to disk (optional). Proper file name extension will be appended to the pathname given. [ None ] ''' from IPython.display import display, Image from PIL.Image import fromarray from StringIO import StringIO data = rerange( data, vmin, vmax, vsym ) data = data.squeeze() # try to be smart if 3 <= data.shape[ 0 ] <= 4: data = data.transpose( ( 1, 2, 0 ) ) s = StringIO() fromarray( data ).save( s, 'png' ) if saveas is not None: open( saveas + '.png', 'wb' ).write( s ) display( Image( s.getvalue() ) )
def nbimage( data ): from IPython.display import display, Image from PIL.Image import fromarray from StringIO import StringIO s = StringIO() fromarray( data ).save( s, 'png' ) display( Image( s.getvalue() ) )
def save_image(env, file_path): """ Saves a screenshot of the given Gym environment monitor. :param Monitor env: the Gym environment monitor from which to extract the image. :param str file_path: the path to the image file. :return: """ image_rotated = np.fliplr(np.rot90(env.env.env.game_state.getScreenRGB(), 3)) fromarray(image_rotated).save(file_path)
def get_segmentation(image, rect): segmentor.set_image(image) seg_img, inp_img, mask, bbox = segmentor.segment_rect(rect) if bbox is not None: seg_img = seg_img[bbox[1]:bbox[1] + bbox[3], bbox[0]:bbox[0] + bbox[2], :] seg_img = fromarray(seg_img) seg_img.putalpha( fromarray(mask[bbox[1]:bbox[1] + bbox[3], bbox[0]:bbox[0] + bbox[2]])) return seg_img, fromarray(inp_img), fromarray(mask), bbox
def _save(self, tile, np_image, alpha=False): """Save the tile np_image at the path produced by _tile_path Parameters ---------- tile: Tile The tile from which was generated np_image np_image: array-like The numpy image to save alpha: bool (optional, default: False) True if the np_image has an alpha channel """ fromarray(np_image).save(self._tile_path(tile, alpha))
def elastic_transform(self, image: Image, mask: Image, weight: Image, grid_size: int, displacement: (int, float)) -> tuple: assert isinstance(image, Image) and isinstance( mask, Image) and isinstance(weight, Image) if random.random() > self._probability: return image, mask, weight width, height = image.size width_span = width / (grid_size * 2) height_span = height / (grid_size * 2) same_horizontal_border = np.array( np.meshgrid([0, height], np.arange(0, width, 1))).T.reshape(-1, 2) same_vertical_border = np.array( np.meshgrid(np.arange(0, height, 1), [0, width])).T.reshape(-1, 2) same_border = np.concatenate( (same_horizontal_border, same_vertical_border), axis=0) displacement_point_y = np.arange(width_span, (2 * grid_size - 1) * width_span + 1, 2 * width_span) displacement_point_x = np.arange(height_span, (2 * grid_size - 1) * height_span + 1, 2 * height_span) source_points = np.array( np.meshgrid(displacement_point_y, displacement_point_x)).T.reshape(-1, 2) displaced_points = source_points + np.random.uniform( -displacement, displacement, source_points.shape) source_points = np.concatenate((source_points, same_border), axis=0) displaced_points = np.concatenate((displaced_points, same_border), axis=0) grid_x, grid_y = np.mgrid[0:height - 1:1j * height, 0:width - 1:1j * width] grid_z = griddata(displaced_points, source_points, (grid_x, grid_y), method='cubic') map_x_32 = np.append([], [ar[:, 1] for ar in grid_z]).reshape( height, width).astype('float32') map_y_32 = np.append([], [ar[:, 0] for ar in grid_z]).reshape( height, width).astype('float32') return fromarray(cv2.remap(np.array(image), map_x_32, map_y_32, cv2.INTER_CUBIC)), \ fromarray(cv2.remap(np.array(mask), map_x_32, map_y_32, cv2.INTER_CUBIC)), \ fromarray(cv2.remap(np.array(weight), map_x_32, map_y_32, cv2.INTER_CUBIC))
def generate_reference_image(reference_index, dataset='reference', index=0, path=None): url = get_resource_locator(reference_index, dataset=dataset, scheme='file', path=path) url = urlparse(url) path = url.path[1:] # Generate reference image. if index == 0: height = 864 # px # similar to the van Hateren dataset width = 864 # px # similar to the van Hateren dataset shape = (height, width) dtype = np.uint8 info = np.iinfo(dtype) v = (info.max - info.min + 1) // 2 a = v * np.ones(shape, dtype=dtype) image = fromarray(a) image.save(path) else: raise NotImplementedError() return
def load_gnt_file(filename): """ Load characters and images from a given GNT file. :param filename: The file path to load. :return: (image: Pillow.Image.Image, character) tuples """ # Thanks to nhatch for the code to read the GNT file, available at https://github.com/nhatch/casia with open(filename, "rb") as f: while True: packed_length = f.read(4) if packed_length == b'': break length = struct.unpack("<I", packed_length)[0] raw_label = struct.unpack(">cc", f.read(2)) width = struct.unpack("<H", f.read(2))[0] height = struct.unpack("<H", f.read(2))[0] photo_bytes = struct.unpack("{}B".format(height * width), f.read(height * width)) # Comes out as a tuple of chars. Need to be combined. Encoded as gb2312, gotta convert to unicode. label = decode(raw_label[0] + raw_label[1], encoding="gb2312") # Create an array of bytes for the image, match it to the proper dimensions, and turn it into an image. image = fromarray(np.array(photo_bytes).reshape(height, width)) yield image, label
def save_mrp(audio_arr, dimension, image_size, mrp_image_path): # Doing 1-D Max Pooling N = image_size - 5 window, stride = 2**(N - 3), 2**(N - 3) audio_tensor_max_pooling = tf.nn.max_pool(audio_arr.reshape( 1, 1, 1, dimension), (1, 1, 1, window), (1, 1, 1, stride), padding='VALID') audio_tensor_max_pooling = audio_tensor_max_pooling[0, 0, 0, :] audio_tensor_dimension = audio_tensor_max_pooling.shape[0].value image_tensor = tf.stack([audio_tensor_max_pooling] * audio_tensor_dimension) recurrence_plot = tf.abs(tf.transpose(image_tensor) - image_tensor) recurrence_plot_max_pooling = recurrence_plot """recurrence_plot_max_pooling = tf.nn.max_pool(tf.reshape(recurrence_plot, (1, audio_tensor_dimension, audio_tensor_dimension, 1)), (1, 4, 4, 1), (1, 4, 4, 1), padding='VALID')""" image_dimension = recurrence_plot_max_pooling.shape[1].value sess = tf.Session() with sess.as_default(): recurrence_plot_image = tf.reshape( recurrence_plot_max_pooling, (image_dimension, image_dimension)).eval() print(np.max(recurrence_plot_image)) recurrence_plot_image_array = np.zeros( (image_dimension, image_dimension, 3), dtype=np.uint8) recurrence_plot_image_array[:, :, 0] = recurrence_plot_image recurrence_plot_image_array[:, :, 1] = recurrence_plot_image recurrence_plot_image_array[:, :, 2] = recurrence_plot_image image = fromarray(recurrence_plot_image_array) image.save(mrp_image_path)
def _prompt(img: np.ndarray, h: Text, value: Text, similarity: float) -> Text: if g.prompt_disabled: LOGGER.warning( "using low similarity label: hash=%s, value=%s, similarity", h, value, similarity, ) return value ret = "" close_img = imagetools.show(fromarray(_pad_img(img)), h) try: while len(ret) != 1: ans = "" while value and ans not in ("Y", "N"): ans = terminal.prompt( f"Matching current displaying image: value={value}, similarity={similarity:0.3f}.\n" "Is this correct? (Y/N)").upper() if ans == "Y": ret = value else: ret = terminal.prompt( "Corresponding text for current displaying image:") finally: close_img() _label(h, ret) LOGGER.info("labeled: hash=%s, value=%s", h, ret) return ret
def pil_scale(orig: Image, w: int = None, h: int = None) -> Image: """ Scale a Pillow image :param orig: ndarray Original cv2 image :param w: New width :param h: New height :return: ndarray """ new_width, new_height = _calculate_scale(orig.width, orig.height, w, h) # thumb = orig.copy() # thumb.thumbnail((new_width, new_height)) # also allows enlarging: if orig.mode.startswith('I'): # workaround for Pillow#4402: arr = np_array(orig) if arr.dtype.kind == 'i': # signed integer is *not* trustworthy in this context # (usually a mistake in the array interface) arr.dtype = np_dtype('u' + arr.dtype.name) if arr.dtype.kind == 'u': # integer needs to be scaled linearly to 8 bit # of course, an image might actually have some lower range # (e.g. 10-bit in I;16 or 20-bit in I or 4-bit in L), # but that would be guessing anyway, so here don't # make assumptions on _scale_, just reduce _precision_ arr = arr >> 8 * (arr.dtype.itemsize - 1) arr = arr.astype(np_uint8) elif arr.dtype.kind == 'f': # float needs to be scaled from [0,1.0] to [0,255] arr *= 255 arr = arr.astype(np_uint8) orig = fromarray(arr) thumb = orig.resize((new_width, new_height)) return thumb
def normalize_and_analyze_image(self, image_array, filename_appendix): """ For an image array (as produced by the camera), optimize brightness and contrast. Store the image in the reference image directory. Then use ORB for keypoint detection and descriptor computation. :param image_array: Numpy array with image as produced by the camera object. :param filename_appendix: String to be appended to filename. The filename begins with the current time (hours, minutes, seconds) for later reference. :return: tuple with four objects: the normalized image array, the image object, the keypoints, and the keypoint descriptors. """ # height, width = image_array.shape[:2] # Optimize the contrast in the image. normalized_image_array = self.clahe.apply(image_array) # Version for tests: use image (already normalized) stored at last session. # normalized_image_array = image_array # Build the normalized image from the luminance channel. normalized_image = fromarray(normalized_image_array, 'L') # Write the normalized image to disk. normalized_image_filename = self.build_filename() + filename_appendix normalized_image.save(normalized_image_filename) if self.configuration.protocol_level > 2: Miscellaneous.protocol("Still image '" + filename_appendix + " captured for auto-alignment.") # Use the ORB for keypoint detection normalized_image_kp = self.orb.detect(normalized_image_array, None) # Compute the descriptors with ORB normalized_image_kp, normalized_image_des = self.orb.compute(normalized_image_array, normalized_image_kp) return normalized_image_array, normalized_image, normalized_image_kp, normalized_image_des
def load_dicoms(directory): lstFilesDCM = [] # create an empty list for dirName, subdirList, fileList in os.walk(directory): for filename in fileList: # if ".dcm" in filename.lower(): # check whether the file's DICOM if "" in filename.lower(): lstFilesDCM.append(os.path.join(dirName, filename)) # Get ref file RefDs = pydicom.dcmread(lstFilesDCM[1]) ConstPixelDims = (len(lstFilesDCM), int(RefDs.Rows), int(RefDs.Columns)) # Load spacing values (in mm) ArrayDicom = np.zeros(ConstPixelDims, dtype=RefDs.pixel_array.dtype) ArrayBNP = [] # loop through all the DICOM files for filenameDCM in lstFilesDCM: # read the file ds = pydicom.dcmread(filenameDCM) # store the raw image data ArrayDicom[lstFilesDCM.index(filenameDCM), :, :] = ds.pixel_array im = fromarray(ds.pixel_array) im = im.convert('I') # ArrayBNP[lstFilesDCM.index(filenameDCM), :, :] = im ArrayBNP.append(ds.pixel_array / 255.0) return ArrayDicom, ArrayBNP, lstFilesDCM
def BlurEnhance(self, ThreGray, Iter): pix = array(self.__im) pix = sin(pix * pi / (2 * 255)) for i in arange(Iter): pix = (sin(pi * (pix - ThreGray)) + 1) / 2 pix = 255 * arcsin(pix**sqrt(Iter)) * 2 / pi self.__im = fromarray(pix)
def text_padding(path, max_width=2500, max_height=2100, color=(255, ) * 3): img = cv2.imread(path) height, width, _ = img.shape height_pad = cv2.copyMakeBorder(img, (max_height - height) // 2, (max_height - height + 1) // 2, 0, 0, cv2.BORDER_WRAP) # Get text regions gray = cv2.cvtColor(height_pad, cv2.COLOR_BGR2GRAY) th, threshed = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (500, 100)) morphed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, kernel) cnts = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] # copy regions at the end of lines width_pad = cv2.copyMakeBorder(height_pad, 0, 0, 0, (max_width - width), cv2.BORDER_CONSTANT, value=color) for c in cnts: if np.prod(c.shape) < 100: continue x, y, w, h = cv2.boundingRect(c) ROI = width_pad[y:y + h, x:x + w] pad = ROI[:, 0:width_pad.shape[1] - ROI.shape[1]].copy() width_pad[y:y + h, w:x + w + pad.shape[1]] = pad.copy() return fromarray(width_pad)
def visualize_cifar10(indices): (x_train, _), _ = cifar10.load_data() cols = 4 rows = ceil(len(indices)/float(cols)) for i in range(len(indices)): plt.subplot(rows, cols, i + 1) plt.imshow(fromarray(x_train[indices[i]]))
def draw_text( data, text, color = 255, pos = 'lr' ): from PIL.Image import fromarray from PIL.ImageDraw import Draw from PIL import ImageFont from numpy import asarray font = ImageFont.load_default() image = fromarray( data ) draw = Draw( image ) w, h = draw.textsize( text, font = font ) position = { 'ul': lambda iw, ih, tw, th: ( 2, 0 ), 'ur': lambda iw, ih, tw, th: ( iw - tw - 2, 0 ), 'll': lambda iw, ih, tw, th: ( 2, ih - th ), 'lr': lambda iw, ih, tw, th: ( iw - tw - 2, ih - th ), } pos = position[ pos ]( data.shape[ 1 ], data.shape[ 0 ], w, h ) draw.text( pos, text, fill = color, font = font ) del draw return asarray( image )
def text_padding3(img, max_width=2500, max_height=2100, color=(255, ) * 3): height, width = img.shape height_pad = cv2.copyMakeBorder(img, (max_height - height) // 2, (max_height - height + 1) // 2, 0, (max_width - width), cv2.BORDER_WRAP) return fromarray(height_pad)
def visualize_latents(X: Tensor, edge: int) -> Image: """Visualize sampled points from latent space. It forms image square lattice of size edge x edge. :param X: Datapoints sampled from latent space. :param edge: Number of images along both X and Y axis. :return: Image object with painted datapoints. """ # Compute height of a single image. Width of image in FreyFace or MNIST # dataset is known. height = 28 width = X.shape[1] // height # Fill canvas with sampled images. X = X.detach().numpy() X = X.reshape(-1, height, width) img = zeros((height * edge, width * edge)) for i in range(edge): for j in range(edge): index = i + edge * j slice_y = slice(height * i, height * (i + 1)) slice_x = slice(width * j, width * (j + 1)) img[slice_y, slice_x] = X[index, :, :] return fromarray(uint8(img * 255))
def GetIm(id=1, imname='', imcount=1): conn = Connection(host="localhost", user="******", passwd="123456", charset="utf8") cursor = conn.cursor() cursor.execute('''USE fvrt;''') if imname == '' and imcount == 1: # 从fvrt数据库的image表中获得id指定的不指名图像 cursor.execute('''SELECT imdata FROM image WHERE id = %s;''', (id, )) else: # 从fvrt数据库的image表中获得某个名称指定的第n个图像 cursor.execute( '''SELECT imdata FROM image WHERE imname = %s AND imcount = %s;''', (imname, imcount)) imdataStr = cursor.fetchone()[0] conn.commit() cursor.close() conn.close() imdataStr = imdataStr.decode("utf-8") imdataInt = [] for i in imdataStr.split('/'): imdataSlice = map(lambda x: int(x), i.split(',')) imdataInt.append(list(imdataSlice)) return fromarray(asarray(imdataInt))
def visualize_reconstruction(x: Tensor, X: Tensor, noimages: int=10) -> Image: """The function takes several first images, plot them, and save to png-file. :param x: Original datapoint. :param X: Reconstructed datapoint. :param noimages: Number of images to plot. :return: Image object. """ height = 28 # Both FreyFace and MNIST datasets has the same width. width = x.shape[1] // height noimages = min(noimages, x.shape[0]) # Adjust to batch size. # Preprocess original images. x = x[:noimages, :].detach().numpy() # type: ndarray x = x.reshape(noimages, height, width) x = swapaxes(x, 0, 1) # Preprocess reconstruction images. X = X[:noimages, :].detach().numpy() # type: ndarray X = X.reshape(noimages, height, width) X = swapaxes(X, 0, 1) # Collate small images in a single canvas. img = zeros((2 * height, noimages * width)) # type: ndarray img[:height, :] = x.reshape(height, -1) img[height:, :] = X.reshape(height, -1) return fromarray(uint8(img * 255))
def send(): if 'file[]' not in request.files: flash('No file part') return redirect('/') anonymizer = dicognito.anonymizer.Anonymizer() folder = datetime.now().strftime("%Y%m%d%H%M%S") dir = join(app.config['UPLOAD_FOLDER'], folder) os.makedirs(dir) jpgfiles = [] for i, file in enumerate(request.files.getlist('file[]'), start=1): if file and allowed_file(file.filename): with pydicom.dcmread(file) as dataset: filename = str(i).zfill(4) dcmfile = join(dir, filename + '.dcm') anonymizer.anonymize(dataset) dataset.save_as(dcmfile) im = fromarray(dataset.pixel_array).convert('RGB') jpgfiles.append(join('upload', folder, filename + '.jpg')) im.save(join(dir, filename + '.jpg')) else: flash('File type not supported') else: flash('{} files successfully uploaded'.format(i)) return render_template('home.html', files=jpgfiles)
def put(self, fn: str, data, max_size=1024): fp = self.get_path(fn) # logger = logging.getLogger(self.name) if fn.endswith(".jpg") or fn.endswith(".jpeg"): logging.warning("JPEG compression requires additional libraries and is untested") elif fn.endswith(".png"): # Convert to 8-bit data = self.squash_to_8bit(data) if not os.path.exists( os.path.dirname(fp) ): os.makedirs(os.path.dirname(fp)) try: im = fromarray(data) except TypeError as e: logging.error(e) logging.warning("Skipping file") return # logging.debug(im) if np.max(im.size) > max_size: logging.debug("Resizing") # logging.debug(data.shape) _max = np.max(im.size) new_size = np.int32((im.size / _max) * max_size) # logging.debug(new_size) im = im.resize(new_size) # logging.debug(im) im.save(fp)
def determine_coords(self, img1, img2, tau): """ Changing color in each triangle First, the mask for start and end triangles are computed After that, I change the colors of the first triangle corresponding to the second, calling self.update_images """ arr_img1 = np.array(img1, dtype=np.float64) arr_img2 = np.array(img2, dtype=np.float64) for start, end in self.match.items(): start_triangle = np.array(start) end_triangle = np.array(end) self.start_coords.update({ start: fillConvexPoly(np.zeros((self.size, self.size)), start_triangle, 1) }) local_end = copy(end_triangle) for i in range(3): local_end[i][0] -= 512 self.end_coords.update({ end: fillConvexPoly(np.zeros((self.size, self.size)), local_end, 1) }) self._update_image(start, end, local_end, arr_img1, arr_img2, tau) return fromarray(arr_img1.astype(np.uint8))
def draw_text(data, text, color=255, pos='lr'): from PIL.Image import fromarray from PIL.ImageDraw import Draw from PIL import ImageFont from numpy import asarray font = ImageFont.load_default() image = fromarray(data) draw = Draw(image) w, h = draw.textsize(text, font=font) position = { 'ul': lambda iw, ih, tw, th: (2, 0), 'ur': lambda iw, ih, tw, th: (iw - tw - 2, 0), 'll': lambda iw, ih, tw, th: (2, ih - th), 'lr': lambda iw, ih, tw, th: (iw - tw - 2, ih - th), } pos = position[pos](data.shape[1], data.shape[0], w, h) draw.text(pos, text, fill=color, font=font) del draw return asarray(image)
def save_image(self, image): if len(image.shape) == 4: img = squeeze(image, axis=0) img = self.deprocess(img) im = fromarray(img) filename = "final" + str(int(time())) + ".png" im.save('./static/'+filename) return filename
def imshow(img, seg=None, overlay=None, colors=None, opacity=0.5, filename=None, unroll=False, index=None): """ Display a 2D image """ if len(img.shape) not in [2, 3]: print "Invalid image shape: ", img.shape return if filename is None: embed = True else: embed = False img = img.rescale() img = img.thumbnail(seg=seg, overlay=overlay, colors=colors, opacity=opacity, unroll=unroll, index=index).astype("uint8") #if len(img.shape) == 3 and img.shape[2] == 3: # img = rgb(img) if embed == True: # http://stackoverflow.com/questions/26649716/how-to-show-pil-image-in-ipython-notebook b = BytesIO() #fromarray( np.transpose(img.view(np.ndarray), # (1,2,0)).astype('uint8')).save(b, format='png') fromarray(img.view(np.ndarray)).save(b, format='png') return IPython.display.Image(data=b.getvalue()) else: if imwrite(filename, img): return IPython.display.Image(filename=filename, url='file://' + os.path.abspath(filename), embed=False) else: return False
def reduce_size_of_pixels( pixels:List[List[List[float]]], reduced_size:Tuple[int,int], ) -> List[List[List[float]]]: return asarray( fromarray(pixels).resize(reduced_size) )
def tensor_to_image(tensor): tensor = np.array(255 * tensor, dtype=np.uint8) if np.ndim(tensor) > 3: assert tensor.shape[0] == 1 tensor = tensor[0] return fromarray(tensor)
def convert_file(infile, outfile): ds = read_file(infile) pixels = ds.pixel_array if ds[0x0028,0x0004].value == "RGB": pixels = pixels.reshape([pixels.shape[1], pixels.shape[2], 3]) im = fromarray(pixels) im.save(outfile)
def write(self, fn: str, data, path: str = None, explode: Sequence = None): fp = self.fp(fn, path, explode) if not os.path.dirname(fn): os.makedirs(os.path.dirname(fn)) im = fromarray(data) im.save(fp)
def nbimageLCVbak( data, vmin = None, vmax = None, vsym = False, saveas = None, zoom = 1 ): ''' Display raw data as a notebook inline image. Parameters: data: array-like object, two or three dimensions. If three dimensional, first or last dimension must have length 3 or 4 and will be interpreted as color (RGB or RGBA). vmin, vmax, vsym: refer to rerange() saveas: Save image file to disk (optional). Proper file name extension will be appended to the pathname given. [ None ] zoom: amount to scale the image ''' from IPython.display import display, Image, HTML from PIL.Image import fromarray from StringIO import StringIO css_styling() data = rerange( data, vmin, vmax, vsym ) data = data.squeeze() # try to be smart if 3 <= data.shape[ 0 ] <= 4: print 'transposing' data = data.transpose( ( 1, 2, 0 ) ) s = StringIO() fromarray( data ).save( s, 'png' ) if saveas is not None: open( saveas + '.png', 'wb' ).write( s ) display( Image( s.getvalue(),width=data.shape[0]*zoom, height=data.shape[1]*zoom ) ) if vmin == None: vmin = data.min() if vmax == None: vmax = data.max() html = 'Range: [%.1f, %.1f] Dims: [%d, %d]*%.2f' % (vmin, vmax, data.shape[0], data.shape[1], zoom) display( HTML( html ) )
def pil(self, **params_to_override): """Returns a PIL image for this pattern, overriding parameters if provided.""" from PIL.Image import fromarray nchans = self.num_channels() if nchans in [0, 1]: mode, arr = None, self(**params_to_override) arr = (255.0 / arr.max() * (arr - arr.min())).astype(np.uint8) elif nchans in [3,4]: mode = 'RGB' if nchans==3 else 'RGBA' arr = np.dstack(self.channels(**params_to_override).values()[1:]) arr = (255.0*arr).astype(np.uint8) else: raise ValueError("Unsupported number of channels") return fromarray(arr, mode)
def plot(W): """ Plot basis vectors. :param W: Basis matrix of the fitted factorization model. :type W: `numpy.matrix` """ set_cmap('gray') blank = new("L", (225 + 6, 280 + 6)) for i in xrange(5): for j in xrange(5): basis = np.array(W[:, 5 * i + j])[:, 0].reshape((56, 46)) basis = basis / np.max(basis) * 255 basis = 255 - basis ima = fromarray(basis) expand(ima, border = 1, fill = 'black') blank.paste(ima.copy(), (j * 46 + j, i * 56 + i)) imshow(blank) savefig("orl_faces.png")
def plot(W): """ Plot basis vectors. :param W: Basis matrix of the fitted factorization model. :type W: `numpy.matrix` """ set_cmap('gray') blank = new("L", (133 + 6, 133 + 6)) for i in xrange(7): for j in xrange(7): basis = np.array(W[:, 7 * i + j])[:, 0].reshape((19, 19)) basis = basis / np.max(basis) * 255 basis = 255 - basis ima = fromarray(basis) ima = ima.rotate(180) expand(ima, border=1, fill='black') blank.paste(ima.copy(), (j * 19 + j, i * 19 + i)) imshow(blank) savefig("cbcl_faces.png")
def render(self, da, level): xmin, xmax = self.tile_def.x_range ymin, ymax = self.tile_def.y_range extent = xmin, ymin, xmax, ymax tiles = self.tile_def.get_tiles_by_extent(extent, level) for t in tiles: x, y, z, data_extent = t dxmin, dymin, dxmax, dymax = data_extent arr = da.loc[{'x':slice(dxmin, dxmax), 'y':slice(dymin, dymax)}] if 0 in arr.shape: continue img = fromarray(arr.data, 'RGBA') if self.post_render_func: extras = dict(x=x, y=y, z=z) img = self.post_render_func(img, **extras) yield (img, x, y, z)
# start_t = time.clock() # im2 = im.tostring("raw", "L", 0, -1) # print time.clock() - start_t im = numpy.array(im) # ID = gl.glGenTextures(1) gl.glBindTexture(gl.GL_TEXTURE_2D, 1) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_LUMINANCE, im_x, im_y, 0, gl.GL_LUMINANCE, gl.GL_UNSIGNED_BYTE, im) gl.glHint(gl.GL_GENERATE_MIPMAP_HINT, gl.GL_FASTEST) gl.glGenerateMipmap(gl.GL_TEXTURE_2D) # gl.getTexImage(gl.GL_TEXTURE_2D, 0, gl.GL_LUMINANCE, gl.GL_UNSIGNED_BYTE) # z0 = gl.glGetTexImageui(gl.GL_TEXTURE_2D, 0, gl.GL_LUMINANCE) # z1 = gl.glGetTexImageui(gl.GL_TEXTURE_2D, 1, gl.GL_LUMINANCE) # print z0, z1 k = 0 while (im.shape[0] > 512): k+=1 start_t = time.clock() im = gl.glGetTexImageub(gl.GL_TEXTURE_2D, k, gl.GL_LUMINANCE, outputType=numpy.array) print im.shape print time.clock() - start_t img = fromarray(im) # img.save('/tmp/pygl_z'+str(k)+'.jpg')
def to_pil(self, origin='lower'): arr = np.flipud(self.data) if origin == 'lower' else self.data return fromarray(arr, 'RGBA')
#### create probes # In[5]: probe_set = probes(size_of_field=size_of_field) probe_set.masks = probe_set.masks.reshape((probe_set.num_probes, size_of_field, size_of_field)) #### generate a simulated target map # In[6]: spm = sparse_point_maps(seed_point_rows, seed_point_cols, size_of_field, cluster_pref='all', number_of_clusters = K) spm.scatter() target_map = spm.nn_interpolation(dx=[1000]) target_image = fromarray(np.squeeze(target_map).astype('uint8')) target_om = om(target_image) # In[7]: ##sanity check: remove all of the duplicates and count from imagery_psychophysics.src.stirling_maps import stirling_num_of_2nd_kind as snk import itertools k = spm.grouping k.sort() print 'length of list: %d' %(len(list(k for k,_ in itertools.groupby(k)))) print 'official count: %d' %(snk(seed_point_cols*seed_point_rows, K)) # In[8]:
def to_pil(self, origin="lower"): arr = self.img if origin == "lower": arr = np.flipud(arr) return fromarray(arr, "RGBA")
def nbimageLCVbak2( data, vmin = None, vmax = None, vsym = False, saveas = None, zoom = 1, nshades = 256, subDim = (1,1) ): ''' Display raw data as a notebook inline image. Parameters: data: array-like object, two or three dimensions. If three dimensional, first or last dimension must have length 3 or 4 and will be interpreted as color (RGB or RGBA). vmin, vmax, vsym: refer to rerange() saveas: Save image file to disk (optional). Proper file name extension will be appended to the pathname given. [ None ] zoom: amount to scale the image ''' from IPython.display import display, Image, HTML from PIL.Image import fromarray from StringIO import StringIO import base64 from PyQt4 import QtGui from PyQt4 import QtCore import numpy data = rerange( data, vmin, vmax, vsym ) data = data.squeeze() # try to be smart if 3 <= data.shape[ 0 ] <= 4: print 'transposing' data = data.transpose( ( 1, 2, 0 ) ) s = StringIO() fromarray( data ).save( s, 'png' ) if saveas is not None: open( saveas + '.png', 'wb' ).write( s ) #display( Image( s.getvalue(),width=data.shape[0]*zoom, # height=data.shape[1]*zoom ) ) matrix = numpy.require(data, numpy.uint8, 'C') (w, h) = matrix.shape print matrix qim = QtGui.QImage(matrix.data, w, h, QtGui.QImage.Format_Indexed8) qim.ndarray = matrix # do we need this? # make colormap incr = (256/nshades)+1 colors = range(0,255,(256/nshades)+1) colors[-1] = 255 colctr = -1 for i in range(256): if i % incr == 0: colctr += 1 qim.setColor(i, QtGui.QColor(colors[colctr], colors[colctr], colors[colctr]).rgb()) # zoom dims = (matrix.shape[0]*zoom, matrix.shape[1]*zoom) qim = qim.scaled(dims[0], dims[1]) ba = QtCore.QByteArray() buf = QtCore.QBuffer(ba) buf.open(QtCore.QIODevice.WriteOnly) qim.save(buf, 'PNG') base64_data = ba.toBase64().data() im = 'data:image/png;base64,' + base64_data head = """<table border=0><tr><th><center>""" foot = """</center></th></tr></table>""" s = """<img src="%s"/>"""%(im) if vmin == None: vmin = data.min() if vmax == None: vmax = data.max() html = 'Range: [%.1f, %.1f]<br>Dims: [%d, %d]*%.2f' % (vmin, vmax, data.shape[0], data.shape[1], zoom) display( HTML ( head + s + html + foot ) )
def to_pil(self, origin="lower"): arr = np.flipud(self.data) if origin == "lower" else self.data return fromarray(arr, "RGBA")