我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用numpy.getbuffer()。
def __init__(self, hash_name='md5', coerce_mmap=False): """ Parameters ---------- hash_name: string The hash algorithm to be used coerce_mmap: boolean Make no difference between np.memmap and np.ndarray objects. """ self.coerce_mmap = coerce_mmap Hasher.__init__(self, hash_name=hash_name) # delayed import of numpy, to avoid tight coupling import numpy as np self.np = np if hasattr(np, 'getbuffer'): self._getbuffer = np.getbuffer else: self._getbuffer = memoryview
def togglepattern(): global togsw,o,ovl,gui,alphaValue # if overlay is inactive, ignore button: if togsw == 0: print "Pattern button pressed, but ignored --- Crosshair not visible." # if overlay is active, drop it, change pattern, then show it again else: if guivisible == 0: # reinitialize array: ovl = np.zeros((height, width, 3), dtype=np.uint8) patternswitch(ovl,0) if o != None: camera.remove_overlay(o) o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue) else: # reinitialize array gui = np.zeros((height, width, 3), dtype=np.uint8) creategui(gui) patternswitch(gui,1) if o != None: camera.remove_overlay(o) o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue) return
def toggleonoff(): global togsw,o,alphaValue if togsw == 1: print "Toggle Crosshair OFF" if o != None: camera.remove_overlay(o) togsw = 0 else: print "Toggle Crosshair ON" if guivisible == 0: o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue) else: o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue) togsw = 1 return # function
def togglepatternZoomOut(): global togsw,o,curpat,col,ovl,gui,alphaValue # if overlay is inactive, ignore button: if togsw == 0: zoom_out() else: if guivisible == 0: zoom_out() # reinitialize array: ovl = np.zeros((height, width, 3), dtype=np.uint8) patternswitcherZoomOut(ovl,0) o = camera.add_overlay(np.getbuffer(ovl), layer=3, alpha=alphaValue) else: zoom_out() # reinitialize array gui = np.zeros((height, width, 3), dtype=np.uint8) creategui(gui) patternswitcherZoomOut(gui,1) o = camera.add_overlay(np.getbuffer(gui), layer=3, alpha=alphaValue) return
def numpy_buffer(ndarray): """Creates a buffer from c_contiguous numpy ndarray.""" # Credits to: https://github.com/joblib/joblib/blob/04b001861e1dd03a857b7b419c336de64e05714c/joblib/hashing.py if isinstance(ndarray, (pd.Series, pd.DataFrame)): ndarray = ndarray.values if ndarray.flags.c_contiguous: obj_c_contiguous = ndarray elif ndarray.flags.f_contiguous: obj_c_contiguous = ndarray.T else: obj_c_contiguous = ndarray.flatten() obj_c_contiguous = obj_c_contiguous.view(np.uint8) if hasattr(np, 'getbuffer'): return np.getbuffer(obj_c_contiguous) else: return memoryview(obj_c_contiguous)
def __call__(self): plte = self.plte a = self.idRaster #a = array('B') #unsigned 1byte int #a.fromlist(idRaster) ### Why is this here? byteswap has no effect on 'uint8' #if SYSTEM_BYTEORDER == '\x00': #little # a.byteswap() #to big (network) # This is used directly in the final PNG, must be zlib. cIdRaster = zlib.compress(numpy.getbuffer(a)) size = pack('!I',len(cIdRaster)) crc = pack('!I',crc32('IDAT'+cIdRaster) & 0xFFFFFFFF) idat = size+'IDAT'+cIdRaster+crc #b = array(UNSIGNED_ITEM_TYPES[4]) #unsigned 4byte int #b.fromlist(plte) #cPLTE = zlib.compress(plte.tostring()) cPLTE = zlib.compress(numpy.getbuffer(plte)) s = SYSTEM_BYTEORDER + pack('!LL',len(idat),len(cPLTE)) # 9bytes <--- ByteOrder(1byte), len of idat(4bytes), len of cPLTE(4bytes) s = s+idat+cPLTE return s
def patternswitch(target,guitoggle): global o, alphaValue toggleonoff() if guitoggle == 1: creategui(gui) o = camera.add_overlay(np.getbuffer(target), layer=3, alpha=alphaValue) return
def hash_from_code(msg): try: return hashlib.md5(msg).hexdigest() except TypeError: assert isinstance(msg, numpy.ndarray) return hashlib.md5(numpy.getbuffer(msg)).hexdigest()
def __call__(self): if self.maxID < 2**16: dtype = 'uint16' #UNSIGNED_ITEM_TYPES[2] #UNSIGNED_2_BYTE_INT DTYPE = '\x00' else: dtype = 'uint32' #UNSIGNED_ITEM_TYPES[4] #UNSIGNED_4_BYTE_INT DTYPE = '\x01' a = self.intRaster.astype(dtype) #return SYSTEM_BYTEORDER+DTYPE+zlib.compress(a.tostring()) return SYSTEM_BYTEORDER+DTYPE+zlib.compress(numpy.getbuffer(a))