Python six.BytesIO() Examples
The following are 30
code examples of six.BytesIO().
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
six
, or try the search function
.
Example #1
Source File: rendered_env_problem.py From tensor2tensor with Apache License 2.0 | 6 votes |
def _generate_time_steps(self, trajectory_list): """Transforms time step observations to frames of a video.""" for time_step in gym_env_problem.GymEnvProblem._generate_time_steps( self, trajectory_list): # Convert the rendered observations from numpy to png format. frame_np = np.array(time_step.pop(env_problem.OBSERVATION_FIELD)) frame_np = frame_np.reshape( [self.frame_height, self.frame_width, self.num_channels]) # TODO(msaffar) Add support for non RGB rendered environments frame = png.from_array(frame_np, "RGB", info={"bitdepth": 8}) frame_buffer = six.BytesIO() frame.save(frame_buffer) # Put the encoded frame back. time_step[_IMAGE_ENCODED_FIELD] = [frame_buffer.getvalue()] time_step[_IMAGE_FORMAT_FIELD] = [_FORMAT] time_step[_IMAGE_HEIGHT_FIELD] = [self.frame_height] time_step[_IMAGE_WIDTH_FIELD] = [self.frame_width] # Add the frame number time_step[_FRAME_NUMBER_FIELD] = time_step[env_problem.TIMESTEP_FIELD] yield time_step
Example #2
Source File: lsun.py From Global-Second-order-Pooling-Convolutional-Networks with MIT License | 6 votes |
def __getitem__(self, index): img, target = None, None env = self.env with env.begin(write=False) as txn: imgbuf = txn.get(self.keys[index]) buf = six.BytesIO() buf.write(imgbuf) buf.seek(0) img = Image.open(buf).convert('RGB') 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
Example #3
Source File: test_serde_weights.py From ngraph-python with Apache License 2.0 | 6 votes |
def test_serialize_and_deserialize_multi_np(): x = np.random.random((1, )) y = np.random.random((2, 3)) z = np.random.random((1, 5, 2)) values = {'x': x, 'y': y, 'z': z} # write out values in x f = BytesIO() serde_weights.write_np_values(values, f) # reset file so it appears to be freshly opened for deserialize_weights f.seek(0) de_values = serde_weights.read_np_values(f) assert values.keys() == de_values.keys() for k, v in values.items(): assert (de_values[k] == v).all()
Example #4
Source File: packet.py From tacacs_plus with BSD 3-Clause "New" or "Revised" License | 6 votes |
def unpacked(cls, raw): # B = unsigned char # !I = network-order (big-endian) unsigned int raw = six.BytesIO(raw) raw_chars = raw.read(4) if raw_chars: version, type, seq_no, flags = struct.unpack( 'BBBB', raw_chars ) session_id, length = struct.unpack('!II', raw.read(8)) return cls(version, type, session_id, length, seq_no, flags) else: raise ValueError( "Unable to extract data from header. Likely the TACACS+ key does not match between server and client" )
Example #5
Source File: test_tz.py From plugin.video.emby with GNU General Public License v3.0 | 6 votes |
def testLeapCountDecodesProperly(self): # This timezone has leapcnt, and failed to decode until # Eugene Oden notified about the issue. # As leap information is currently unused (and unstored) by tzfile() we # can only indirectly test this: Take advantage of tzfile() not closing # the input file if handed in as an opened file and assert that the # full file content has been read by tzfile(). Note: For this test to # work NEW_YORK must be in TZif version 1 format i.e. no more data # after TZif v1 header + data has been read fileobj = BytesIO(base64.b64decode(NEW_YORK)) tz.tzfile(fileobj) # we expect no remaining file content now, i.e. zero-length; if there's # still data we haven't read the file format correctly remaining_tzfile_content = fileobj.read() self.assertEqual(len(remaining_tzfile_content), 0)
Example #6
Source File: authentication.py From tacacs_plus with BSD 3-Clause "New" or "Revised" License | 6 votes |
def unpacked(cls, raw): # 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 # # +----------------+----------------+----------------+----------------+ # | status | flags | server_msg len | # +----------------+----------------+----------------+----------------+ # | data len | server_msg ... # +----------------+----------------+----------------+----------------+ # | data ... # +----------------+----------------+ # B = unsigned char # !H = network-order (big-endian) unsigned short raw = six.BytesIO(raw) status, flags = struct.unpack('BB', raw.read(2)) server_msg_len, data_len = struct.unpack('!HH', raw.read(4)) server_msg = raw.read(server_msg_len) data = raw.read(data_len) return cls(status, flags, server_msg, data)
Example #7
Source File: accounting.py From tacacs_plus with BSD 3-Clause "New" or "Revised" License | 6 votes |
def unpacked(cls, raw): # 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 # +----------------+----------------+----------------+----------------+ # | server_msg len | data_len | # +----------------+----------------+----------------+----------------+ # | status | server_msg ... # +----------------+----------------+----------------+----------------+ # | data ... # +----------------+ # B = unsigned char # !H = network-order (big-endian) unsigned short raw = six.BytesIO(raw) server_msg_len, data_len = struct.unpack('!HH', raw.read(4)) status = struct.unpack('B', raw.read(1))[0] server_msg = raw.read(server_msg_len) data = raw.read(data_len) if data_len else b'' return cls(status, server_msg, data)
Example #8
Source File: downloader.py From icrawler with MIT License | 6 votes |
def keep_file(self, task, response, min_size=None, max_size=None): """Decide whether to keep the image Compare image size with ``min_size`` and ``max_size`` to decide. Args: response (Response): response of requests. min_size (tuple or None): minimum size of required images. max_size (tuple or None): maximum size of required images. Returns: bool: whether to keep the image. """ try: img = Image.open(BytesIO(response.content)) except (IOError, OSError): return False task['img_size'] = img.size if min_size and not self._size_gt(img.size, min_size): return False if max_size and not self._size_lt(img.size, max_size): return False return True
Example #9
Source File: plot.py From lingvo with Apache License 2.0 | 6 votes |
def FigureToSummary(name, fig): """Create tf.Summary proto from matplotlib.figure.Figure. Args: name: Summary name. fig: A matplotlib figure object. Returns: A `tf.Summary` proto containing the figure rendered to an image. """ canvas = backend_agg.FigureCanvasAgg(fig) fig.canvas.draw() ncols, nrows = fig.canvas.get_width_height() png_file = six.BytesIO() canvas.print_figure(png_file) png_str = png_file.getvalue() return tf.Summary(value=[ tf.Summary.Value( tag='%s/image' % name, image=tf.Summary.Image( height=nrows, width=ncols, colorspace=3, encoded_image_string=png_str)) ])
Example #10
Source File: lsun_loader.py From pre-training with Apache License 2.0 | 6 votes |
def __getitem__(self, index): img, target = None, None env = self.env with env.begin(write=False) as txn: imgbuf = txn.get(self.keys[index]) buf = six.BytesIO() buf.write(imgbuf) buf.seek(0) img = Image.open(buf).convert('RGB') 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
Example #11
Source File: test_storage.py From django-qiniu-storage with MIT License | 6 votes |
def test_read_file(self): ASSET_FILE_NAMES = [u'Read.txt', u'读.txt'] for assert_file_name in ASSET_FILE_NAMES: REMOTE_PATH = join(UNIQUE_PATH, assert_file_name) test_file = six.BytesIO() test_file.write(u"你好世界 Hello World".encode('utf-8')) test_file.seek(0) self.storage.save(REMOTE_PATH, test_file) fil = self.storage.open(REMOTE_PATH, 'r') assert fil._is_read == False content = fil.read() assert content.startswith(u"你好") assert fil._is_read == True # Test open mode fil = self.storage.open(REMOTE_PATH, 'rb') bin_content = fil.read() assert bin_content.startswith(u"你好".encode('utf-8'))
Example #12
Source File: test_private_storage.py From django-qiniu-storage with MIT License | 6 votes |
def test_read_file(self): ASSET_FILE_NAMES = [u'Read.txt', u'读.txt'] for assert_file_name in ASSET_FILE_NAMES: REMOTE_PATH = join(UNIQUE_PATH, assert_file_name) test_file = six.BytesIO() test_file.write(u"你好世界 Hello World".encode('utf-8')) test_file.seek(0) self.storage.save(REMOTE_PATH, test_file) fil = self.storage.open(REMOTE_PATH, 'r') assert fil._is_read == False content = fil.read() assert content.startswith(u"你好") assert fil._is_read == True # Test open mode fil = self.storage.open(REMOTE_PATH, 'rb') bin_content = fil.read() assert bin_content.startswith(u"你好".encode('utf-8'))
Example #13
Source File: rendered_env_problem.py From BERT with Apache License 2.0 | 6 votes |
def _generate_time_steps(self, trajectory_list): """Transforms time step observations to frames of a video.""" for time_step in gym_env_problem.GymEnvProblem._generate_time_steps( self, trajectory_list): # Convert the rendered observations from numpy to png format. frame_np = np.array(time_step.pop(env_problem.OBSERVATION_FIELD)) frame_np = frame_np.reshape( [self.frame_height, self.frame_width, self.num_channels]) # TODO(msaffar) Add support for non RGB rendered environments frame = png.from_array(frame_np, "RGB", info={"bitdepth": 8}) frame_buffer = six.BytesIO() frame.save(frame_buffer) # Put the encoded frame back. time_step[_IMAGE_ENCODED_FIELD] = [frame_buffer.getvalue()] time_step[_IMAGE_FORMAT_FIELD] = [_FORMAT] time_step[_IMAGE_HEIGHT_FIELD] = [self.frame_height] time_step[_IMAGE_WIDTH_FIELD] = [self.frame_width] # Add the frame number time_step[_FRAME_NUMBER_FIELD] = time_step[env_problem.TIMESTEP_FIELD] yield time_step
Example #14
Source File: pkl_utils.py From D-VAE with MIT License | 6 votes |
def load(f, persistent_load=PersistentNdarrayLoad): """Load a file that was dumped to a zip file. :param f: The file handle to the zip file to load the object from. :type f: file :param persistent_load: The persistent loading function to use for unpickling. This must be compatible with the `persisten_id` function used when pickling. :type persistent_load: callable, optional .. versionadded:: 0.8 """ with closing(zipfile.ZipFile(f, 'r')) as zip_file: p = pickle.Unpickler(BytesIO(zip_file.open('pkl').read())) p.persistent_load = persistent_load(zip_file) return p.load()
Example #15
Source File: switch.py From xcffib with Apache License 2.0 | 6 votes |
def GetProperty(self, value_mask, items, is_checked=True): buf = six.BytesIO() buf.write(struct.pack("=xx2xI", value_mask)) if value_mask & CA.Counter: counter = items.pop(0) buf.write(struct.pack("=I", counter)) if value_mask & CA.Value: value = items.pop(0) buf.write(value.pack() if hasattr(value, "pack") else INT64.synthetic(*value).pack()) if value_mask & CA.ValueType: valueType = items.pop(0) buf.write(struct.pack("=I", valueType)) if value_mask & CA.Events: events = items.pop(0) buf.write(struct.pack("=I", events)) return self.send_request(59, buf, GetPropertyCookie, is_checked=is_checked)
Example #16
Source File: backends.py From django-qiniu-storage with MIT License | 5 votes |
def read(self, num_bytes=None): if not self._is_read: content = self._storage._read(self._name) self.file = six.BytesIO(content) self._is_read = True if num_bytes is None: data = self.file.read() else: data = self.file.read(num_bytes) if 'b' in self._mode: return data else: return force_text(data)
Example #17
Source File: test_audio_helpers.py From assistant-sdk-python with Apache License 2.0 | 5 votes |
def setUp(self): self.stream = BytesIO() self.sink = audio_helpers.WaveSink(self.stream, 16000, 2)
Example #18
Source File: test_storage.py From django-qiniu-storage with MIT License | 5 votes |
def test_write_and_delete_file(self): ASSET_FILE_NAMES = [u'Write&Deltete.txt', u'写和删.txt'] for assert_file_name in ASSET_FILE_NAMES: REMOTE_PATH = join(UNIQUE_PATH, assert_file_name) assert self.storage.exists(REMOTE_PATH) == False fil = QiniuFile(REMOTE_PATH, self.storage, mode='wb') content = u"你好世界 Hello World" # get file size dummy_file = six.BytesIO() dummy_file.write(content.encode('utf-8')) dummy_file.seek(0, os.SEEK_END) file_size = dummy_file.tell() fil.write(content) self.storage._save(REMOTE_PATH, fil) assert self.storage.exists(REMOTE_PATH) assert self.storage.size(REMOTE_PATH) == file_size now = datetime.utcnow() modified_time = self.storage.modified_time(REMOTE_PATH) # Datetime on Qiniu servers may be faster or slower than the local # machine. Thus the absolute delta within 60s should be considered # acceptable. time_delta = max(now, modified_time) - min(now, modified_time) assert time_delta.seconds < 180 self.storage.delete(REMOTE_PATH) assert self.storage.exists(REMOTE_PATH) == False
Example #19
Source File: test_audio_helpers.py From assistant-sdk-python with Apache License 2.0 | 5 votes |
def setUp(self): stream = BytesIO() w = wave.open(stream, 'wb') sample_rate = 16000 bytes_per_sample = 2 w.setframerate(sample_rate) w.setsampwidth(bytes_per_sample) w.setnchannels(1) w.writeframes(b'audiodata') self.stream = BytesIO(stream.getvalue()) self.source = audio_helpers.WaveSource( self.stream, sample_rate, bytes_per_sample) self.sleep_time_1024 = self.source._sleep_time(1024) self.sleep_time_512 = self.source._sleep_time(512)
Example #20
Source File: test_audio_helpers.py From assistant-sdk-python with Apache License 2.0 | 5 votes |
def test_raw(self): self.stream = BytesIO(b'audiodata') self.source = audio_helpers.WaveSource(self.stream, 16000, 2) self.assertEqual(b'audiodata', self.source.read(9))
Example #21
Source File: tests.py From django-excel-response with Apache License 2.0 | 5 votes |
def test_create_excel_from_list(self): r = response.ExcelResponse( [['a', 'b', 'c'], [1, 2, 3], [4, 5, 6]] ) output = six.BytesIO(r.getvalue()) # This should theoretically raise errors if it's not a valid spreadsheet wb = openpyxl.load_workbook(output, read_only=True) ws = wb.active self.assertEqual((ws['A1'].value, ws['B1'].value, ws['C1'].value), ('a', 'b', 'c')) self.assertEqual((ws['A2'].value, ws['B2'].value, ws['C2'].value), (1, 2, 3))
Example #22
Source File: switch.py From xcffib with Apache License 2.0 | 5 votes |
def pack(self): buf = six.BytesIO() buf.write(struct.pack("=iI", self.hi, self.lo)) return buf.getvalue()
Example #23
Source File: test_serde_weights.py From ngraph-python with Apache License 2.0 | 5 votes |
def test_round_trip(): # set up an op and Assign a value to it so we can read it out axes = ng.make_axes([ ng.make_axis(name='A', length=2), ng.make_axis(name='B', length=3), ]) x_op = ng.variable(axes) assign_op = ng.AssignOp(x_op, 1) with executor(assign_op) as assign_computation: t = assign_computation.transformer # Set initial value assign_computation() # Test value np.testing.assert_allclose(serde_weights.extract_op(t, x_op), 1) # write out values in x and graph f = BytesIO() # ## EXAMPLE OF HOW TO FULLY SERIALIZE A GRAPH ### serde_weights.serialize_weights(t, [x_op], f) graph_string = serde.serialize_graph([x_op]) # ## /EXAMPLE OF HOW TO FULLY SERIALIZE A GRAPH ### f.seek(0) # ## EXAMPLE OF HOW TO FULLY DESERIALIZE A GRAPH ### new_ops = serde.deserialize_graph(graph_string) serde_weights.deserialize_weights(t, new_ops, f) # ## /EXAMPLE OF HOW TO FULLY DESERIALIZE A GRAPH ### np.testing.assert_allclose(serde_weights.extract_op(t, new_ops[0]), 1)
Example #24
Source File: test_serde_weights.py From ngraph-python with Apache License 2.0 | 5 votes |
def test_serialize_and_deserialize_single_raw_np(): x = np.random.random((1, )) # write out values in x f = BytesIO() serde_weights.write_raw_np(x, f) # reset file so it appears to be freshly opened for deserialize_weights f.seek(0) de_x = f.read() assert de_x == x.tostring()
Example #25
Source File: visualization_utils.py From Person-Detection-and-Tracking with MIT License | 5 votes |
def encode_image_array_as_png_str(image): """Encodes a numpy array into a PNG string. Args: image: a numpy array with shape [height, width, 3]. Returns: PNG encoded image string. """ image_pil = Image.fromarray(np.uint8(image)) output = six.BytesIO() image_pil.save(output, format='PNG') png_string = output.getvalue() output.close() return png_string
Example #26
Source File: visualization_utils.py From Person-Detection-and-Tracking with MIT License | 5 votes |
def encode_image_array_as_png_str(image): """Encodes a numpy array into a PNG string. Args: image: a numpy array with shape [height, width, 3]. Returns: PNG encoded image string. """ image_pil = Image.fromarray(np.uint8(image)) output = six.BytesIO() image_pil.save(output, format='PNG') png_string = output.getvalue() output.close() return png_string
Example #27
Source File: gradelib.py From xqueue-watcher with GNU Affero General Public License v3.0 | 5 votes |
def _tokens(code): """ A wrapper around tokenize.generate_tokens. """ # Protect against pathological inputs: http://bugs.python.org/issue16152 code = code.rstrip() + "\n" if isinstance(code, six.text_type): code = code.encode('utf8') code = "# coding: utf8\n" + code toks = tokenize.generate_tokens(six.BytesIO(code).readline) return toks
Example #28
Source File: visualization_utils.py From MobileNet-SSDLite-RealSense-TF with MIT License | 5 votes |
def encode_image_array_as_png_str(image): """Encodes a numpy array into a PNG string. Args: image: a numpy array with shape [height, width, 3]. Returns: PNG encoded image string. """ image_pil = Image.fromarray(np.uint8(image)) output = six.BytesIO() image_pil.save(output, format='PNG') png_string = output.getvalue() output.close() return png_string
Example #29
Source File: s3.py From splits with MIT License | 5 votes |
def __init__(self, uri, mode='r', s3 = None): self.mode = mode self.s3uri = S3Uri(uri) assert self.s3uri.is_file(), "Uri (got {0}) must be a file (not directory or bucket) on S3.".format(uri) if s3: self.s3 = s3 else: self.__init_s3() six.BytesIO.__init__(self) if 'r' in self.mode: self.s3.getfile(self.s3uri, self) self.seek(0)
Example #30
Source File: visualization_utils.py From ros_people_object_detection_tensorflow with Apache License 2.0 | 5 votes |
def encode_image_array_as_png_str(image): """Encodes a numpy array into a PNG string. Args: image: a numpy array with shape [height, width, 3]. Returns: PNG encoded image string. """ image_pil = Image.fromarray(np.uint8(image)) output = six.BytesIO() image_pil.save(output, format='PNG') png_string = output.getvalue() output.close() return png_string