def liner(): "提取眼睛行(带眼镜)" #import os #try: os.mkdir('glassline') #except: pass import PIL.Image glassmodel = np.empty((len(glasslist),(70-25)*(90-0)),np.uint8) idx = 0 for i in glasslist: img = PIL.Image.open(s2o(dataset.read(i))) img=img.crop((0,25,90,70)) glassmodel[idx] = misc.fromimage(img).flatten() #img.save('glassline\\'+i.split('/')[-1]) print i idx+=1 print glassmodel.shape np.save('glassline.npy',glassmodel) nglassmodel = np.empty((len(noglasslist),(70-25)*(90-0)),np.uint8) idx = 0 for i in noglasslist: img = PIL.Image.open(s2o(dataset.read(i))) img=img.crop((0,25,90,70)) nglassmodel[idx] = misc.fromimage(img).flatten() #img.save('glassline\\'+i.split('/')[-1]) print i idx+=1 print nglassmodel.shape np.save('nglassline.npy',nglassmodel)
def original_color_transform(content, generated): generated = fromimage(toimage(generated, mode='RGB'), mode='YCbCr') # Convert to YCbCr color space generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr generated = fromimage(toimage(generated, mode='YCbCr'), mode='RGB') # Convert to RGB color space return generated
def _init_images(self, inputs, convert=False): """Initialize Input Image Paths and Image Array. This is a inner function that should not be used outside of this Class. Every time you do this, images will be recalculated based on the input image directory. Return: images: image dict: {path, image array} """ def _preprocess_batch(img): return ([i[0] for i in img], np.asarray([i[1] for i in img])) if convert == True: images = [[ os.path.join(d, filename), misc.fromimage(Image.open(os.path.join(d,filename)).convert('RGB').resize((self.height, self.width), Image.ANTIALIAS)) ] for d,_,fList in os.walk(inputs) \ for filename in fList \ if fnmatch.fnmatch(filename, '*.jpg')] else: images = [[ os.path.join(d, filename), misc.fromimage(Image.open(os.path.join(d,filename))) ] for d,_,fList in os.walk(inputs) \ for filename in fList \ if fnmatch.fnmatch(filename, '*.jpg')] return [ _preprocess_batch(images[i:i + self.extract_image_batch]) for i in range(0, len(images), self.extract_image_batch) ]
def original_color_transform(content, generated, mask=None, hist_match=0, mode='YCbCr'): generated = fromimage(toimage(generated, mode='RGB'), mode=mode) # Convert to YCbCr color space if mask is None: if hist_match == 1: for channel in range(3): generated[:, :, channel] = match_histograms(generated[:, :, channel], content[:, :, channel]) else: generated[:, :, 1:] = content[:, :, 1:] else: width, height, channels = generated.shape for i in range(width): for j in range(height): if mask[i, j] == 1: if hist_match == 1: for channel in range(3): generated[i, j, channel] = match_histograms( generated[i, j, channel], content[i, j, channel]) else: generated[i, j, 1:] = content[i, j, 1:] generated = fromimage(toimage(generated, mode=mode), mode='RGB') # Convert to RGB color space return generated
def __rotate_image(self, matrix, angle=None, precision=1): ''' Rotates the given matrix to a specified angle (in degree). matrix : 2D numpy array [angle] : Rotation angle of the matrix. If none is specified, it will use the previously defined angle [precision] : relative size of the matrix used for the rotation. For example, with a precision set to 0.5, the matrix that will rotate has half the size of the original ''' if angle == None: angle = self.angle # The rotation is done by converting the matrix in a PIL object # and then applying the rotation to it to finally reconvert # into a numpy matrix. # # A mask is generated to reject pixels that are outside the # original image. It finally results in a masked array. Thanks # to that, we can do matrix manipulation without taking care # of suplementary pixels generated by the rotation. tmp_image = misc.toimage(matrix, mode='F') new_size = (numpy.array(tmp_image.size) * precision).round() tmp_image = tmp_image.resize(new_size) tmp_mask = misc.toimage(numpy.ones(new_size).transpose(), mode='F') array_image = misc.fromimage(tmp_image.rotate(angle, expand=1)) array_mask = abs(misc.fromimage(tmp_mask.rotate(angle, expand=1)) - 1) return numpy.ma.array(array_image, mask=array_mask)
def read_captcha( file ): from time import time image = Image.open( file ) image = image.convert( "P" ) image = image.resize( ( image.size[0] * 3, image.size[1] * 3 ), Image.ANTIALIAS ) image2 = Image.new( "P", image.size, 255 ) for x in range( image.size[1] ): for y in range( image.size[0] ): pix = image.getpixel( ( y, x ) ) if pix > 15 and pix < 150: pass image2.putpixel( ( y, x ), 0 ) image2 = image2.convert( 'RGB' ) data = misc.fromimage( image2 ) data_slices = find_paws( 255-data, smooth_radius = 2, threshold = 200 ) draw = ImageDraw.Draw( image2 ) letters = [] bboxes = slice_to_bbox( data_slices ) for bbox in bboxes: xwidth = bbox.x2 - bbox.x1 ywidth = bbox.y2 - bbox.y1 if xwidth < 40 and ywidth < 40: draw.rectangle( ( bbox.x1 - 1, bbox.y1 - 1, bbox.x2 + 1, bbox.y2 + 1 ), fill='white' ) elif xwidth > 60 and ywidth > 69: letters.append( ( bbox.x1, bbox.y1, bbox.x2, bbox.y2 ) ) #draw.rectangle( ( bbox.x1 - 1, bbox.y1 - 1, bbox.x2 + 1, bbox.y2 + 1 ), outline='red' ) letters = sorted( letters, key=lambda i: i[0] ) if len( letters ) == 5: i = 0 final_result = [] for letter in letters: i += 1 im = image2.crop( letter ) image = image.convert( "P" ) im = im.resize( ( im.size[0], im.size[1] ), Image.ANTIALIAS ) im = im.filter( ImageFilter.DETAIL ) dt = misc.fromimage( im ) filename = 'resources/%s-%d.png' % ( file, i ) im = misc.toimage( dt ) im.save( filename ) tempFile = tempfile.NamedTemporaryFile( delete = False ) process = subprocess.Popen(['tesseract', filename, tempFile.name, '-psm', '10', 'letters' ], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) process.communicate() final_result.append( open( tempFile.name + '.txt', 'r' ).read() ) return ''.join( [ l.upper().strip() for l in final_result ] )
def extract(watermarked_filename): # Key input key = "N;fpr-y7hrcMste4" # Load cover image (array) watermarked_array = misc.fromimage(Image.open(watermarked_filename)) # Blue plane B = watermarked_array[:, :, 2] # Initial values la, QF = -1, 77.0 vac = [] vac_aux = [] list_param = [0.27, 0.33456289967845987, 1.99989888346577778, 8] x0, p = list_param[1], list_param[2] secrete_bits = "" # Instances dt = Dtop(13, 13) blocks_instance = BlocksImage(B) # Creating zero matrix dims = (blocks_instance.max_num_blocks(), 64) matrix_vac = np.zeros(dims) # Sub-keys Lkey = div_key(key) # Key expansion from sha512 key_expansion = sha512_bin(Lkey[0]) + sha512_bin(Lkey[1]) # Collecting AC coefficients for i in range(blocks_instance.max_num_blocks()): DCT_Coef = dt.dtop2(blocks_instance.get_block(i)) matrix_vac[i, :] = vzig_zag_scan(quantpq(DCT_Coef, QF)) vac.extend(matrix_vac[i, :][1:9]) key_expansion = increase_string(key_expansion, len(vac)) # Dividing into bloks of length 9 N = len(vac) // 64 # Extracting secrete bits for i in range(N): j = i * 64 k = (i + 1) * 64 vacp = vac[j:k] pos = permuted_pos(key_expansion[j:k], x0, p) for r in range(len(vacp)): secrete_bits += ext_lsb(abs(round(vacp[pos[r]]))) # Conformando QR wh = int(math.sqrt(len(secrete_bits))) extract_image = Image.new("1", (wh, wh), 255) array_extract_image = misc.fromimage(extract_image) for i in range(wh): for y in range(wh): if secrete_bits[wh*i+y] == '0': array_extract_image[i, y] = 0 myqr = MyQR62() return misc.toimage(myqr.get_resconstructed(array_extract_image))
def dat2(self, inputImage): s = inputImage.size input_array = misc.fromimage(inputImage) outputImage = Image.new('L', s) output_array = misc.fromimage(outputImage) for x in range(s[1]): for y in range(s[0]): output_array[(x + y) % s[1], (2 * x + y) % s[0]] = input_array[x, y] return misc.toimage(output_array)
def _loadImageY(self, path, is_noise): im = Image.open(path).convert('YCbCr') if is_noise: im = misc.fromimage(im).astype('float32') else: im = misc.fromimage(im.resize((2*im.size[0], 2*im.size[1]), resample=Image.NEAREST)).astype('float32') x = np.reshape(np.array(im[:,:,0]), (1, 1, im.shape[0], im.shape[1])) / 255.0 return im, x
def _loadImageRGB(self, path, is_noise): im = Image.open(path) if is_noise: im = misc.fromimage(im).astype('float32') else: im = misc.fromimage(im.resize((2*im.size[0], 2*im.size[1]), resample=Image.NEAREST)).astype('float32') x = np.array([[im[:,:,0], im[:,:,1], im[:,:,2]]])/255.0 return im, x
def _loadImageRGB(self, path, is_noise): im = Image.open(path) if is_noise: im = misc.fromimage(im).astype('float32') else: im = misc.fromimage( im.resize((2 * im.size[0], 2 * im.size[1]), resample=Image.NEAREST)).astype('float32') x = np.array([[im[:, :, 0], im[:, :, 1], im[:, :, 2]]]) / 255.0 return im, x
def _loadImageY(self, path, is_noise): im = Image.open(path).convert('YCbCr') if is_noise: im = misc.fromimage(im).astype('float32') else: im = misc.fromimage( im.resize((2 * im.size[0], 2 * im.size[1]), resample=Image.NEAREST)).astype('float32') x = np.reshape(np.array(im[:, :, 0]), (1, 1, im.shape[0], im.shape[1])) / 255.0 return im, x
def original_color_transform(content, generated): ''' Applies the color space of content image to the generated image Args: content: input image of shape (img_width, img_height, 3) generated: input image of shape (img_width, img_height, 3) Returns: image of same shape as input shape ''' generated = fromimage(toimage(generated), mode='YCbCr') # Convert to YCbCr color space generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr generated = fromimage(toimage(generated, mode='YCbCr'), mode='RGB') # Convert to RGB color space return generated
def original_color_transform(content, generated, mask=None): generated = fromimage(toimage(generated, mode='RGB'), mode='YCbCr') # Convert to YCbCr color space if mask is None: generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr else: width, height, channels = generated.shape for i in range(width): for j in range(height): if mask[i, j] == 1: generated[i, j, 1:] = content[i, j, 1:] generated = fromimage(toimage(generated, mode='YCbCr'), mode='RGB') # Convert to RGB color space return generated
def flickr_nn(folder_name, pd, num_nn): files = glob(folder_name + "*.jpeg") for file in files: #run nearest neighbors, get the 100 nearest neighbors for each image # and copy them to the right location # along wi,th the tag files print "opening image" im = Image.open(file) im2 = fromimage(im.resize((32, 32), Image.ANTIALIAS)) print "performing nearest neighbors" filenames = pd.knn(im2, num_nn) mydir = file.split(".")[0] try: os.mkdir(mydir) except: print "directory already exists" for my_match_file in filenames: my_match_file = my_match_file.replace("_pca.pck", ".jpeg") shutil.copy(my_match_file, mydir+"/"+my_match_file.split("/")[-1]) my_match_file = my_match_file.replace("FlickrResized", "FlickrImages") tofile = my_match_file.split("/")[-1].split(".")[-2] shutil.copy(my_match_file, mydir+"/"+tofile+"_small.jpeg") my_match_file = my_match_file.replace(".jpeg", "_tags.txt") my_match_file = my_match_file.replace("FlickrImages", "FlickrNotes") try: shutil.copy(my_match_file, mydir+"/"+my_match_file.split("/")[-1]) except: print "no tag files"
def tst_fromimage(filename, irange): fp = open(filename, "rb") img = misc.fromimage(PIL.Image.open(fp)) fp.close() imin, imax = irange assert_(img.min() >= imin) assert_(img.max() <= imax)
def snapshot(image=None, size=None): r"""Acquires an image as a numpy array. If the image argument is None, a screenshot is grabbed. otherwise, the given image is converted to a 2- or 3-dimensional numpy array, depending on whether it's colour or grayscale. """ if isinstance(image, ndarray): return image if image == None: try: from ImageGrab import grab image = grab() image.save("screenshot.png") except: from os import system name = "screenshot.png" command = "scrot %s" % name system(command) image = loadimage(name) elif isinstance(image, basestring): image = loadimage(image) image.load() if size != None: (m, n) = size image = image.resize((n, m), ANTIALIAS) return dstack(fromimage(channel) for channel in image.split())
def im2ar( image_ ): """Convert PIL Image to Numpy array.""" if image_.mode in ('L', 'I', 'F'): # Warning: only works with PIL.Image.Image whose mode is 'L', 'I' or 'F' # => error if mode == 'I;16' for instance array_ = fromimage( image_ ) return array_
def get_dlib_face_regions(self): """ Detect face regions with DLIB :return: list of Regions """ self._assert_image_loaded() face_regions = [] detector = dlib.get_frontal_face_detector() for zoom in DLIB_ZOOM: strip = self.panorama_img.crop((0, 1975, PANORAMA_WIDTH, 2600)) zoomed_size = (int(zoom * PANORAMA_WIDTH), int(zoom * 625)) zoomed = strip.resize(zoomed_size, Image.BICUBIC) detected_faces, _, _ = detector.run(misc.fromimage(zoomed), DLIB_UPSCALE, DLIB_THRESHOLD) regions = [] for d in detected_faces: regions.append((d.left(), d.top(), d.right() - d.left(), d.bottom() - d.top())) derived = derive(regions, 0, 1975, zoom, 'dlib', DLIB_UPSCALE, '>{}'.format(DLIB_THRESHOLD)) face_regions.extend(derived) return face_regions
def _compress(data, quality=75): data_shape = np.shape(data) is_l = data_shape[-1] == 1 if is_l: data = np.squeeze(data, axis=3) buffer_fp = BytesIO() tmp_data = [] for cur_data in data: buffer_fp.truncate() buffer_fp.seek(0) cur_img = Image.fromarray(cur_data) cur_img.save(buffer_fp, format='jpeg', quality=quality) # buffer = buffer_fp.getbuffer() compressed_img = Image.open(buffer_fp) #compressed_img.show() tmp_data.append(misc.fromimage(compressed_img)) data = np.asarray(tmp_data) if is_l: data = np.expand_dims(data, axis=3) #data = data[:, :, :, np.newaxis] return data
def find_dominant_colors(image): """Cluster the colors of the image in CLUSTER_NUMBER of clusters. Returns an array of dominant colors reverse sorted by cluster size. """ array = img_as_float(fromimage(image)) # Reshape from MxNx4 to Mx4 array array = array.reshape(scipy.product(array.shape[:2]), array.shape[2]) # Remove transparent pixels if any (channel 4 is alpha) if array.shape[-1] > 3: array = array[array[:, 3] == 1] # Finding centroids (centroids are colors) centroids, _ = kmeans(array, CLUSTER_NUMBER) # Allocate pixel to a centroid cluster observations, _ = vq(array, centroids) # Calculate the number of pixels in a cluster histogram, _ = scipy.histogram(observations, len(centroids)) # Sort centroids by number of pixels in their cluster sorted_centroids = sorted(zip(centroids, histogram), key=lambda x: x[1], reverse=True) sorted_colors = tuple((couple[0] for couple in sorted_centroids)) return sorted_colors
def original_color_transform(content, generated, mask=None): generated = fromimage(toimage(generated, mode="RGB"), mode="YCbCr") if mask is None: generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr else: width, height, channels = generated.shape for i in range(width): for j in range(height): if mask[i, j] == 1: generated[i, j, 1:] = content[i, j, 1:] generated = fromimage(toimage(generated, mode="YCbCr"), mode="RGB") return generated
def resize(data, dims): """ Wrapper to resize an image """ import scipy.misc as smp import Image tmp = smp.fromimage(smp.toimage(data, mode='F')) tmp = tmp.resize(dims, Image.ANTIALIAS) return tmp
def tst_fromimage(filename, irange): fp = open(filename, "rb") img = misc.fromimage(PIL.Image.open(fp)) fp.close() imin,imax = irange assert_(img.min() >= imin) assert_(img.max() <= imax)
def prepare_image_for_correlation(im): letterArray = fromimage(im.convert('RGB')) # Black and white letterArray = scipy.inner(letterArray, [299, 587, 114]) / 1000.0 # Normalize letterArray = (letterArray - letterArray.mean()) / letterArray.std() return letterArray
def transform(single): simage = misc.toimage(single) timg = PIL.Image.new(simage.mode,simage.size) timg.paste(simage.resize((int(simage.size[0]*scale),int(simage.size[1]*scale)),PIL.Image.BICUBIC),(int(dx-simage.size[0]*(scale-1)*0.5),int(dy-simage.size[1]*(scale-1)*0.5))) simage = timg simage = simage.rotate(angle,PIL.Image.BICUBIC,expand=False) return misc.fromimage(simage)
def fonts_template(fn=None, ttf=None): ttf = ttf or 'c:/windows/fonts/ariali.ttf' fn = fn or 'd:/temp/fonts.arrs' m = dict() font = ImageFont.truetype(ttf, 18) for e in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ': arr = font.getmask(e, mode='L') arr = Image.Image()._new(arr) arr = misc.fromimage(arr) h, w = arr.shape print '%s:<%s,%s> %s' % (e, h, w, arr[0, 0]) if w < 10: tmp = numpy.ndarray((h, 10), dtype=arr.dtype) tmp.fill(0) i = (10 - w) / 2 tmp[:, i:i + w] = arr arr = tmp #arr = arr[3:18,:] arr = arr[2:19, :] arr = im_norm(arr) rs = ndimage.correlate(arr, arr, mode='constant', cval=0.0) limit = numpy.max(rs) m[e] = (limit, arr) cPickle.dump(m, open(fn, 'wb'))
def scale_colors(images: np.ndarray, nchannels=3): try: orig_shape = images.shape assert images.shape[-1] == nchannels if images.dtype == np.uint: return images from scipy.misc import toimage, fromimage def prep(img): return fromimage(toimage(np.clip(img, 0, 255), mode='RGB')) if images.ndim == 3: return fromimage(toimage(np.clip(images, 0, 255))) images = images.copy() res = np.zeros(images.shape, dtype=np.uint8) res.shape = (-1,) + images.shape[-nchannels:] images.shape = res.shape N = res.shape[0] for i in range(N): res[i] = prep(images[i]) res.shape = orig_shape return res except: print('input shape: %s' % orig_shape, file=sys.stderr) raise
def fonts_template(fn=None,ttf=None): ttf = ttf or 'c:/windows/fonts/ariali.ttf' fn = fn or 'd:/temp/fonts.arrs' m = dict() font = ImageFont.truetype(ttf, 18) for e in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ': arr = font.getmask(e, mode='L') arr = Image.Image()._new(arr) arr = misc.fromimage(arr) h,w = arr.shape print '%s:<%s,%s> %s'%(e, h,w, arr[0,0]) if w<10: tmp = numpy.ndarray( (h,10), dtype=arr.dtype ) tmp.fill(0) i = (10-w)/2 tmp[:,i:i+w] = arr arr = tmp #arr = arr[3:18,:] arr = arr[2:19,:] arr = im_norm(arr) rs = ndimage.correlate(arr,arr, mode='constant', cval=0.0) limit = numpy.max(rs) m[e] = (limit,arr) cPickle.dump(m, open(fn,'wb'))
def load_edge(self, img, index, mask): sigma = self.sigma # in test mode images are masked (with masked regions), # using 'mask' parameter prevents canny to detect edges for the masked regions mask = None if self.training else (1 - mask / 255).astype(np.bool) # canny if self.edge == 1: # no edge if sigma == -1: return np.zeros(img.shape).astype(np.float) # random sigma if sigma == 0: sigma = random.randint(1, 4) return canny(img, sigma=sigma, mask=mask).astype(np.float) # external else: imgh, imgw = img.shape[0:2] edge = Image.open(self.edge_data[index]).resize((imgh, imgw),Image.ANTIALIAS) edge = fromimage(edge) # non-max suppression if self.nms == 1: edge = edge * canny(img, sigma=sigma, mask=mask) return edge
def extract(self, watermarked_image): ''' Retorna la imagen marcada con los pixeles modificados de color verde ''' # Generate R1 and R2 self.M, self.N = watermarked_image.size self.generateR1andR2(self.N, self.M) # Dividiendo en componentes RGB r, g, b = watermarked_image.split() # Bloques modificados en componente Red bmr = self.extractEnComponente(r) # Bloques modificados en componente Green bmg = self.extractEnComponente(g) # Bloques modificados en componente Blue bmb = self.extractEnComponente(b) bm = list(set(bmr + bmg + bmb)) # Dividing in 1x1 blocks watermarked_array = misc.fromimage(watermarked_image) y = len(watermarked_array) x = len(watermarked_array[0]) for item in bm: fila = item // x columna = item % x watermarked_array[fila, columna] = [0, 255, 0] return misc.toimage(watermarked_array)
def threshold2(frame, threshold): '''Returns a list of the pixel indicies of all of the pixels in the image whose value is below the given threshold. Requires 'im_array' to be a 2D bitmap array of numbers (for our purposes these numbers represent an 8-bit image and therefore take on values from 0-255.)''' points = [] frame = frame.point(lambda p: p < threshold) im_array = fromimage(frame).astype('float') try: B = argwhere(im_array) (ystart, xstart), (ystop, xstop) = B.min(0), B.max(0) + 1 cropped_array = im_array[ystart:ystop, xstart:xstop] p = array(nonzero(cropped_array)).swapaxes(0,1) for (y,x) in p: points.append((xstart+x,ystart+y)) except ValueError: pass return points
def getRGB(filename): im = Image.open(filename) width = im.size[0] height = im.size[1] im = im.convert('RGB') data = fromimage(im) return [data, width, height]
def extract(self, watermarked_image): import cv2 # To array watermarked_array = misc.fromimage(watermarked_image) modifiedBlocks = [] # components for i in range(3): component = watermarked_array[:, :, i] m = self.extractFromComponent(component) modifiedBlocks += m modifiedBlocks = list(set(modifiedBlocks)) # Dividing in 32x32 blocks blocks32x32 = BlocksImage(watermarked_array, 32, 32) for item in modifiedBlocks: coord = blocks32x32.get_coord(item) for x in range(32): for y in range(32): watermarked_array[coord[0]+x, coord[1]+y] = [0, 255, 0] cv2.rectangle( watermarked_array, (coord[1], coord[0]), (coord[3], coord[2]), (0, 255, 0), 1) return Image.fromarray(watermarked_array)
def im2ar(image_): """Convert PIL Image to Numpy array.""" if image_.mode in ('L', 'I', 'F'): # Warning: only works with PIL.Image.Image whose mode is 'L', 'I' or 'F' # => error if mode == 'I;16' for instance array_ = fromimage(image_) return array_
def load_and_align_data(pre_detect_images, image_size, margin, gpu_memory_fraction): images = [] for image in pre_detect_images: im = Image.open(BytesIO(base64.b64decode(image['b64_bytes']))) img = misc.fromimage(im, flatten=False, mode='RGB') images.append(img) results = detect_face.bulk_detect_face(images, 0.05, pnet, rnet, onet, threshold, factor) for ctr, res in enumerate(results): pre_detect_images[ctr]['faces'] = [] if not res: continue img_size = np.asarray(images[ctr].shape)[0:2] bounding_boxes, _ = res for box_ctr, bounding_box in enumerate(bounding_boxes): det = np.squeeze(bounding_boxes[box_ctr, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = images[ctr][bb[1]:bb[3], bb[0]:bb[2], :] aligned = misc.imresize( cropped, (image_size, image_size), interp='bilinear') prewhitened = facenet.prewhiten(aligned) pre_detect_images[ctr]['faces'].append({ 'prewhitened': prewhitened, 'bb': bb }) return pre_detect_images
def score(filename): """ Score individual image files for the genetic algorithm. The idea is to derive predictive factors for the langmuir performance (i.e., max power) based on the connectivity, phase fractions, domain sizes, etc. The scoring function should be based on multivariate fits from a database of existing simulations. To ensure good results, use robust regression techniques and cross-validate the best-fit. :param filename: image file name :type filename: str :return score (ideally as an estimated maximum power in W/(m^2)) :rtype float """ # this works around a weird bug in scipy.misc.imread with 1-bit images # open them with PIL as 8-bit greyscale "L" and then convert to ndimage pil_img = Image.open(filename) image = misc.fromimage(pil_img.convert("L")) width, height = image.shape if width != 256 or height != 256: print "Size Error: ", filename # isize = analyze.interface_size(image) ads1, std1 = analyze.average_domain_size(image) # we now need to invert the image to get the second domain size inverted = (image < image.mean()) ads2, std2 = analyze.average_domain_size(inverted) #overall average domain size ads = (ads1 + ads2) / 2.0 # transfer distances # connectivity td1, connect1, td2, connect2 = analyze.transfer_distance(image) spots = np.logical_xor(image, ndimage.binary_erosion(image, structure=np.ones((2,2)))) erosion = np.count_nonzero(spots) spots = np.logical_xor(image, ndimage.binary_dilation(image, structure=np.ones((2,2)))) dilation = np.count_nonzero(spots) # fraction of phase one nonzero = np.count_nonzero(image) fraction = float(nonzero) / float(image.size) # scores zero at 0, 1 and maximum at 0.5 ps = fraction*(1.0-fraction) # from simulations with multivariate nonlinear regression return (-1.98566e8) + (-1650.14)/ads + (-680.92)*math.pow(ads,0.25) + \ 1.56236e7*math.tanh(14.5*(connect1 + 0.4)) + 1.82945e8*math.tanh(14.5*(connect2 + 0.4)) \ + 2231.32*connect1*connect2 \ + (-4.72813)*td1 + (-4.86025)*td2 \ + 3.79109e7*ps**8 \ + 0.0540293*dilation + 0.0700451*erosion
def score(filename): """ Score individual image files for the genetic algorithm. The idea is to derive predictive factors for the langmuir performance (i.e., max power) based on the connectivity, phase fractions, domain sizes, etc. The scoring function should be based on multivariate fits from a database of existing simulations. To ensure good results, use robust regression techniques and cross-validate the best-fit. :param filename: image file name :type filename: str :return score (ideally as an estimated maximum power in W/(m^2)) :rtype float """ # this works around a weird bug in scipy.misc.imread with 1-bit images # open them with PIL as 8-bit greyscale "L" and then convert to ndimage pil_img = Image.open(filename) image = misc.fromimage(pil_img.convert("L")) width, height = image.shape if width != 256 or height != 256: print "Size Error: ", filename # isize = analyze.interface_size(image) ads1, std1 = analyze.average_domain_size(image) # we now need to invert the image to get the second domain size inverted = (image < image.mean()) ads2, std2 = analyze.average_domain_size(inverted) #overall average domain size ads = (ads1 + ads2) / 2.0 # transfer distances # connectivity td1, connect1, td2, connect2 = analyze.transfer_distance(image) spots = np.logical_xor( image, ndimage.binary_erosion(image, structure=np.ones((2, 2)))) erosion = np.count_nonzero(spots) spots = np.logical_xor( image, ndimage.binary_dilation(image, structure=np.ones((2, 2)))) dilation = np.count_nonzero(spots) # fraction of phase one nonzero = np.count_nonzero(image) fraction = float(nonzero) / float(image.size) # scores zero at 0, 1 and maximum at 0.5 ps = fraction * (1.0 - fraction) # from simulations with multivariate nonlinear regression return (-1.98566e8) + (-1650.14)/ads + (-680.92)*math.pow(ads,0.25) + \ 1.56236e7*math.tanh(14.5*(connect1 + 0.4)) + 1.82945e8*math.tanh(14.5*(connect2 + 0.4)) \ + 2231.32*connect1*connect2 \ + (-4.72813)*td1 + (-4.86025)*td2 \ + 3.79109e7*ps**8 \ + 0.0540293*dilation + 0.0700451*erosion
def convert_profile_numpy(image_np, inprof_path, outprof_path, intent_name): if (not have_pilutil) or (not have_cms): return image_np in_image_pil = toimage(image_np) out_image_pil = convert_profile_pil(in_image_pil, inprof_path, outprof_path, intent_name) image_out = fromimage(out_image_pil) return image_out
def tst_fromimage(filename, irange, shape): fp = open(filename, "rb") img = misc.fromimage(PIL.Image.open(fp)) fp.close() imin, imax = irange assert_equal(img.min(), imin) assert_equal(img.max(), imax) assert_equal(img.shape, shape)
def setUp(self): image_name = '../media/kewell1.jpg' self.reference_image_uint8 = fromimage(Image.open(image_name)) self.fv = vivid.ImageSource(imlist=[image_name]) self.cs = vivid.ConvertedSource(self.fv, target_type = vivid.cv.CV_32FC3) self.gs = vivid.GreySource(self.cs)
def convert_profile_numpy_transform(image_np, transform): if (not have_pilutil) or (not have_cms): return image_np in_image_pil = toimage(image_np) convert_profile_pil_transform(in_image_pil, transform, inPlace=True) image_out = fromimage(in_image_pil) return image_out
def check_fromimage(filename, irange, shape): fp = open(filename, "rb") img = misc.fromimage(PIL.Image.open(fp)) fp.close() imin, imax = irange assert_equal(img.min(), imin) assert_equal(img.max(), imax) assert_equal(img.shape, shape)
def img2code(self, ss, alpha=0.4): im = Image.open(StringIO(ss)) im = im.convert('L') arr = misc.fromimage(im) roi,res = self.decode(arr) if len(roi)!=4: return None return ''.join( e[0] for e in roi )
def load_image(inf, spacing=None, medium_index=None, illum_wavelen=None, illum_polarization=None, normals=None, noise_sd=None, channel=None, name=None): """ Load data or results Parameters ---------- inf : single or list of basestring or files File to load. If the file is a yaml file, all other arguments are ignored. If inf is a list of image files or filenames they are all loaded as a a timeseries hologram channel : int or tuple of ints (optional) number(s) of channel to load for a color image (in general 0=red, 1=green, 2=blue) Returns ------- obj : The object loaded, :class:`holopy.core.marray.Image`, or as loaded from yaml """ if name is None: name = os.path.splitext(os.path.split(inf)[-1])[0] with open(inf,'rb') as pi: arr = fromimage(pilimage.open(pi)).astype('d') if hasattr(pi, 'tag') and isinstance(yaml.load(pi.tag[270][0]), dict): warnings.warn("Metadata detected but ignored. Use hp.load to read it") extra_dims = None if channel is None: if arr.ndim > 2: raise BadImage('Not a greyscale image. You must specify which channel(s) to use') elif arr.ndim == 2: if not channel == 'all': warnings.warn("Not a color image (channel number ignored)") pass else: # color image with specified channel(s) if channel == 'all': channel = range(arr.shape[2]) channel = ensure_array(channel) if channel.max() >= arr.shape[2]: raise LoadError(filename, "The image doesn't have a channel number {0}".format(channel.max())) else: arr = arr[:, :, channel].squeeze() if len(channel) > 1: # multiple channels. increase output dimensionality if channel.max() <=2: channel = [['red','green','blue'][c] for c in channel] extra_dims = {illumination: channel} if not is_none(illum_wavelen) and not isinstance(illum_wavelen,dict) and len(ensure_array(illum_wavelen)) == len(channel): illum_wavelen = xr.DataArray(ensure_array(illum_wavelen), dims=illumination, coords=extra_dims) if not isinstance(illum_polarization, dict) and np.array(illum_polarization).ndim == 2: pol_index = xr.DataArray(channel, dims=illumination, name=illumination) illum_polarization=xr.concat([to_vector(pol) for pol in illum_polarization], pol_index) return data_grid(arr, spacing, medium_index, illum_wavelen, illum_polarization, normals, noise_sd, name, extra_dims)
def _read_tiff(filename): """ Reads a TIFF and returns the image as a NumPy array (double precision). Uses tifffile.py (by Christoph Gohlke) to detect size and depth of image. Notes ----- TOFIX: The library doesn't convert our Photon Focus 12-bit tiffs correctly, so we call a special decoder for all 12-bit tiffs (should fix this in the future so that all tiffs can be opened by tifffile) """ tif = TIFFfile(filename) might_be_color = True if len(tif.pages) > 1: try: arr = tif.asarray().transpose() might_be_color = False except Exception: print('failed to read multipage tiff, attempting to read a single page') # assuming a one-page tiff here... depth = tif[0].tags.bits_per_sample.value width = tif[0].tags.image_width.value height = tif[0].tags.image_length.value # I think the "samples per pixel" corresponds to the number of # channels; check on a 24-bit tiff to make sure channels = tif[0].tags.samples_per_pixel.value if depth == 8: tif.close() # use PIL to open it # TOFIX: see if tifffile will open 8-bit tiffs from our # cameras correctly im = PILImage.open(filename) arr = fromimage(im).astype('d') elif depth == 12: tif.close() if width == height: arr = _read_tiff_12bit(filename, height) else: raise NotImplementedError("Read non-square 12 bit tiff") else: # use the tifffile representation arr = tif.asarray().astype('d') tif.close() try: # 270 is the image description tag description = PILImage.open(filename).tag[270] except KeyError: description = "{}" return arr, might_be_color, description
def LoadColorAndGreyscaleImages(path): try: color = misc.imread(path) if len(color.shape) == 3 and color.shape[2] == 3: return color, misc.fromimage(misc.toimage(color), flatten=True) else: return None, None except Exception as e: return None, None
def load_drawn_labels(name): fn = pyhrf.get_data_file_name('simu_labels_%s.png' %name) if not op.exists(fn): raise Exception('Unknown label map %s (%s)' %(name,fn)) from scipy.misc import fromimage from PIL import Image labels = fromimage(Image.open(fn)) return labels[np.newaxis,:,:]
def get_grayscale_tensor(samples): i = 0 grayscales = [] for sample in samples: current_image,label = sample current_image = misc.fromimage(current_image)/255.0 grayscaled = np.dot(current_image[...,:3], [0.299, 0.587, 0.144]) gs = np.reshape(grayscaled,(1024)).tolist() grayscales.append( (gs,label) ) return grayscales
def check_fromimage(filename, irange, shape): fp = open(filename, "rb") with suppress_warnings() as sup: sup.filter(DeprecationWarning) img = misc.fromimage(PIL.Image.open(fp)) fp.close() imin, imax = irange assert_equal(img.min(), imin) assert_equal(img.max(), imax) assert_equal(img.shape, shape)
def im2ar( im_ , im_depth): """Convert PIL Image to Numpy array.""" array_ = list() for m in range(im_depth): image_ = im_[m] if image_.mode in ('L', 'I', 'F'): # Warning: only works with PIL.Image.Image whose mode is 'L', 'I' or 'F' # => error if mode == 'I;16' for instance array_.append(fromimage( image_ )) return array_
def sift(image,keypoints = None, first = -1 , octaves = -1 , levels = initLevels , threshold = -1 , edgeThreshold = 10.0, magnif = 3.0 , noorient = 0 , stableorder = 0 , savegss = 0 , verbose = 0 , binary = 0 , unnormalized = 0 ): from scipy.misc import imread,fromimage if threshold == -1 : threshold = 0.04 / levels / 2. if keypoints is None : keypoints = np.zeros((0,0),'float32') haveKeypoints = False else : haveKeypoints = True if type(keypoints) is not np.ndarray : keypoints = np.array(keypoints) if keypoints.dtype != 'float32' : keypoints = keypoints.astype('float32') nkeypoints = C.c_int(len(keypoints)) if type(image) is str : image = imread(image) elif type(image) is not np.ndarray : image = fromimage(image) if type(image) is str : image = imread(image) if image.ndim > 2 : image = image.mean(2).astype('float32') if image.dtype != 'float32' : image = image.astype('float32') height,width = image.shape res_ = _lib.run_sift(height,width,image, nkeypoints, keypoints, first, octaves, levels, threshold, edgeThreshold, magnif, noorient, stableorder, savegss, verbose,binary, haveKeypoints,unnormalized) nkeypoints = nkeypoints.value res = np.array(res_[:132*nkeypoints]).reshape(nkeypoints,132) _lib.free(res_) return res[:,:4],res[:,4:]
def load_image(handle, shape=None, astype=np.float64): """ Load data from image file. """ pil_img = Image.open(handle) image = misc.fromimage(pil_img.convert("L")) if not shape is None: image = np.reshape(image, shape) return image.astype(astype)
def load_image(filename, channel=None, spacing=None, optics=None): """ Handler for opening various types of image files. Parameters ---------- filename : String name of file to open. channel : int (optional) number of channel in color image (in general 0=red, 1=green, 2=blue), if None, return all colors Returns ------- arr : numpy.array array with data in double precision (type 'd') Raises ------ LoadError if there is a problem loading a file """ # The most reliable way to determine what type of image file we are working # with is to just try opening it with various loaders. Extensions could be # missing or wrong, but, ie, if np.load succeeds, it was a np file try: return np.load(filename) except IOError: pass might_be_color = True try: arr, might_be_color, description = _read_tiff(filename) except (ValueError, TypeError): im = PILImage.open(filename) arr = fromimage(im).astype('d') description = "{}" # pick out only one channel of a color image if channel is not None and len(arr.shape) > 2 and might_be_color: if channel >= arr.shape[2]: raise LoadError(filename, "The image doesn't have a channel number {0}".format(channel)) else: arr = arr[:, :, channel] elif channel > 0: warnings.warn("Warning: not a color image (channel number ignored)") metadata = json.loads(description) return Image(arr, spacing=spacing, optics=optics, metadata=metadata)
def generate_scalar(self): """ breaks the image into a multi-dimensional array -> (ar) then breaks the rows up by NUM_CLUSTERS & color -> (codes) """ ar = fromimage(self.im) shape = ar.shape ar = ar.reshape(product(shape[:2]), shape[2]) float_ar = ar+0.0 codes, dist = kmeans(float_ar, self.NUM_CLUSTERS) return ar, codes
def loadImageAsGreyScale( filename, xscale='1mum', yscale='link2X', fieldunit=1 ): from PIL import Image import scipy from scipy.misc import fromimage from pyphant.core import DataContainer from pyphant.quantities import Quantity im = Image.open(filename) if im.mode == "I;16": im = im.convert("I") data = fromimage(im).astype("int16") else: data = fromimage(im, flatten=True) Ny, Nx = data.shape xUnit = Quantity(xscale.encode('utf-8')) xAxis = DataContainer.FieldContainer(scipy.linspace(0.0, xUnit.value, Nx, True), xUnit / xUnit.value, longname = 'x-coordinate', shortname = 'x') if yscale == 'link2X': yUnit = xUnit * float(Ny) / Nx else: yUnit = Quantity(yscale.encode('utf-8')) yAxis = DataContainer.FieldContainer(scipy.linspace(0.0, yUnit.value, Ny, True), yUnit / yUnit.value, longname = 'y-coordinate', shortname = 'y') try: FieldUnit = Quantity(fieldunit.encode('utf-8')) except AttributeError: FieldUnit = fieldunit return DataContainer.FieldContainer( data, FieldUnit, longname="Image", shortname="I", dimensions=[yAxis, xAxis] )