我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用PIL.Image.fromarray()。
def velocity_ocr(image,coords,f1app): # crop and convert image to greyscale img = Image.fromarray(image).crop(coords).convert('L') img = img.resize([img.width*2,img.height*2]) if f1app: # filters for video from the f1 app img = ImageEnhance.Brightness(img).enhance(3.0) img = ImageEnhance.Contrast(img).enhance(2.0) else: # filters for onboard video graphic img = ImageEnhance.Brightness(img).enhance(0.1) img = ImageEnhance.Contrast(img).enhance(2.0) img = ImageEnhance.Contrast(img).enhance(4.0) img = ImageEnhance.Brightness(img).enhance(0.2) img = ImageEnhance.Contrast(img).enhance(16.0) try: # vel = pytesseract.image_to_string(img,config='digits') vel = pytesseract.image_to_string(img) except UnicodeDecodeError: vel = -1 return vel
def encode_jpeg(arr): assert arr.dtype == np.uint8 # simulate multi-channel array for single channel arrays if len(arr.shape) == 3: arr = np.expand_dims(arr, 3) # add channels to end of x,y,z arr = arr.transpose((3,2,1,0)) # channels, z, y, x reshaped = arr.reshape(arr.shape[3] * arr.shape[2], arr.shape[1] * arr.shape[0]) if arr.shape[0] == 1: img = Image.fromarray(reshaped, mode='L') elif arr.shape[0] == 3: img = Image.fromarray(reshaped, mode='RGB') else: raise ValueError("Number of image channels should be 1 or 3. Got: {}".format(arr.shape[3])) f = io.BytesIO() img.save(f, "JPEG") return f.getvalue()
def quantize_without_scipy(self, image): """" This function can be used if no scipy is availabe. It's 7 times slower though. """ w,h = image.size px = np.asarray(image).copy() memo = {} for j in range(w): for i in range(h): key = (px[i,j,0],px[i,j,1],px[i,j,2]) try: val = memo[key] except KeyError: val = self.convert(*key) memo[key] = val px[i,j,0],px[i,j,1],px[i,j,2] = val return Image.fromarray(px).convert("RGB").quantize(palette=self.paletteImage())
def draw_sequences(i, k, step, action, draw, region_image, background, path_testing_folder, iou, reward, gt_mask, region_mask, image_name, save_boolean): mask = Image.fromarray(255 * gt_mask) mask_img = Image.fromarray(255 * region_mask) image_offset = (1000 * step, 70) text_offset = (1000 * step, 550) masked_image_offset = (1000 * step, 1400) mask_offset = (1000 * step, 700) action_string = string_for_action(action) footnote = 'action: ' + action_string + ' ' + 'reward: ' + str(reward) + ' Iou:' + str(iou) draw.text(text_offset, str(footnote), (0, 0, 0), font=font) img_for_paste = Image.fromarray(region_image) background.paste(img_for_paste, image_offset) background.paste(mask, mask_offset) background.paste(mask_img, masked_image_offset) file_name = path_testing_folder + '/' + image_name + str(i) + '_object_' + str(k) + '.png' if save_boolean == 1: background.save(file_name) return background
def draw_sequences_test(step, action, qval, draw, region_image, background, path_testing_folder, region_mask, image_name, save_boolean): aux = np.asarray(region_image, np.uint8) img_offset = (1000 * step, 70) footnote_offset = (1000 * step, 550) q_predictions_offset = (1000 * step, 500) mask_img_offset = (1000 * step, 700) img_for_paste = Image.fromarray(aux) background.paste(img_for_paste, img_offset) mask_img = Image.fromarray(255 * region_mask) background.paste(mask_img, mask_img_offset) footnote = 'action: ' + str(action) q_val_predictions_text = str(qval) draw.text(footnote_offset, footnote, (0, 0, 0), font=font) draw.text(q_predictions_offset, q_val_predictions_text, (0, 0, 0), font=font) file_name = path_testing_folder + image_name + '.png' if save_boolean == 1: background.save(file_name) return background
def draw_bounding_boxes(image, gt_boxes, im_info): num_boxes = gt_boxes.shape[0] gt_boxes_new = gt_boxes.copy() gt_boxes_new[:,:4] = np.round(gt_boxes_new[:,:4].copy() / im_info[2]) disp_image = Image.fromarray(np.uint8(image[0])) for i in xrange(num_boxes): this_class = int(gt_boxes_new[i, 4]) disp_image = _draw_single_box(disp_image, gt_boxes_new[i, 0], gt_boxes_new[i, 1], gt_boxes_new[i, 2], gt_boxes_new[i, 3], 'N%02d-C%02d' % (i, this_class), FONT, color=STANDARD_COLORS[this_class % NUM_COLORS]) image[0, :] = np.array(disp_image) return image
def show_one_img_mask(data): w,h = 1918,1280 a = randint(0,31) path = "../input/test" data = np.load(data).item() name,masks = data['name'][a],data['pred'] img = Image.open("%s/%s"%(path,name)) #img.show() plt.imshow(img) plt.show() mask = np.squeeze(masks[a]) mask = imresize(mask,[h,w]).astype(np.float32) print(mask.shape,mask[0]) img = Image.fromarray(mask*256)#.resize([w,h]) plt.imshow(img) plt.show()
def Occlusion_exp(image,occluding_size,occluding_stride,model,preprocess,classes,groundTruth): img = np.copy(image) height, width,_= img.shape output_height = int(math.ceil((height-occluding_size)/occluding_stride+1)) output_width = int(math.ceil((width-occluding_size)/occluding_stride+1)) ocludedImages=[] for h in range(output_height): for w in range(output_width): #occluder region h_start = h*occluding_stride w_start = w*occluding_stride h_end = min(height, h_start + occluding_size) w_end = min(width, w_start + occluding_size) input_image = copy.copy(img) input_image[h_start:h_end,w_start:w_end,:] = 0 ocludedImages.append(preprocess(Image.fromarray(input_image))) L = np.empty(output_height*output_width) L.fill(groundTruth) L = torch.from_numpy(L) tensor_images = torch.stack([img for img in ocludedImages]) dataset = torch.utils.data.TensorDataset(tensor_images,L) dataloader = torch.utils.data.DataLoader(dataset,batch_size=5,shuffle=False, num_workers=8) heatmap=np.empty(0) model.eval() for data in dataloader: images, labels = data if use_gpu: images, labels = (images.cuda()), (labels.cuda(async=True)) outputs = model(Variable(images)) m = nn.Softmax() outputs=m(outputs) if use_gpu: outs=outputs.cpu() heatmap = np.concatenate((heatmap,outs[0:outs.size()[0],groundTruth].data.numpy())) return heatmap.reshape((output_height, output_width))
def drawCaption(self, img, caption): img_txt = Image.fromarray(img) # get a font fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50) # get a drawing context d = ImageDraw.Draw(img_txt) # draw text, half opacity d.text((10, 256), 'Stage-I', font=fnt, fill=(255, 255, 255, 255)) d.text((10, 512), 'Stage-II', font=fnt, fill=(255, 255, 255, 255)) if img.shape[0] > 832: d.text((10, 832), 'Stage-I', font=fnt, fill=(255, 255, 255, 255)) d.text((10, 1088), 'Stage-II', font=fnt, fill=(255, 255, 255, 255)) idx = caption.find(' ', 60) if idx == -1: d.text((256, 10), caption, font=fnt, fill=(255, 255, 255, 255)) else: cap1 = caption[:idx] cap2 = caption[idx+1:] d.text((256, 10), cap1, font=fnt, fill=(255, 255, 255, 255)) d.text((256, 60), cap2, font=fnt, fill=(255, 255, 255, 255)) return img_txt
def drawCaption(img, caption): img_txt = Image.fromarray(img) # get a font fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50) # get a drawing context d = ImageDraw.Draw(img_txt) # draw text, half opacity d.text((10, 256), 'Stage-I', font=fnt, fill=(255, 255, 255, 255)) d.text((10, 512), 'Stage-II', font=fnt, fill=(255, 255, 255, 255)) if img.shape[0] > 832: d.text((10, 832), 'Stage-I', font=fnt, fill=(255, 255, 255, 255)) d.text((10, 1088), 'Stage-II', font=fnt, fill=(255, 255, 255, 255)) idx = caption.find(' ', 60) if idx == -1: d.text((256, 10), caption, font=fnt, fill=(255, 255, 255, 255)) else: cap1 = caption[:idx] cap2 = caption[idx+1:] d.text((256, 10), cap1, font=fnt, fill=(255, 255, 255, 255)) d.text((256, 60), cap2, font=fnt, fill=(255, 255, 255, 255)) return img_txt
def quantize_without_scipy(self, image): """" This function can be used if no scipy is availabe. It's 7 times slower though. """ w, h = image.size px = np.asarray(image).copy() memo = {} for j in range(w): for i in range(h): key = (px[i, j, 0], px[i, j, 1], px[i, j, 2]) try: val = memo[key] except KeyError: val = self.convert(*key) memo[key] = val px[i, j, 0], px[i, j, 1], px[i, j, 2] = val return Image.fromarray(px).convert("RGB").quantize(palette=self.paletteImage())
def eval_segmentation_bin( self, y, model_output, th=.5, path='../data/predict/'): '''Evaluation the performance of a binary segmentation. The default used threshold .5. The used evaluation is the mean squarre error. ''' # binarization nbr, dim = y.shape output_bin = np.float32((model_output > th) * 1.) mse = self.MeanSquareError(y, model_output) for i in xrange(nbr): im_gt = Image.fromarray(np.reshape(np.uint8(y[i,:] *255.), (128,128))) im_bin = Image.fromarray(np.reshape(np.uint8(output_bin[i,:] *255.), (128,128))) im_gr = Image.fromarray(np.reshape(np.uint8(model_output[i,:] *255.) , (128,128))) temp = np.concatenate((im_gt, im_bin, im_gr), axis=1) two_imgs = sc.misc.toimage(temp) sc.misc.imsave(path + str(i) +'.png', two_imgs) #two_imgs.show() #raw_input('Press ENTER to continue...') return mse
def segment( self, y, model_output, th=.5, path='../data/predict/'): '''Segment an image using the output of a neural network. The default used threshold .5. ''' # binarization nbr, dim = y.shape output_bin = np.float32((model_output > th) * 1.) for i in xrange(nbr): im_gt = Image.fromarray(np.reshape(np.uint8(y[i,:]) *255., (128,128))) im_bin = Image.fromarray(np.reshape(np.uint8(output_bin[i,:]) *255., (128,128))) im_gr = Image.fromarray(np.reshape(np.uint8(model_output[i,:] *255.) , (128,128))) temp = np.concatenate((im_gt, im_bin, im_gr), axis=1) two_imgs = sc.misc.toimage(temp) sc.misc.imsave(path + str(i) +'.png', two_imgs) #two_imgs.show() #raw_input('Press ENTER to continue...')
def save_unit_test(self, list_im_unit_test, output_unit_test_reproj, mean_shape_train, window,path='../data/unit_test/'): """ Save the output of the unit test. """ nbr = list_im_unit_test.shape[0] for i in xrange(nbr): mtx = list_im_unit_test[i][0] mtx3D = np.zeros((window[0],window[1],3)) mtx3D[:,:,0] = mtx mtx3D[:,:,1] = mtx mtx3D[:,:,2] = mtx im = Image.fromarray(np.uint8( mtx3D * 255)) bbx = [0,0, window[0], window[1]] pred = output_unit_test_reproj[i] # display the mean shape #self.show_landmarks_unit_test( im=im, phis_pred=pred, phis_mean_train=mean_shape_train, bbox=bbx, save=True, path=path+str(i)+'.jpg') # don't show the mean shape. #self.show_only_landmarks_unit_test( im=im, phis_pred=pred, bbox=bbx, save=True, path=path+str(i)+'.jpg') self.show_only_landmarks_unit_test( im=im, phis_pred=mean_shape_train, bbox=bbx, save=True, path=path+str(i)+'.jpg')
def update_photo(data=None,widget=None): global Z if data is None: # By default, assume we're updating with the current value of Z data = np.repeat(np.repeat(np.uint8(from_tanh(model.sample_at(np.float32([Z.flatten()]))[0])),4,1),4,2) else: data = np.repeat(np.repeat(np.uint8(data),4,1),4,2) if widget is None: widget = output # Reshape image to canvas mshape = (4*64,4*64,1) im = Image.fromarray(np.concatenate([np.reshape(data[0],mshape),np.reshape(data[1],mshape),np.reshape(data[2],mshape)],axis=2),mode='RGB') # Make sure photo is an object of the current widget so the garbage collector doesn't wreck it widget.photo = ImageTk.PhotoImage(image=im) widget.create_image(0,0,image=widget.photo,anchor=NW) widget.tag_raise(pixel_rect) # Function to update the latent canvas.
def image_has_low_contrast(image: LIST_3D, threshold: Opt[float] = DEFAULT_CONTRAST_THRESHOLD) -> bool: """ Parameters ---------- image A 3D array with the RGB values for the image threshold The maximum difference in luma that is considered low contrast Returns ------- True if the image has low contrast, False otherwise """ # Convert the image to grayscale, find the difference in luma image = Image.fromarray(image) extrema = image.convert("L").getextrema() return True if abs(extrema[1] - extrema[0]) <= threshold else False
def preprocess_image(img_path, img_size): if not os.path.exists(img_path): print(img_path) return None, 0, 0 input_image = 255 * caffe.io.load_image(img_path) image = PILImage.fromarray(np.uint8(input_image)) image = np.array(image) mean_vec = np.array([103.939, 116.779, 123.68], dtype=np.float32) reshaped_mean_vec = mean_vec.reshape(1, 1, 3); preprocess_img = image[:,:,::-1] preprocess_img = preprocess_img - reshaped_mean_vec # Pad as necessary cur_h, cur_w, cur_c = preprocess_img.shape pad_h = img_size - cur_h pad_w = img_size - cur_w preprocess_img = np.pad(preprocess_img, pad_width=((0, pad_h), (0, pad_w), (0, 0)), mode = 'constant', constant_values = 0) return preprocess_img, cur_h, cur_w
def array_to_img(x, dim_ordering='default', scale=True): from PIL import Image if dim_ordering == 'default': dim_ordering = K.image_dim_ordering() if dim_ordering == 'th': x = x.transpose(1, 2, 0) if scale: x += max(-np.min(x), 0) x_max = np.max(x) if x_max != 0: x /= x_max x *= 255 if x.shape[2] == 3: # RGB return Image.fromarray(x.astype('uint8'), 'RGB') elif x.shape[2] == 1: # grayscale return Image.fromarray(x[:, :, 0].astype('uint8'), 'L') else: raise Exception('Unsupported channel number: ', x.shape[2])
def _prepare_sample_data(self, submission_type): """Prepares sample data for the submission. Args: submission_type: type of the submission. """ # write images images = np.random.randint(0, 256, size=[BATCH_SIZE, 299, 299, 3], dtype=np.uint8) for i in range(BATCH_SIZE): Image.fromarray(images[i, :, :, :]).save( os.path.join(self._sample_input_dir, IMAGE_NAME_PATTERN.format(i))) # write target class for targeted attacks if submission_type == 'targeted_attack': target_classes = np.random.randint(1, 1001, size=[BATCH_SIZE]) target_class_filename = os.path.join(self._sample_input_dir, 'target_class.csv') with open(target_class_filename, 'w') as f: for i in range(BATCH_SIZE): f.write((IMAGE_NAME_PATTERN + ',{1}\n').format(i, target_classes[i]))
def save_images(images, filenames, output_dir): """Saves images to the output directory. Args: images: array with minibatch of images filenames: list of filenames without path If number of file names in this list less than number of images in the minibatch then only first len(filenames) images will be saved. output_dir: directory where to save images """ for i, filename in enumerate(filenames): # Images for inception classifier are normalized to be in [-1, 1] interval, # so rescale them back to [0, 1]. with tf.gfile.Open(os.path.join(output_dir, filename), 'w') as f: img = (((images[i, :, :, :] + 1.0) * 0.5) * 255.0).astype(np.uint8) Image.fromarray(img).save(f, format='PNG')
def load_data(**kwargs): load_mscoco = kwargs['load_mscoco'] interim_testing = kwargs['interim_testing'] if load_mscoco: data = load_mscoco_data() else: txt_file = sys.argv[1] image_dir = os.path.dirname(txt_file) with open(txt_file) as fin: image_filenames = [os.path.join(image_dir, line.rstrip('\n')) for line in fin] data = load_arbitrary_data(image_filenames=image_filenames) if interim_testing: for idx, item in enumerate(data['data_gen']): filename, extension = os.path.splitext(image_filenames[idx]) out_filename = filename + '_interim_w{}_h{}'.format(w, h) + extension PILImage.fromarray(item).save(out_filename) return data
def load_augment(fname, w, h, aug_params=no_augmentation_params, transform=None, sigma=0.0, color_vec=None): """Load augmented image with output shape (w, h). Default arguments return non augmented image of shape (w, h). To apply a fixed transform (color augmentation) specify transform (color_vec). To generate a random augmentation specify aug_params and sigma. """ img = load_image(fname) img = perturb(img, augmentation_params=aug_params, target_shape=(w, h)) #if transform is None: # img = perturb(img, augmentation_params=aug_params, target_shape=(w, h)) #else: # img = perturb_fixed(img, tform_augment=transform, target_shape=(w, h)) #randString = str(np.random.normal(0,1,1)) #im = Image.fromarray(img.transpose(1,2,0).astype('uint8')) #figName = fname.split("/")[-1] #im.save("imgs/"+figName+randString+".jpg") np.subtract(img, MEAN[:, np.newaxis, np.newaxis], out=img) #np.divide(img, STD[:, np.newaxis, np.newaxis], out=img) #img = augment_color(img, sigma=sigma, color_vec=color_vec) return img
def transform_image(image, crop_rect, input_size, hue, sat, value, mirror): cx, cy, cw, ch = crop_rect image = image.crop((cx, cy, cx + cw, cy + ch)).resize((input_size, input_size), Image.BILINEAR) hsv_image = np.asarray(image, dtype=np.float32) dh = int((np.random.random() * 2 - 1) * hue * 255) ds = rand_scale(sat) dv = rand_scale(value) h = hsv_image[:,:,0] h += dh if dh > 0: h[h >= 256] -= 256 else: h[h < 0] += 256 hsv_image[:,:,1] *= ds hsv_image[:,:,2] *= dv image = Image.fromarray(hsv_image.clip(0, 255).astype(np.uint8), 'HSV').convert('RGB') image = np.asarray(image, dtype=np.float32) / 255.0 image = image.transpose(2, 0, 1) if mirror: return image[:,:,::-1] return image
def __getitem__(self, index): if self.train: img, target = self.train_data[index], self.train_labels[index] else: img, target = self.test_data[index], self.test_labels[index] # doing this so that it is consistent with all other datasets # to return a PIL Image img = Image.fromarray(img.numpy(), mode='L') if self.transform is not None: img = self.transform(img) if self.target_transform is not None: target = self.target_transform(target) return img, target
def _process_frame84(frame): img = np.reshape(frame, [210, 160, 3]).astype(np.float32) # RGB??? # https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale img = img[:, :, 0] * 0.299 + img[:, :, 1] * 0.587 + img[:, :, 2] * 0.114 # ??Image?????BILINEAR?? img = Image.fromarray(img) resized_screen = img.resize((84, 110), Image.BILINEAR) resized_screen = np.array(resized_screen) x_t = resized_screen[18:102, :] x_t = np.reshape(x_t, [84, 84, 1]) return x_t.astype(np.uint8) # ??????????step?reset
def _process_frame_mario(frame): img = np.reshape(frame, [224, 256, 3]).astype(np.float32) # RGB??? # https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale img = img[:, :, 0] * 0.299 + img[:, :, 1] * 0.587 + img[:, :, 2] * 0.114 # ??Image?????BILINEAR?? img = Image.fromarray(img) resized_screen = img.resize((84, 110), Image.BILINEAR) resized_screen = np.array(resized_screen) x_t = resized_screen[18:102, :] x_t = np.reshape(x_t, [84, 84, 1]) return x_t.astype(np.uint8) # ??????????step?reset
def setup_class(cls): img_w = img_h = 20 rgb_images = [] gray_images = [] for n in range(8): bias = np.random.rand(img_w, img_h, 1) * 64 variance = np.random.rand(img_w, img_h, 1) * (255-64) imarray = np.random.rand(img_w, img_h, 3) * variance + bias im = Image.fromarray(imarray.astype('uint8')).convert('RGB') rgb_images.append(im) imarray = np.random.rand(img_w, img_h, 1) * variance + bias im = Image.fromarray(imarray.astype('uint8').squeeze()).convert('L') gray_images.append(im) cls.all_test_images = [rgb_images, gray_images]
def draw_seg(self, img, seg_gt, segmentation, name): """Applies generated segmentation mask to an image""" palette = np.load('Extra/palette.npy').tolist() img_size = (img.shape[1], img.shape[0]) segmentation = cv2.resize(segmentation, dsize=img_size, interpolation=cv2.INTER_NEAREST) image = Image.fromarray((img * 255).astype('uint8')) segmentation_draw = Image.fromarray((segmentation).astype('uint8'), 'P') segmentation_draw.putpalette(palette) segmentation_draw.save(self.directory + '/%s_segmentation.png' % name, 'PNG') image.save(self.directory + '/%s.jpg' % name, 'JPEG') if seg_gt: seg_gt_draw = Image.fromarray((seg_gt).astype('uint8'), 'P') seg_gt_draw.putpalette(palette) seg_gt_draw.save(self.directory + '/%s_seg_gt.png' % name, 'PNG')
def dump_vwf(data): font = None for i in range(0, 64): line = None for k in range(0, 16): char_index = i * 16 + k char_data = data[char_index * 24: (char_index + 1) * 24] char = bytes_to_char(char_data) if line is not None: line = np.concatenate([line, char], 1) else: line = char if font is not None: font = np.concatenate([font, line], 0) else: font = line im = Image.fromarray(np.uint8(font * 255)) bio = BytesIO() im.save(bio, 'PNG') return bio.getvalue()
def plot(self, image, filename, save_sample): """ Plot an image.""" image = np.minimum(image, 1) image = np.maximum(image, -1) image = np.squeeze(image) # Scale to 0..255. imin, imax = image.min(), image.max() image = (image - imin) * 255. / (imax - imin) + .5 image = image.astype(np.uint8) if save_sample: try: Image.fromarray(image).save(filename) except Exception as e: print("Warning: could not sample to ", filename, ". Please check permissions and make sure the path exists") print(e) GlobalViewer.update(image)
def save_rotated_test_images(): # # DESCRIPTION # This function rotates the test image and create four patches of 400 * 400 # It then saves those 32 images in the test_set_images folder of each image # # # Loop over all images for i in range(1,51): # Load image image = mpimg.imread('test_set_images/test_'+str(i)+'/test_'+str(i)+'.png') rotations = mk_rotations(image) rota_count = 0 for rotation in rotations: patches = make_4_patch(rotation) patch_count = 0 for patch in patches: patch = format_image(patch) Image.fromarray(patch).save('test_set_images/test_'+str(i)+'/Test_'+str(i)+'_rota'+str(rota_count)+'_patch'+str(patch_count)+'.png') patch_count += 1 rota_count+=1 print('Writing image ',i)
def rotate_testing(): ## Saves rotated versions of each image in the training set for i in np.linspace(1,50,50): image = mpimg.imread('test_set_images/test_'+str(np.int(i))+'/test_'+str(np.int(i))+'.png') imgs = mk_rotations(image) count =0 for im in imgs: im = format_image(im) Image.fromarray(im).save('test_set_images/test_'+str(np.int(i))+'/Test_'+str(np.int(i))+'_rota'+str(np.int(count))+'.png') count+=1 count =0 print('Writing image ',i) return 0 ## Apply rotations and transpositions to each image of the training set and saves them
def compute_hist(net, save_dir, dataset, layer='score', gt='label'): n_cl = net.blobs[layer].channels if save_dir: os.mkdir(save_dir) hist = np.zeros((n_cl, n_cl)) loss = 0 for idx in dataset: net.forward() hist += fast_hist(net.blobs[gt].data[0, 0].flatten(), net.blobs[layer].data[0].argmax(0).flatten(), n_cl) if save_dir: im = Image.fromarray(net.blobs[layer].data[0].argmax(0).astype(np.uint8), mode='P') im.save(os.path.join(save_dir, idx + '.png')) # compute the loss as well loss += net.blobs['loss'].data.flat[0] return hist, loss / len(dataset)
def test_current_screen(self): env = ale.ALE('breakout') tempdir = tempfile.mkdtemp() print('tempdir: {}'.format(tempdir), file=sys.stderr) for episode in range(6): env.initialize() t = 0 while not env.is_terminal: for i in range(4): screen = env.state[i] self.assertEqual(screen.dtype, np.uint8) img = Image.fromarray(screen, mode='L') filename = '{}/{}_{}_{}.bmp'.format( tempdir, str(episode).zfill(6), str(t).zfill(6), i) img.save(filename) legal_actions = env.legal_actions a = random.randrange(len(legal_actions)) env.receive_action(a) t += 1
def imwrite(x, filename): """ Write images as 8bit images to disk. Convenient wrapper around save function of PIL library -------------------------------------------------------------------------- Usage: Call: imwrite(x, filename) Input: x input image x filename string where to save image -------------------------------------------------------------------------- Copyright (C) 2010 Michael Hirsch """ if not x.dtype == 'uint8': x *= 255. imx = Image.fromarray(np.uint8(x)) imx.save(filename)
def generate_result(data_root, input_file, threshold, output_dir): with open(input_file) as fin: result_list = json.load(fin) for it in result_list: key, subset, z = parse_image_file(it['file']) filename = os.path.join(data_root, subset, key + ".mhd") boxes = filterBoxes[it['box']] numpyImage, numpyOrigin, numpySpacing = util.load_itk_image(filename) voxelWidth = 65 prefix = subset + '-' + key + '-' + str(z) + '-' index = 0 for box in boxes: x = box[0] y = box[1] patch = numpyImage[z, x - voxelWidth / 2:x + voxelWidth / 2, y - voxelWidth / 2:y + voxelWidth / 2] Image.fromarray(patch * 255).convert('L').save(os.path.join(output_dir, prefix + str(index) + '.jpg')) index = index + 1
def getAndUpdateBufferY(self, data): if self._iter < self._max_buffer_size: self._buffer_y[self._iter, :] = data[0] return data self._buffer_y[0:self._max_buffer_size-2, :] = self._buffer_y[1:self._max_buffer_size-1, :] self._buffer_y[self._max_buffer_size-1, : ]=data[0] if np.random.rand() < 0.5: return data id = np.random.randint(0, self._max_buffer_size) return self._buffer_y[id, :].reshape((1, 3, self._image_size, self._image_size)) """ def save_images(self,img, w=2, h=3): img = cuda.to_cpu(img) img = img.reshape((w, h, 3, self._image_size, self._image_size)) img = img.transpose(0,1,3,4,2) img = (img + 1) *127.5 img = np.clip(img, 0, 255) img = img.astype(np.uint8) img = img.reshape((w, h, self._image_size, self._image_size, 3)).transpose(0,2,1,3,4).reshape((w*self._image_size, h*self._image_size, 3))[:,:,::-1] Image.fromarray(img).save(self._eval_foler+"/iter_"+str(self._iter)+".jpg") """
def _ungzip(save_path, extract_path, database_name, _): """ Unzip a gzip file and extract it to extract_path :param save_path: The path of the gzip files :param extract_path: The location to extract the data to :param database_name: Name of database :param _: HACK - Used to have to same interface as _unzip """ # Get data from save_path with open(save_path, 'rb') as f: with gzip.GzipFile(fileobj=f) as bytestream: magic = _read32(bytestream) if magic != 2051: raise ValueError('Invalid magic number {} in file: {}'.format(magic, f.name)) num_images = _read32(bytestream) rows = _read32(bytestream) cols = _read32(bytestream) buf = bytestream.read(rows * cols * num_images) data = np.frombuffer(buf, dtype=np.uint8) data = data.reshape(num_images, rows, cols) # Save data to extract_path for image_i, image in enumerate( tqdm(data, unit='File', unit_scale=True, miniters=1, desc='Extracting {}'.format(database_name))): Image.fromarray(image, 'L').save(os.path.join(extract_path, 'image_{}.jpg'.format(image_i)))
def read_image(): im = Image.open('data/len_full.jpg') print im.mode print im.getpixel((0,0)) bw = im.convert('1') x,y = bw.size grey = im.convert('L') for i in range(x): for j in range(y): pass #print bw.getpixel((i,j)) #bw.show() #grey.show() data = grey.getdata() new_data = np.matrix(data) print new_data dt = np.reshape(new_data,(855,400)) print dt for i in range(855-1): for j in range(400-1): if random.random()>0.5: dt[j,i]=0 new_im = Image.fromarray(dt) new_im.show()
def array_to_img(x): """ Util function for converting 4D numpy array to numpy array. Returns PIL RGB image. References ---------- - adapted from keras preprocessing/image.py """ x = np.asarray(x) x += max(-np.min(x), 0) x_max = np.max(x) if x_max != 0: x /= x_max x *= 255 return Image.fromarray(x.astype('uint8'), 'RGB')
def transform(audio_data, save_image_path, nFFT=256, overlap=0.75): '''audio_data: signals to convert save_image_path: path to store the image file''' # spectrogram freq_data = stft(audio_data, nFFT, overlap) freq_data = np.maximum(np.abs(freq_data), np.max(np.abs(freq_data)) / 10000) log_freq_data = 20. * np.log10(freq_data / 1e-4) N_samples = log_freq_data.shape[0] # log_freq_data = np.maximum(log_freq_data, max_m - 70) # print(np.max(np.max(log_freq_data))) # print(np.min(np.min(log_freq_data))) log_freq_data = np.round(log_freq_data) log_freq_data = np.transpose(log_freq_data) # ipdb.set_trace() assert np.max(np.max(log_freq_data)) < 256, 'spectrogram value too large' # save the image spec_imag = Image.fromarray(log_freq_data) spec_imag = spec_imag.convert('RGB') spec_imag.save(save_image_path) return N_samples
def pixelFilter(img, threshold, replace, mode): """Replace some pixels and return a new one Used to remove some noise :param img: an numpy.array :param threshold: usually a tuple pixels compare to. :param replace: pixels used to replace :param mode: 1 means if pixels larger than threshold then do, 0 means less. Usage: >>> img_ = np.array(img) >>> img_ = pixelFilter(np.array(img2), (15, 15, 15), (255, 255, 255), 1) >>> img_ = Image.fromarray(img_) """ if mode == 1: img_ = np.where(img>threshold, replace, img) elif mode == 0: img_ = np.where(img<threshold, replace, img) else: raise Exception("mode is either 1 or 0") return np.array(img_, dtype='uint8', copy=False)
def imageTransform(img, size=(IMAGE_WIDTH, IMAGE_HEIGHT)): # resize img = img.resize(size) # gray img = img.convert('L') # filter img_ = np.array(img) img_ = pixelFilter(img_, 60, 255, 1) img = Image.fromarray(img_) # normalize img = np.array(img).flatten() / 255 return img
def _encode_as_webp(data, profile=None, affine=None): """ Uses BytesIO + PIL to encode a (3, 512, 512) array into a webp bytearray. Parameters ----------- data: ndarray (3 x 512 x 512) uint8 RGB array profile: None ignored affine: None ignored Returns -------- contents: bytearray webp-encoded bytearray of the provided input data """ with BytesIO() as f: im = Image.fromarray(np.rollaxis(data, 0, 3)) im.save(f, format='webp', lossless=True) return f.getvalue()
def create_test_image(image_path,row,col): channel = 3 image_matrix = [[[0 for x in xrange(0,channel)] for y in xrange(0,col)] for z in xrange(0,row)] for x in xrange(0,row): for y in xrange(0,col): if x <= row/2 and y <= col/2: image_matrix[x][y] = [255,0,0] elif x <= row/2 and y > col/2: image_matrix[x][y] = [0,0,255] elif x > row/2 and y <= col/2: image_matrix[x][y] = [0,255,0] else: image_matrix[x][y] = [0,0,0] image_matrix_np = np.array(image_matrix, dtype='uint8') im = Image.fromarray(image_matrix_np) im.save(image_path) return
def _observation(self, observation): """ Paper: First, to encode a single frame we take the maximum value for each pixel colour value over the frame being encoded and the previous frame. This was necessary to remove flickering that is present in games where some objects appear only in even frames while other objects appear only in odd frames, an artefact caused by the limited number of sprites Atari 2600 can display at once. """ obs = np.maximum(observation, self.previous_frame) self.previous_frame = observation """ Paper: Second, we then extract the Y channel, also known as luminance, from the RGB frame and rescale it to 84 x 84 """ img = Image.fromarray(obs) obs = img.resize([84, 84]).convert('L') obs = np.asarray(obs, dtype=np.uint8) return obs
def preprocess_image(image_path): if not os.path.exists(image_path): return None, 0, 0 input_image = 255 * caffe.io.load_image(image_path) image = PILImage.fromarray(np.uint8(input_image)) image = np.array(image) mean_vec = np.array([103.939, 116.779, 123.68], dtype=np.float32) reshaped_mean_vec = mean_vec.reshape(1, 1, 3); im = image[:,:,::-1] im = im - reshaped_mean_vec # Pad as necessary cur_h, cur_w, cur_c = im.shape pad_h = 500 - cur_h pad_w = 500 - cur_w im = np.pad(im, pad_width=((0, pad_h), (0, pad_w), (0, 0)), mode = 'constant', constant_values = 0) return im, cur_h, cur_w