def __init__(self): self.FPS = 10 self.screenbuf = "" self.password = "" if ver == 2: self.screenfile = io.StringIO() elif ver == 3: self.screenfile = io.BytesIO() threading.Thread(target=self.getframes).start()
def __init__(self, sound, start=0, end=None): super(SoundThread, self).__init__() self.wav_buffer = io.BytesIO() _output = wave.open(self.wav_buffer, "wb") _output.setnchannels(self.sound.channels) _output.setsampwidth(self.sound.samplewidth) _output.setframerate(self.sound.framerate) _output.writeframes(self.sound.raw) _output.close() self.wav_buffer.seek(0)
def __init__(self, source, start=0, end=None): if type(source) is bytes: in_wav = wave.open(io.BytesIO(source)) else: in_wav = wave.open(source, "rb") self.framerate = in_wav.getframerate() self.channels = in_wav.getnchannels() self.samplewidth = in_wav.getsampwidth() in_wav.setpos(int(start * self.framerate)) if end is None: end = (in_wav.getnframes() - start / self.framerate) self.raw = in_wav.readframes(int((end - start) * self.framerate)) in_wav.close()
def get_wavedesc(self, channel): ''' Requests the wave descriptor for `channel` from the scope. Returns it in dictionary format. ''' if channel not in range(1, 5): raise Exception('channel must be in %s.' % str(range(1, 5))) self.send('c%s:wf? desc' % str(channel)) msg = self.recv() if not int(msg[1]) == channel+48: raise RuntimeError('waveforms out of sync or comm_header is off.') data = StringIO.BytesIO(msg) startpos = re.search(b'WAVEDESC', data.read()).start() wavedesc = {} # check endian data.seek(startpos + 34) if struct.unpack('<'+Enum.packfmt, data.read(Enum.length)) == 0: endian = '>' wavedesc['little_endian'] = True np.little_endian = True else: endian = '<' wavedesc['little_endian'] = False np.little_endian = False data.seek(startpos) # build dictionary of wave description for name, pos, datatype in wavedesc_template: raw = data.read(datatype.length) if datatype in (String, UnitDefinition): wavedesc[name] = raw.rstrip(b'\x00') elif datatype in (TimeStamp,): wavedesc[name] = struct.unpack(endian+datatype.packfmt, raw) else: wavedesc[name] = struct.unpack(endian+datatype.packfmt, raw)[0] # determine data type if wavedesc['comm_type'] == 0: wavedesc['dtype'] = np.int8() elif wavedesc['comm_type'] == 1: wavedesc['dtype'] = np.int16() else: raise Exception('unknown comm_type.') return wavedesc
def create_mock_tfrecord(): pil_image = Image.fromarray(np.array([[[123, 0, 0]]], dtype=np.uint8), 'RGB') image_output_stream = StringIO.BytesIO() print(type(image_output_stream)) pil_image.save(image_output_stream, format='png') encoded_image = image_output_stream.getvalue() feature_map = { 'test_field': dataset_util.float_list_feature([1, 2, 3, 4]), standard_fields.TfExampleFields.image_encoded: dataset_util.bytes_feature(encoded_image), } tf_example = tf.train.Example(features=tf.train.Features(feature=feature_map)) with tf.python_io.TFRecordWriter(get_mock_tfrecord_path()) as writer: writer.write(tf_example.SerializeToString())
def extract_sound(self, start=0, end=None): if not start and not end: raise ValueError start_pos = self.to_index(start) if end: end_pos = self.to_index(end) else: end_pos = len(self.raw) _buffer = io.BytesIO() _output = wave.open(_buffer, "wb") _output.setnchannels(self.channels) _output.setsampwidth(self.samplewidth) _output.setframerate(self.framerate) raw = self.raw[start_pos:end_pos] _output.writeframes(self.raw[start_pos:end_pos]) _output.close() _buffer.seek(0) return Sound(_buffer)
def after_request(self, response): accept_encoding = request.headers.get('Accept-Encoding', '') if response.status_code < 200 or \ response.status_code >= 300 or \ response.direct_passthrough or \ len(response.data) < self.minimum_size or \ 'gzip' not in accept_encoding.lower() or \ 'Content-Encoding' in response.headers: return response gzip_buffer = StringIO.BytesIO() gzip_file = gzip.GzipFile(mode='wb', compresslevel=self.compress_level, fileobj=gzip_buffer) gzip_file.write(response.data) gzip_file.close() response.data = gzip_buffer.getvalue() response.headers['Content-Encoding'] = 'gzip' response.headers['Content-Length'] = len(response.data) return response
def gzip_decode(z): return gzip.GzipFile(fileobj=io.BytesIO(z), mode="rb").read()
def gzip_encode(s): z = io.BytesIO() with gzip.GzipFile(fileobj=z, mode="wb") as f: f.write(s) z.seek(0) return z.read()