Python PIL.Image.frombytes() Examples
The following are 30
code examples of PIL.Image.frombytes().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
PIL.Image
, or try the search function
.

Example #1
Source File: linux.py From Airtest with Apache License 2.0 | 8 votes |
def snapshot(self, filename="tmp.png"): """ Take a screenshot and save it to `tmp.png` filename by default Args: filename: name of file where to store the screenshot Returns: display the screenshot """ w, h = self.get_current_resolution() dsp = display.Display() root = dsp.screen().root raw = root.get_image(0, 0, w, h, X.ZPixmap, 0xffffffff) image = Image.frombytes("RGB", (w, h), raw.data, "raw", "BGRX") from airtest.aircv.utils import pil_2_cv2 image = pil_2_cv2(image) return image
Example #2
Source File: util.py From D4LCN with MIT License | 7 votes |
def fig_to_im(fig): fig.canvas.draw() # Get the RGBA buffer from the figure w, h = fig.canvas.get_width_height() buf = np.fromstring(fig.canvas.tostring_argb(), dtype=np.uint8) buf.shape = (w, h, 4) # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode buf = np.roll(buf, 3, axis=2) w, h, d = buf.shape im_pil = Image.frombytes("RGBA", (w, h), buf.tostring()) im_np = np.array(im_pil)[:,:,:3] return im_np
Example #3
Source File: convert_bair.py From amortized-variational-filtering with MIT License | 6 votes |
def convert(data_path): # iterate through the data splits for data_split in ['train', 'test']: os.makedirs(os.path.join(data_path, data_split)) data_split_path = os.path.join(data_path, 'softmotion30_44k', data_split) data_split_files = gfile.Glob(os.path.join(data_split_path, '*')) # iterate through the TF records for f in data_split_files: print('Current file: ' + f) ind = int(f.split('/')[-1].split('_')[1]) # starting video index # iterate through the sequences in this TF record for serialized_example in tf.python_io.tf_record_iterator(f): os.makedirs(os.path.join(data_path, data_split, str(ind))) example = tf.train.Example() example.ParseFromString(serialized_example) # iterate through the sequence for i in range(30): image_name = str(i) + '/image_aux1/encoded' byte_str = example.features.feature[image_name].bytes_list.value[0] img = Image.frombytes('RGB', (64, 64), byte_str) img = np.array(img.getdata()).reshape(img.size[1], img.size[0], 3) / 255. imsave(os.path.join(data_path, data_split, str(ind), str(i) + '.png'), img) print(' Finished processing sequence ' + str(ind)) ind += 1
Example #4
Source File: main.py From SupervisedChromeTrex with MIT License | 6 votes |
def getmssimage(self): import mss with mss.mss() as sct: mon = sct.monitors[1] L = mon["left"] + self.X T = mon["top"] + self.Y W = L + self.width H = T + self.height bbox = (L,T,W,H) #print(bbox) sct_img = sct.grab(bbox) img_pil = Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX') img_np = np.array(img_pil) #finalimg = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY) return img_np
Example #5
Source File: test_file_gif.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_transparent_optimize(self): # from issue #2195, if the transparent color is incorrectly # optimized out, gif loses transparency # Need a palette that isn't using the 0 color, and one # that's > 128 items where the transparent color is actually # the top palette entry to trigger the bug. data = bytes(bytearray(range(1, 254))) palette = ImagePalette.ImagePalette("RGB", list(range(256))*3) im = Image.new('L', (253, 1)) im.frombytes(data) im.putpalette(palette) out = self.tempfile('temp.gif') im.save(out, transparency=253) reloaded = Image.open(out) self.assertEqual(reloaded.info['transparency'], 253)
Example #6
Source File: opengl.py From c3nav with Apache License 2.0 | 6 votes |
def run(self): while True: task = self._queue.get() ctx = self._get_ctx(task.width, task.height) ctx.ctx.clear(*task.background_rgb) ctx.prog['mvp'].value = task.mvp if task.vertices: vbo = ctx.ctx.buffer(task.vertices) # noinspection PyTypeChecker vao = ctx.ctx.simple_vertex_array(ctx.prog, vbo, 'in_vert', 'in_color') vao.render() color_rbo2 = ctx.ctx.renderbuffer((task.width, task.height)) fbo2 = ctx.ctx.framebuffer([color_rbo2]) ctx.ctx.copy_framebuffer(fbo2, ctx.fbo) img = Image.frombytes('RGB', (task.width, task.height), fbo2.read(components=3)) f = io.BytesIO() img.save(f, 'PNG') f.seek(0) task.set_result(f.read())
Example #7
Source File: bbox_plotter.py From kiss with GNU General Public License v3.0 | 6 votes |
def show_pca(self, dest_image, updater): colors = ['navy', 'turquoise', 'darkorange'] if getattr(updater, 'pca', None) is None: return dest_image pca_discriminator = updater.pca.reshape(3, -1, updater.n_components_pca) plt.figure() for i, color, in enumerate(colors): plt.scatter(pca_discriminator[i, :, 0], pca_discriminator[i, :, 1], color=color, lw=2) plt.legend(['fake', 'real', 'anchor']) canvas = plt.get_current_fig_manager().canvas canvas.draw() image = Image.frombytes('RGB', canvas.get_width_height(), canvas.tostring_rgb()) image = image.resize((self.image_size.width, self.image_size.height), Image.LANCZOS) dest_image.paste(image, (self.image_size.width, self.image_size.height)) plt.close() return dest_image
Example #8
Source File: dino_api.py From go_dino with GNU General Public License v3.0 | 6 votes |
def find_game_position(self, threshold) -> Dict: monitor = self.shooter.monitors[0] buffer = self.shooter.grab(monitor) image = Image.frombytes('RGB', buffer.size, buffer.rgb).convert('L') image = np.array(image) dino_template = cv2.imread(os.path.join('templates', 'dino.png'), 0) res = cv2.matchTemplate(image, dino_template, cv2.TM_CCOEFF_NORMED) loc = np.where(res >= threshold) if len(loc[0]) == 0: dino_template = cv2.imread(os.path.join('templates', 'dino2.png'), 0) res = cv2.matchTemplate(image, dino_template, cv2.TM_CCOEFF_NORMED) loc = np.where(res >= threshold) if len(loc[0]): pt = next(zip(*loc[::-1])) w, h = dino_template.shape[::-1] lw, lh = self.landscape_template.shape[::-1] return dict(monitor, height=lh, left=pt[0], top=pt[1] - lh + h, width=lw) return {}
Example #9
Source File: tools.py From Dindo-Bot with MIT License | 6 votes |
def screen_game(region, save_to=None): x, y, width, height = region try: raw = root.get_image(x, y, width, height, X.ZPixmap, 0xffffffff) if hasattr(Image, 'frombytes'): # for Pillow screenshot = Image.frombytes('RGB', (width, height), raw.data, 'raw', 'BGRX') else: # for PIL screenshot = Image.fromstring('RGB', (width, height), raw.data, 'raw', 'BGRX') if save_to is not None: screenshot.save(save_to + '.png') except: filename = save_to + '.png' if save_to is not None else None screenshot = pyautogui.screenshot(filename, region) return screenshot # Return pixel color of given x, y coordinates
Example #10
Source File: Simulator.py From minos with MIT License | 6 votes |
def __process_color(self, name, rgb): """Converts rgb bytes to Image and reshapes""" frame = rgb['data'] encoding = rgb.get('encoding') data = None image = None # RGB image mode = 'RGBA' if encoding == 'rgba': data = np.reshape(frame, rgb['shape']) elif encoding == 'gray': mode = 'L' data = np.reshape(frame, (rgb['shape'][0], rgb['shape'][1])) if self.params.get('save_png'): if image is None: image = Image.frombytes(mode,(data.shape[0], data.shape[1]),data) cnt = self.stats_counter['frames_received'] image.save(os.path.join(self._output_dir, name + ('_%d.png' % cnt))) return {'image': image, 'data': data}
Example #11
Source File: sensor.py From coiltraine with MIT License | 6 votes |
def save_to_disk(self, filename, format='.png'): """Save this image to disk (requires PIL installed).""" filename = _append_extension(filename, format) try: from PIL import Image as PImage except ImportError: raise RuntimeError( 'cannot import PIL, make sure pillow package is installed') image = PImage.frombytes( mode='RGBA', size=(self.width, self.height), data=self.raw_data, decoder_name='raw') color = image.split() image = PImage.merge("RGB", color[2::-1]) folder = os.path.dirname(filename) if not os.path.isdir(folder): os.makedirs(folder) image.save(filename, quality=100)
Example #12
Source File: Simulator.py From minos with MIT License | 6 votes |
def __process_camera_frame(self, name, f): """Converts generic camera based frame (assume to be rgba) bytes to Image and reshapes""" if type(f)==list: f=f[0] data = np.reshape(f['data'], f['shape']) data_viz = None if 'data_viz' in f: data_viz = np.reshape(f['data_viz'], f['shape']) image = None if self.params.get('save_png'): if image is None: imgd = data_viz if data_viz is not None else data image = Image.frombytes('RGBA',(imgd.shape[0], imgd.shape[1]),imgd) cnt = self.stats_counter['frames_received'] image.save(os.path.join(self._output_dir, name + ('_%d.png' % cnt))) return {'image': image, 'data': data, 'data_viz': data_viz}
Example #13
Source File: score.py From MLOpsDatabricks with MIT License | 6 votes |
def run(raw_data): transform = transforms.transforms.Compose([ transforms.transforms.ToTensor(), transforms.transforms.Normalize( (0.1307,), (0.3081,)) ]) img = Image.frombytes( '1', (28, 28), str(json.loads(json.dumps(raw_data))['data']).encode()) input_data = transform(img) inputs_dc.collect(input_data) input_data = input_data.unsqueeze(0) classes = ['tshirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] output = model(input_data) prediction_dc.collect(output) index = torch.argmax(output, 1) return classes[index]
Example #14
Source File: sensor.py From Hands-On-Intelligent-Agents-with-OpenAI-Gym with MIT License | 6 votes |
def save_to_disk(self, filename): """Save this image to disk (requires PIL installed).""" filename = _append_extension(filename, '.png') try: from PIL import Image as PImage except ImportError: raise RuntimeError( 'cannot import PIL, make sure pillow package is installed') image = PImage.frombytes( mode='RGBA', size=(self.width, self.height), data=self.raw_data, decoder_name='raw') color = image.split() image = PImage.merge("RGB", color[2::-1]) folder = os.path.dirname(filename) if not os.path.isdir(folder): os.makedirs(folder) image.save(filename)
Example #15
Source File: sensor.py From Hands-On-Intelligent-Agents-with-OpenAI-Gym with MIT License | 6 votes |
def save_to_disk(self, filename): """Save this image to disk (requires PIL installed).""" filename = _append_extension(filename, '.png') try: from PIL import Image as PImage except ImportError: raise RuntimeError( 'cannot import PIL, make sure pillow package is installed') image = PImage.frombytes( mode='RGBA', size=(self.width, self.height), data=self.raw_data, decoder_name='raw') color = image.split() image = PImage.merge("RGB", color[2::-1]) folder = os.path.dirname(filename) if not os.path.isdir(folder): os.makedirs(folder) image.save(filename)
Example #16
Source File: camera_v4l2.py From flask-video-streaming with MIT License | 6 votes |
def frames(): video = v4l2capture.Video_device(Camera.video_source) # Suggest an image size. The device may choose and return another if unsupported size_x = 640 size_y = 480 size_x, size_y = video.set_format(size_x, size_y) video.create_buffers(1) video.queue_all_buffers() video.start() bio = io.BytesIO() try: while True: select.select((video,), (), ()) # Wait for the device to fill the buffer. image_data = video.read_and_queue() image = Image.frombytes("RGB", (size_x, size_y), image_data) image.save(bio, format="jpeg") yield bio.getvalue() bio.seek(0) bio.truncate() finally: video.close()
Example #17
Source File: remote_terminal.py From pepper-robot-programming with MIT License | 6 votes |
def _capture3dImage(self): # Depth Image in RGB # WARNING : The same Name could be used only six time. strName = "capture3dImage_{}".format(random.randint(1,10000000000)) clientRGB = self.video.subscribeCamera(strName, AL_kDepthCamera, AL_kQVGA, 11, 15) imageRGB = self.video.getImageRemote(clientRGB) imageWidth = imageRGB[0] imageHeight = imageRGB[1] array = imageRGB[6] image_string = str(bytearray(array)) # Create a PIL Image from our pixel array. im = Image.frombytes("RGB", (imageWidth, imageHeight), image_string) # Save the image inside the images foler in pwd. image_name_3d = "images/img3d-" + str(self.imageNo3d) + ".png" im.save(image_name_3d, "PNG") # Stored in images folder in the pwd, if not present then create one self.imageNo3d += 1 im.show() return
Example #18
Source File: remote_voice.py From pepper-robot-programming with MIT License | 6 votes |
def _capture3dImage(self): # Depth Image in RGB # WARNING : The same Name could be used only six time. strName = "capture3dImage_{}".format(random.randint(1,10000000000)) clientRGB = self.video.subscribeCamera(strName, AL_kDepthCamera, AL_kQVGA, 11, 15) imageRGB = self.video.getImageRemote(clientRGB) imageWidth = imageRGB[0] imageHeight = imageRGB[1] array = imageRGB[6] image_string = str(bytearray(array)) # Create a PIL Image from our pixel array. im = Image.frombytes("RGB", (imageWidth, imageHeight), image_string) # Save the image inside the images foler in pwd. image_name_3d = "images/img3d-" + str(self.imageNo3d) + ".png" im.save(image_name_3d, "PNG") # Stored in images folder in the pwd, if not present then create one self.imageNo3d += 1 im.show() return
Example #19
Source File: remote_voice.py From pepper-robot-programming with MIT License | 6 votes |
def _capture2dImage(self, cameraId): # Capture Image in RGB # WARNING : The same Name could be used only six time. strName = "capture2DImage_{}".format(random.randint(1,10000000000)) clientRGB = self.video.subscribeCamera(strName, cameraId, AL_kVGA, 11, 10) imageRGB = self.video.getImageRemote(clientRGB) imageWidth = imageRGB[0] imageHeight = imageRGB[1] array = imageRGB[6] image_string = str(bytearray(array)) # Create a PIL Image from our pixel array. im = Image.frombytes("RGB", (imageWidth, imageHeight), image_string) # Save the image inside the images foler in pwd. image_name_2d = "images/img2d-" + str(self.imageNo2d) + ".png" im.save(image_name_2d, "PNG") # Stored in images folder in the pwd, if not present then create one self.imageNo2d += 1 im.show() return
Example #20
Source File: asthama_search.py From pepper-robot-programming with MIT License | 6 votes |
def _capture3dImage(self): # Depth Image in RGB # WARNING : The same Name could be used only six time. strName = "capture3dImage_{}".format(random.randint(1,10000000000)) clientRGB = self.video_service.subscribeCamera(strName, AL_kDepthCamera, AL_kQVGA, 11, 10) imageRGB = self.video_service.getImageRemote(clientRGB) imageWidth = imageRGB[0] imageHeight = imageRGB[1] array = imageRGB[6] image_string = str(bytearray(array)) # Create a PIL Image from our pixel array. im = Image.frombytes("RGB", (imageWidth, imageHeight), image_string) # Save the image. image_name_3d = "images/img3d-" + str(self.imageNo3d) + ".png" im.save(image_name_3d, "PNG") # Stored in images folder in the pwd, if not present then create one self.imageNo3d += 1 im.show() return
Example #21
Source File: asthama_search.py From pepper-robot-programming with MIT License | 6 votes |
def _capture2dImage(self, cameraId): # Capture Image in RGB # WARNING : The same Name could be used only six time. strName = "capture2DImage_{}".format(random.randint(1,10000000000)) clientRGB = self.video_service.subscribeCamera(strName, cameraId, AL_kVGA, 11, 10) imageRGB = self.video_service.getImageRemote(clientRGB) imageWidth = imageRGB[0] imageHeight = imageRGB[1] array = imageRGB[6] image_string = str(bytearray(array)) # Create a PIL Image from our pixel array. im = Image.frombytes("RGB", (imageWidth, imageHeight), image_string) # Save the image. image_name_2d = "images/img2d-" + str(self.imageNo2d) + ".png" im.save(image_name_2d, "PNG") # Stored in images folder in the pwd, if not present then create one self.imageNo2d += 1 im.show() return
Example #22
Source File: screenshot.py From demosys-py with ISC License | 5 votes |
def create(file_format='png', name=None): """ Create a screenshot :param file_format: formats supported by PIL (png, jpeg etc) """ dest = "" if settings.SCREENSHOT_PATH: if not os.path.exists(settings.SCREENSHOT_PATH): print("SCREENSHOT_PATH does not exist. creating: {}".format(settings.SCREENSHOT_PATH)) os.makedirs(settings.SCREENSHOT_PATH) dest = settings.SCREENSHOT_PATH else: print("SCREENSHOT_PATH not defined in settings. Using cwd as fallback.") if not Config.target: Config.target = context.window().fbo image = Image.frombytes( "RGB", (Config.target.viewport[2], Config.target.viewport[3]), Config.target.read(viewport=Config.target.viewport, alignment=Config.alignment), ) image = image.transpose(Image.FLIP_TOP_BOTTOM) if not name: name = "{}.{}".format(datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f"), file_format) dest = os.path.join(dest, name) print("Creating screenshot:", dest) image.save(dest, format=file_format)
Example #23
Source File: Simulator.py From minos with MIT License | 5 votes |
def __process_depth(self, name, depth): """Converts depth bytes to Image and reshapes""" frame = depth['data'] encoding = depth.get('encoding') data = None data_clean = None image = None # depths #self._logger.info('frame length %d', len(frame)) #self._logger.info('shape %s', depth['shape']) mode = 'RGBA' if encoding == 'rgba': data = np.reshape(frame, depth['shape']) elif encoding == 'depth' or encoding == 'binned': #dims = (depth['shape'][0], depth['shape'][1]) #self._logger.info(dims) data = np.reshape(frame, (depth['shape'][0], depth['shape'][1])) mode = 'L' # TODO: need to make sure in meters and is float32 for depth sensor noise simulation depth_sensor = self._sensors_by_name[name] if depth_sensor.noise_sim is not None: # Simulate noise data_clean = np.copy(data) # TODO: Is this copying unnecessarily expensive, should there be a flag guarding against this? depth_sensor.noise_sim.simulate(data) if self.params.get('save_png'): if image is None: # self._logger.info('type is %s' % type(data)) d = data.astype(np.float32) d = (d * (255.0 / np.max(d))).astype(np.uint8) image = Image.frombytes(mode,(d.shape[0], d.shape[1]),d) cnt = self.stats_counter['frames_received'] image.save(os.path.join(self._output_dir, name + ('_%d.png' % cnt))) return {'image': image, 'data': data, 'data_clean': data_clean}
Example #24
Source File: test_image.py From neptune-client with Apache License 2.0 | 5 votes |
def test_get_image_content_from_figure(self): # given pyplot.plot([1, 2, 3, 4]) pyplot.ylabel('some interesting numbers') figure = pyplot.gcf() figure.canvas.draw() expected_image = Image.frombytes('RGB', figure.canvas.get_width_height(), figure.canvas.tostring_rgb()) # expect self.assertEqual(get_image_content(figure), _get_pil_image_data(expected_image))
Example #25
Source File: util.py From photobooth with GNU Affero General Public License v3.0 | 5 votes |
def unpickle_image(image_data): if image_data is None: return None else: image = Image.frombytes(*image_data) return image
Example #26
Source File: plot.py From Jacinle with MIT License | 5 votes |
def plot2pil(fig): canvas = fig.canvas canvas.draw() pil = Image.frombytes('RGB', canvas.get_width_height(), canvas.tostring_rgb()) return pil
Example #27
Source File: convert.py From Dindo-Bot with MIT License | 5 votes |
def pixbuf2image(pixbuf): data = pixbuf.get_pixbufels() width = pixbuf.props.width height = pixbuf.props.height stride = pixbuf.props.rowstride mode = 'RGB' if pixbuf.props.has_alpha == True: mode = 'RGBA' image = Image.frombytes(mode, (width, height), data, 'raw', mode, stride) return image # Convert Pillow image to GdkPixbuf
Example #28
Source File: util.py From kraken with Apache License 2.0 | 5 votes |
def array2pil(a: np.array) -> Image: if a.dtype == np.dtype("B"): if a.ndim == 2: return Image.frombytes("L", (a.shape[1], a.shape[0]), a.tostring()) elif a.ndim == 3: return Image.frombytes("RGB", (a.shape[1], a.shape[0]), a.tostring()) else: raise Exception("bad image rank") elif a.dtype == np.dtype('float32'): return Image.frombytes("F", (a.shape[1], a.shape[0]), a.tostring()) else: raise Exception("unknown image type")
Example #29
Source File: mpv.py From FeelUOwn with GNU General Public License v3.0 | 5 votes |
def screenshot_raw(self, includes='subtitles'): """Mapped mpv screenshot_raw command, see man mpv(1). Returns a pillow Image object.""" from PIL import Image res = self.node_command('screenshot-raw', includes) if res['format'] != 'bgr0': raise ValueError('Screenshot in unknown format "{}". Currently, only bgr0 is supported.' .format(res['format'])) img = Image.frombytes('RGBA', (res['w'], res['h']), res['data']) b,g,r,a = img.split() return Image.merge('RGB', (r,g,b))
Example #30
Source File: graphics.py From kitty with GNU General Public License v3.0 | 5 votes |
def test_load_png(self): s, g, l, sl = load_helpers(self) w, h = 5, 3 rgba_data = byte_block(w * h * 4) img = Image.frombytes('RGBA', (w, h), rgba_data) rgb_data = img.convert('RGB').convert('RGBA').tobytes() def png(mode='RGBA'): buf = BytesIO() i = img if mode != i.mode: i = img.convert(mode) i.save(buf, 'PNG') return buf.getvalue() for mode in 'RGBA RGB'.split(): data = png(mode) sl(data, f=100, expecting_data=rgb_data if mode == 'RGB' else rgba_data) for m in 'LP': img = img.convert(m) rgba_data = img.convert('RGBA').tobytes() data = png(m) sl(data, f=100, expecting_data=rgba_data) self.ae(l(b'a' * 20, f=100, S=20).partition(':')[0], 'EBADPNG')