def load(image, filename): """load an XPM file""" colors = cpp = count = None state = _WANT_XPM palette = {} index = 0 with open(filename, "rt", encoding="ascii") as file: for lino, line in enumerate(file, start=1): line = line.strip() if not line or (line.startswith(("/*", "//")) and state != _WANT_XPM): continue # if branches are ordered by frequency of occurrence if state == _WANT_COLOR: count, state = _parse_color(lino, line, palette, cpp, count) if state == _WANT_PIXELS: count = image.height elif state == _WANT_PIXELS: count, state, index = _parse_pixels(lino, line, image.pixels, palette, cpp, count, index) if state == _DONE: break elif state == _WANT_XPM: state = _parse_xpm(lino, line) elif state == _WANT_NAME: state = _parse_name(lino, line) elif state == _WANT_VALUES: colors, cpp, count, state = _parse_values(lino, line, image) image.pixels = Image.create_array(image.width, image.height)
def load(image, filename): """load an XPM file""" colors = cpp = count = None state = _WANT_XPM palette = {} index = 0 with open(filename, "rt", encoding="ascii") as file: for lino, line in enumerate(file, start=1): line = line.strip() if not line or (line.startswith( ("/*", "//")) and state != _WANT_XPM): continue # if branches are ordered by frequency of occurrence if state == _WANT_COLOR: count, state = _parse_color(lino, line, palette, cpp, count) if state == _WANT_PIXELS: count = image.height elif state == _WANT_PIXELS: count, state, index = _parse_pixels(lino, line, image.pixels, palette, cpp, count, index) if state == _DONE: break elif state == _WANT_XPM: state = _parse_xpm(lino, line) elif state == _WANT_NAME: state = _parse_name(lino, line) elif state == _WANT_VALUES: colors, cpp, count, state = _parse_values(lino, line, image) image.pixels = Image.create_array(image.width, image.height)
def load(image, filename): """load a PNG file""" reader = png.Reader(filename=filename) image.width, image.height, pixels, _ = reader.asRGBA8() image.pixels = Image.create_array(image.width, image.height) index = 0 for row in pixels: for r, g, b, α in zip(row[::4], row[1::4], row[2::4], row[3::4]): image.pixels[index] = Image.color_for_argb(α, r, g, b) index += 1
def load(image, filename): """load an XBM file""" with open(filename, "rb") as file: xbm = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) i = xbm.find(_DEFINE) j = xbm.find(_BITS) if i == -1 or j == -1: raise Image.Error("failed to parse '{}'".format(filename)) _parse_defines(image, xbm[i:j]) image.pixels = Image.create_array(image.width, image.height, Image.ColorForName["white"]) _parse_bits(image, xbm[j + len(_BITS):])