Python array.array.tostring() Examples

The following are 30 code examples of array.array.tostring(). 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 array.array , or try the search function .
Example #1
Source File: png.py    From stegoVeritas with GNU General Public License v2.0 6 votes vote down vote up
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 
Example #2
Source File: png.py    From blenderseed with MIT License 6 votes vote down vote up
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 
Example #3
Source File: png.py    From sublime-markdown-popups with MIT License 6 votes vote down vote up
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 
Example #4
Source File: png.py    From coastermelt with MIT License 6 votes vote down vote up
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 
Example #5
Source File: png.py    From GBVideoPlayer with MIT License 6 votes vote down vote up
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 
Example #6
Source File: png.py    From GDMC with ISC License 6 votes vote down vote up
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 
Example #7
Source File: png.py    From GDMC with ISC License 5 votes vote down vote up
def iterboxed(self, rows):
        """Iterator that yields each scanline in boxed row flat pixel
        format.  `rows` should be an iterator that yields the bytes of
        each row in turn.
        """

        def asvalues(raw):
            """Convert a row of raw bytes into a flat row.  Result may
            or may not share with argument"""

            if self.bitdepth == 8:
                return raw
            if self.bitdepth == 16:
                raw = tostring(raw)
                return array('H', struct.unpack('!%dH' % (len(raw) // 2), raw))
            assert self.bitdepth < 8
            width = self.width
            # Samples per byte
            spb = 8 // self.bitdepth
            out = array('B')
            mask = 2 ** self.bitdepth - 1
            shifts = map(self.bitdepth.__mul__, reversed(range(spb)))
            for o in raw:
                out.extend(map(lambda i: mask & (o >> i), shifts))
            return out[:width]

        return itertools.imap(asvalues, rows) 
Example #8
Source File: png.py    From stegoVeritas with GNU General Public License v2.0 5 votes vote down vote up
def read(self, n):
        r = self.buf[self.offset:self.offset+n]
        if isarray(r):
            r = r.tostring()
        self.offset += n
        return r 
Example #9
Source File: png.py    From stegoVeritas with GNU General Public License v2.0 5 votes vote down vote up
def iterboxed(self, rows):
        """Iterator that yields each scanline in boxed row flat pixel
        format.  `rows` should be an iterator that yields the bytes of
        each row in turn.
        """

        def asvalues(raw):
            """Convert a row of raw bytes into a flat row.  Result may
            or may not share with argument"""

            if self.bitdepth == 8:
                return raw
            if self.bitdepth == 16:
                raw = tostring(raw)
                return array('H', struct.unpack('!%dH' % (len(raw)//2), raw))
            assert self.bitdepth < 8
            width = self.width
            # Samples per byte
            spb = 8//self.bitdepth
            out = array('B')
            mask = 2**self.bitdepth - 1
            shifts = list(map(self.bitdepth.__mul__, reversed(list(range(spb)))))
            for o in raw:
                out.extend([mask&(o>>i) for i in shifts])
            return out[:width]

        return map(asvalues, rows) 
Example #10
Source File: png.py    From stegoVeritas with GNU General Public License v2.0 5 votes vote down vote up
def testTrnsArray(self):
        """Test that reading a type 2 PNG with tRNS chunk yields each
        row as an array (using asDirect)."""
        r = Reader(bytes=_pngsuite['tbrn2c08'])
        list(r.asDirect()[2])[0].tostring

    # Invalid file format tests.  These construct various badly
    # formatted PNG files, then feed them into a Reader.  When
    # everything is working properly, we should get FormatError
    # exceptions raised. 
Example #11
Source File: png.py    From GDMC with ISC License 5 votes vote down vote up
def tostring(row):
        l = len(row)
        return struct.pack('%dB' % l, *row) 
Example #12
Source File: png.py    From GDMC with ISC License 5 votes vote down vote up
def tostring(row):
        """Convert row of bytes to string.  Expects `row` to be an
        ``array``.
        """
        return row.tostring() 
Example #13
Source File: png.py    From GDMC with ISC License 5 votes vote down vote up
def read(self, n):
        r = self.buf[self.offset:self.offset + n]
        if isarray(r):
            r = r.tostring()
        self.offset += n
        return r 
Example #14
Source File: png.py    From stegoVeritas with GNU General Public License v2.0 5 votes vote down vote up
def tostring(row):
        l = len(row)
        return struct.pack('%dB' % l, *row) 
Example #15
Source File: png.py    From GDMC with ISC License 5 votes vote down vote up
def testTrnsArray(self):
        """Test that reading a type 2 PNG with tRNS chunk yields each
        row as an array (using asDirect)."""
        r = Reader(bytes=_pngsuite['tbrn2c08'])
        list(r.asDirect()[2])[0].tostring

    # Invalid file format tests.  These construct various badly
    # formatted PNG files, then feed them into a Reader.  When
    # everything is working properly, we should get FormatError
    # exceptions raised. 
Example #16
Source File: png.py    From sublime-markdown-popups with MIT License 5 votes vote down vote up
def tostring(row):
        l = len(row)
        return struct.pack('%dB' % l, *row) 
Example #17
Source File: png.py    From sublime-markdown-popups with MIT License 5 votes vote down vote up
def tostring(row):
        """Convert row of bytes to string.  Expects `row` to be an
        ``array``.
        """
        return row.tostring()

# Conditionally convert to bytes.  Works on Python 2 and Python 3. 
Example #18
Source File: png.py    From sublime-markdown-popups with MIT License 5 votes vote down vote up
def read(self, n):
        r = self.buf[self.offset:self.offset+n]
        if isarray(r):
            r = r.tostring()
        self.offset += n
        return r 
Example #19
Source File: png.py    From sublime-markdown-popups with MIT License 5 votes vote down vote up
def iterboxed(self, rows):
        """Iterator that yields each scanline in boxed row flat pixel
        format.  `rows` should be an iterator that yields the bytes of
        each row in turn.
        """

        def asvalues(raw):
            """Convert a row of raw bytes into a flat row.  Result may
            or may not share with argument"""

            if self.bitdepth == 8:
                return raw
            if self.bitdepth == 16:
                raw = tostring(raw)
                return array('H', struct.unpack('!%dH' % (len(raw)//2), raw))
            assert self.bitdepth < 8
            width = self.width
            # Samples per byte
            spb = 8//self.bitdepth
            out = array('B')
            mask = 2**self.bitdepth - 1
            shifts = list(map(self.bitdepth.__mul__, reversed(list(range(spb)))))
            for o in raw:
                out.extend([mask&(o>>i) for i in shifts])
            return out[:width]

        return map(asvalues, rows) 
Example #20
Source File: png.py    From sublime-markdown-popups with MIT License 5 votes vote down vote up
def testTrnsArray(self):
        """Test that reading a type 2 PNG with tRNS chunk yields each
        row as an array (using asDirect)."""
        r = Reader(bytes=_pngsuite['tbrn2c08'])
        list(r.asDirect()[2])[0].tostring

    # Invalid file format tests.  These construct various badly
    # formatted PNG files, then feed them into a Reader.  When
    # everything is working properly, we should get FormatError
    # exceptions raised. 
Example #21
Source File: png.py    From stegoVeritas with GNU General Public License v2.0 5 votes vote down vote up
def tostring(row):
        """Convert row of bytes to string.  Expects `row` to be an
        ``array``.
        """
        return row.tostring()

# Conditionally convert to bytes.  Works on Python 2 and Python 3. 
Example #22
Source File: png.py    From blenderseed with MIT License 5 votes vote down vote up
def tostring(row):
            l = len(row)
            return struct.pack('%dB' % l, *row) 
Example #23
Source File: png.py    From coastermelt with MIT License 5 votes vote down vote up
def testTrnsArray(self):
        """Test that reading a type 2 PNG with tRNS chunk yields each
        row as an array (using asDirect)."""
        r = Reader(bytes=_pngsuite['tbrn2c08'])
        list(r.asDirect()[2])[0].tostring

    # Invalid file format tests.  These construct various badly
    # formatted PNG files, then feed them into a Reader.  When
    # everything is working properly, we should get FormatError
    # exceptions raised. 
Example #24
Source File: png.py    From coastermelt with MIT License 5 votes vote down vote up
def iterboxed(self, rows):
        """Iterator that yields each scanline in boxed row flat pixel
        format.  `rows` should be an iterator that yields the bytes of
        each row in turn.
        """

        def asvalues(raw):
            """Convert a row of raw bytes into a flat row.  Result may
            or may not share with argument"""

            if self.bitdepth == 8:
                return raw
            if self.bitdepth == 16:
                raw = tostring(raw)
                return array('H', struct.unpack('!%dH' % (len(raw)//2), raw))
            assert self.bitdepth < 8
            width = self.width
            # Samples per byte
            spb = 8//self.bitdepth
            out = array('B')
            mask = 2**self.bitdepth - 1
            shifts = map(self.bitdepth.__mul__, reversed(range(spb)))
            for o in raw:
                out.extend(map(lambda i: mask&(o>>i), shifts))
            return out[:width]

        return itertools.imap(asvalues, rows) 
Example #25
Source File: png.py    From coastermelt with MIT License 5 votes vote down vote up
def read(self, n):
        r = self.buf[self.offset:self.offset+n]
        if isarray(r):
            r = r.tostring()
        self.offset += n
        return r 
Example #26
Source File: png.py    From coastermelt with MIT License 5 votes vote down vote up
def tostring(row):
        """Convert row of bytes to string.  Expects `row` to be an
        ``array``.
        """
        return row.tostring()

# Conditionally convert to bytes.  Works on Python 2 and Python 3. 
Example #27
Source File: png.py    From coastermelt with MIT License 5 votes vote down vote up
def tostring(row):
        l = len(row)
        return struct.pack('%dB' % l, *row) 
Example #28
Source File: png.py    From GBVideoPlayer with MIT License 5 votes vote down vote up
def serialtoflat(self, bytes, width=None):
        """Convert serial format (byte stream) pixel data to flat row
        flat pixel.
        """

        if self.bitdepth == 8:
            return bytes
        if self.bitdepth == 16:
            bytes = tostring(bytes)
            return array('H',
              struct.unpack('!%dH' % (len(bytes)//2), bytes))
        assert self.bitdepth < 8
        if width is None:
            width = self.width
        # Samples per byte
        spb = 8//self.bitdepth
        out = array('B')
        mask = 2**self.bitdepth - 1
        shifts = map(self.bitdepth.__mul__, reversed(range(spb)))
        l = width
        for o in bytes:
            out.extend([(mask&(o>>s)) for s in shifts][:l])
            l -= spb
            if l <= 0:
                l = width
        return out 
Example #29
Source File: png.py    From GBVideoPlayer with MIT License 5 votes vote down vote up
def read(self, n):
        r = self.buf[self.offset:self.offset+n]
        if isarray(r):
            r = r.tostring()
        self.offset += n
        return r 
Example #30
Source File: png.py    From GBVideoPlayer with MIT License 5 votes vote down vote up
def tostring(row):
        """ Python3 definition, array.tostring() is deprecated in Python3
        """
        return row.tobytes()

# Conditionally convert to bytes.  Works on Python 2 and Python 3.