本文整理汇总了Python中array.array.array方法的典型用法代码示例。如果您正苦于以下问题:Python array.array方法的具体用法?Python array.array怎么用?Python array.array使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类array.array
的用法示例。
在下文中一共展示了array.array方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: serialize_numpy
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def serialize_numpy(m, t):
'''
serialize_numpy(m, type) converts the numpy array m into a byte stream that can be read by the
nben.util.Py4j Java class. The function assumes that the type of the array needn't be encoded
in the bytearray itself. The bytearray will begin with an integer, the number of dimensions,
followed by that number of integers (the dimension sizes themselves) then the bytes of the
array, flattened.
The argument type gives the type of the array to be transferred and must be 'i' for integer or
'd' for double (or any other string accepted by array.array()).
'''
# Start with the header: <number of dimensions> <dim1-size> <dim2-size> ...
header = array('i', [len(m.shape)] + list(m.shape))
# Now, we can do the array itself, just flattened
body = array(t, m.flatten().tolist())
# Wrap bytes if necessary...
if sys.byteorder != 'big':
header.byteswap()
body.byteswap()
# And return the result:
return bytearray(header.tostring() + body.tostring())
示例2: fit_transform
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def fit_transform(self, X, y=None):
"""Learn a list of feature name -> indices mappings and transform X.
Like fit(X) followed by transform(X), but does not require
materializing X in memory.
Parameters
----------
X : Mapping or iterable over Mappings
Dict(s) or Mapping(s) from feature names (arbitrary Python
objects) to feature values (strings or convertible to dtype).
y : (ignored)
Returns
-------
Xa : {array, sparse matrix}
Feature vectors; always 2-d.
"""
return self._transform(X, fitting=True)
示例3: make_palette
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def make_palette(self):
"""Create the byte sequences for a ``PLTE`` and if necessary a
``tRNS`` chunk. Returned as a pair (*p*, *t*). *t* will be
``None`` if no ``tRNS`` chunk is necessary.
"""
p = array('B')
t = array('B')
for x in self.palette:
p.extend(x[0:3])
if len(x) > 3:
t.append(x[3])
p = tostring(p)
t = tostring(t)
if t:
return p,t
return p,None
示例4: convert_ppm_and_pgm
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def convert_ppm_and_pgm(self, ppmfile, pgmfile, outfile):
"""
Convert a PPM and PGM file containing raw pixel data into a
PNG outfile with the parameters set in the writer object.
"""
pixels = array('B')
pixels.fromfile(ppmfile,
(self.bitdepth/8) * self.color_planes *
self.width * self.height)
apixels = array('B')
apixels.fromfile(pgmfile,
(self.bitdepth/8) *
self.width * self.height)
pixels = interleave_planes(pixels, apixels,
(self.bitdepth/8) * self.color_planes,
(self.bitdepth/8))
if self.interlace:
self.write_passes(outfile, self.array_scanlines_interlace(pixels))
else:
self.write_passes(outfile, self.array_scanlines(pixels))
示例5: file_scanlines
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def file_scanlines(self, infile):
"""
Generates boxed rows in flat pixel format, from the input file
`infile`. It assumes that the input file is in a "Netpbm-like"
binary format, and is positioned at the beginning of the first
pixel. The number of pixels to read is taken from the image
dimensions (`width`, `height`, `planes`) and the number of bytes
per value is implied by the image `bitdepth`.
"""
# Values per row
vpr = self.width * self.planes
row_bytes = vpr
if self.bitdepth > 8:
assert self.bitdepth == 16
row_bytes *= 2
fmt = '>%dH' % vpr
def line():
return array('H', struct.unpack(fmt, infile.read(row_bytes)))
else:
def line():
scanline = array('B', infile.read(row_bytes))
return scanline
for y in range(self.height):
yield line()
示例6: read_flat
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def read_flat(self):
"""
Read a PNG file and decode it into flat row flat pixel format.
Returns (*width*, *height*, *pixels*, *metadata*).
May use excessive memory.
`pixels` are returned in flat row flat pixel format.
See also the :meth:`read` method which returns pixels in the
more stream-friendly boxed row flat pixel format.
"""
x, y, pixel, meta = self.read()
arraycode = 'BH'[meta['bitdepth']>8]
pixel = array(arraycode, itertools.chain(*pixel))
return x, y, pixel, meta
示例7: palette
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def palette(self, alpha='natural'):
"""Returns a palette that is a sequence of 3-tuples or 4-tuples,
synthesizing it from the ``PLTE`` and ``tRNS`` chunks. These
chunks should have already been processed (for example, by
calling the :meth:`preamble` method). All the tuples are the
same size: 3-tuples if there is no ``tRNS`` chunk, 4-tuples when
there is a ``tRNS`` chunk. Assumes that the image is colour type
3 and therefore a ``PLTE`` chunk is required.
If the `alpha` argument is ``'force'`` then an alpha channel is
always added, forcing the result to be a sequence of 4-tuples.
"""
if not self.plte:
raise FormatError(
"Required PLTE chunk is missing in colour type 3 image.")
plte = group(array('B', self.plte), 3)
if self.trns or alpha == 'force':
trns = array('B', self.trns or '')
trns.extend([255]*(len(plte)-len(trns)))
plte = map(operator.add, plte, group(trns, 1))
return plte
示例8: palette
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def palette(self, alpha='natural'):
"""Returns a palette that is a sequence of 3-tuples or 4-tuples,
synthesizing it from the ``PLTE`` and ``tRNS`` chunks. These
chunks should have already been processed (for example, by
calling the :meth:`preamble` method). All the tuples are the
same size: 3-tuples if there is no ``tRNS`` chunk, 4-tuples when
there is a ``tRNS`` chunk. Assumes that the image is colour type
3 and therefore a ``PLTE`` chunk is required.
If the `alpha` argument is ``'force'`` then an alpha channel is
always added, forcing the result to be a sequence of 4-tuples.
"""
if not self.plte:
raise FormatError(
"Required PLTE chunk is missing in colour type 3 image.")
plte = group(array('B', self.plte), 3)
if self.trns or alpha == 'force':
trns = array('B', self.trns or '')
trns.extend([255]*(len(plte)-len(trns)))
plte = list(map(operator.add, plte, group(trns, 1)))
return plte
示例9: convert_ppm_and_pgm
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def convert_ppm_and_pgm(self, ppmfile, pgmfile, outfile):
"""
Convert a PPM and PGM file containing raw pixel data into a
PNG outfile with the parameters set in the writer object.
"""
pixels = array('B')
pixels.fromfile(ppmfile,
(self.bitdepth / 8) * self.color_planes *
self.width * self.height)
apixels = array('B')
apixels.fromfile(pgmfile,
(self.bitdepth / 8) *
self.width * self.height)
pixels = interleave_planes(pixels, apixels,
(self.bitdepth / 8) * self.color_planes,
(self.bitdepth / 8))
if self.interlace:
self.write_passes(outfile, self.array_scanlines_interlace(pixels))
else:
self.write_passes(outfile, self.array_scanlines(pixels))
示例10: read_flat
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def read_flat(self):
"""
Read a PNG file and decode it into flat row flat pixel format.
Returns (*width*, *height*, *pixels*, *metadata*).
May use excessive memory.
`pixels` are returned in flat row flat pixel format.
See also the :meth:`read` method which returns pixels in the
more stream-friendly boxed row flat pixel format.
"""
x, y, pixel, meta = self.read()
arraycode = 'BH'[meta['bitdepth'] > 8]
pixel = array(arraycode, itertools.chain(*pixel))
return x, y, pixel, meta
示例11: isinteger
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def isinteger(x):
"""Check if `x` is platform native integer"""
try:
return int(x) == x
except (TypeError, ValueError):
return False
# === Legacy Version Support ===
# In order to work on Python 2.3 we fix up a recurring annoyance involving
# the array type. In Python 2.3 an array cannot be initialised with an
# array, and it cannot be extended with a list (or other sequence).
# Both of those are repeated issues in the code. Whilst I would not
# normally tolerate this sort of behaviour, here we "shim" a replacement
# for array into place (and hope no-one notices). You never read this.
示例12: make_palette
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def make_palette(self):
"""Create the byte sequences for a ``PLTE`` and if necessary a
``tRNS`` chunk. Returned as a pair (*p*, *t*). *t* will be
``None`` if no ``tRNS`` chunk is necessary.
"""
p = array(str('B'))
t = array(str('B'))
for x in self.palette:
p.extend(x[0:3])
if len(x) > 3:
t.append(x[3])
p = tostring(p)
t = tostring(t)
if t:
return p,t
return p,None
示例13: convert_ppm_and_pgm
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def convert_ppm_and_pgm(self, ppmfile, pgmfile, outfile):
"""
Convert a PPM and PGM file containing raw pixel data into a
PNG outfile with the parameters set in the writer object.
"""
pixels = array(str('B'))
pixels.fromfile(ppmfile,
(self.bitdepth/8) * self.color_planes *
self.width * self.height)
apixels = array(str('B'))
apixels.fromfile(pgmfile,
(self.bitdepth/8) *
self.width * self.height)
pixels = interleave_planes(pixels, apixels,
(self.bitdepth/8) * self.color_planes,
(self.bitdepth/8))
if self.interlace:
self.write_passes(outfile, self.array_scanlines_interlace(pixels))
else:
self.write_passes(outfile, self.array_scanlines(pixels))
示例14: file_scanlines
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def file_scanlines(self, infile):
"""
Generates boxed rows in flat pixel format, from the input file
`infile`. It assumes that the input file is in a "Netpbm-like"
binary format, and is positioned at the beginning of the first
pixel. The number of pixels to read is taken from the image
dimensions (`width`, `height`, `planes`) and the number of bytes
per value is implied by the image `bitdepth`.
"""
# Values per row
vpr = self.width * self.planes
row_bytes = vpr
if self.bitdepth > 8:
assert self.bitdepth == 16
row_bytes *= 2
fmt = '>%dH' % vpr
def line():
return array(str('H'), struct.unpack(fmt, infile.read(row_bytes)))
else:
def line():
scanline = array(str('B'), infile.read(row_bytes))
return scanline
for y in range(self.height):
yield line()
示例15: to_java_doubles
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import array [as 别名]
def to_java_doubles(m):
'''
to_java_doubles(m) yields a java array object for the vector or matrix m.
'''
global _java
if _java is None: _init_registration()
m = np.asarray(m)
dims = len(m.shape)
if dims > 2: raise ValueError('1D and 2D arrays supported only')
bindat = serialize_numpy(m, 'd')
return (_java.jvm.nben.util.Numpy.double2FromBytes(bindat) if dims == 2
else _java.jvm.nben.util.Numpy.double1FromBytes(bindat))