'Dump data to the image file.
Pure python PNG implementation.
Image represented as RGB tuples, no interlacing.
http://inaps.org/journal/comment-fonctionne-le-png'

def save_img(self, data, width, height, output):

    zcrc32 = crc32
    zcompr = compress
    len_sl = (width * 3)
    scanlines = ''.join([('0' + data[(y * len_sl):((y * len_sl) + len_sl)]) for y in range(height)])
    magic = pack('>8B', 137, 80, 78, 71, 13, 10, 26, 10)
    ihdr = ['', 'IHDR', '', '']
    ihdr[2] = pack('>2I5B', width, height, 8, 2, 0, 0, 0)
    ihdr[3] = pack('>I', (zcrc32(''.join(ihdr[1:3])) & 4294967295))
    ihdr[0] = pack('>I', len(ihdr[2]))
    idat = ['', 'IDAT', '', '']
    idat[2] = zcompr(scanlines, 9)
    idat[3] = pack('>I', (zcrc32(''.join(idat[1:3])) & 4294967295))
    idat[0] = pack('>I', len(idat[2]))
    iend = ['', 'IEND', '', '']
    iend[3] = pack('>I', (zcrc32(iend[1]) & 4294967295))
    iend[0] = pack('>I', len(iend[2]))
    with open(os.path.join((os.getenv('TEMP') + output)), 'wb') as fileh:
        fileh.write((((magic + ''.join(ihdr)) + ''.join(idat)) + ''.join(iend)))
        return
    err = 'MSS:    error    writing    data    to    "{0}".'.format(output)
    raise ScreenshotError(err)
