Exemple #1
0
 def open_image(fobj):
     return Image(
         RGBA,
         1,
         1,
         loader,
     )
Exemple #2
0
def image_factory(colors, alpha=True):
    height = len(colors)
    width = len(colors[0]) if height else 0
    pixel_size = 4 if alpha else 3
    pixel_array = pixel_array_factory(colors, alpha)
    def loader():
        return pixel_array, None
    return Image(ColorType(pixel_size, alpha), width, height, loader)
Exemple #3
0
 def test_new(self):
     img = Image.new(RGBA, 5, 5, Black)
     self.assertImage(img, [
         [Black, Black, Black, Black, Black],
         [Black, Black, Black, Black, Black],
         [Black, Black, Black, Black, Black],
         [Black, Black, Black, Black, Black],
         [Black, Black, Black, Black, Black],
     ])
Exemple #4
0
 def test_new(self):
     img = Image.new(5, 5, Black, RGBA)
     self.assertImage(img, [
         [Black, Black, Black, Black, Black],
         [Black, Black, Black, Black, Black],
         [Black, Black, Black, Black, Black],
         [Black, Black, Black, Black, Black],
         [Black, Black, Black, Black, Black],
         ])
Exemple #5
0
    def test_delayed_loading(self):
        pixel_array = pixel_array_factory([
            [Black]
        ])
        class Loader(object):
            def __init__(self):
                self.callcount = 0

            def __call__(self):
                self.callcount += 1
                return pixel_array, None
        loader = Loader()
        image = Image(RGBA, 1, 1, loader)
        self.assertEqual(loader.callcount, 0)
        image.set_color(0, 0, White)
        self.assertEqual(loader.callcount, 1)
        image.flip_left_right()
        self.assertEqual(loader.callcount, 1)
Exemple #6
0
def image_factory(colors, alpha=True):
    height = len(colors)
    width = len(colors[0]) if height else 0
    pixel_size = 4 if alpha else 3
    pixel_array = get_pixel_array(
        array.array('B', [0] * width * height * pixel_size), width, height,
        pixel_size)
    for y in range(height):
        for x in range(width):
            pixel_array.set(x, y, colors[y][x].to_pixel(pixel_size))
    return Image(pixel_array, ColorType(pixel_size, alpha))
Exemple #7
0
 def test_format_registration(self):
     def loader():
         return pixel_array_factory([[Black]]), None
     def open_image(fobj):
         return Image(
             RGBA,
             1,
             1,
             loader,
         )
     def save_image(image, fobj):
         fobj.write('saved')
     register(Format(open_image, save_image, ['test']))
     img = Image.open(StringIO())
     self.assertIsInstance(img, Image)
     self.assertEqual(img.width, 1)
     self.assertEqual(img.height, 1)
     self.assertEqual(img.get_color(0, 0), Black)
     sio = StringIO()
     img.save(sio, 'test')
     self.assertEqual(sio.getvalue(), 'saved')
Exemple #8
0
def decode(fileobj):
    decoder = TonyJpegDecoder()
    jpegsrc = fileobj.read()
    try:
        bmpout = decoder.decode(jpegsrc)
    except:
        fileobj.seek(0)
        return None
    pixels = array.array('B')
    row_width = decoder.Width * PIXELSIZE
    # rows are bottom to top
    for reversed_row_num in range(decoder.Height - 1, -1, -1):
        start = reversed_row_num * (row_width + 2)
        end = start + row_width
        pixels.extend(bmpout[start:end])
        #pixels.extend(bmpout[:3 * decoder.Width])
        #del bmpout[:3 * decoder.Width]
        #del bmpout[:2] # kill padding
    pixel_array = get_pixel_array(pixels, decoder.Width, decoder.Height,
                                  PIXELSIZE)
    return Image(pixel_array, RGB)
Exemple #9
0
    def test_delayed_loading(self):
        pixel_array = pixel_array_factory([[Black]])

        class Loader(object):
            def __init__(self):
                self.callcount = 0

            def __call__(self):
                self.callcount += 1
                return pixel_array, None

        loader = Loader()
        image = Image(RGBA, 1, 1, loader)
        self.assertEqual(loader.callcount, 0)
        image.set_color(0, 0, White)
        self.assertEqual(loader.callcount, 1)
        image.flip_left_right()
        self.assertEqual(loader.callcount, 1)
Exemple #10
0
    def test_format_registration(self):
        def loader():
            return pixel_array_factory([[Black]]), None

        def open_image(fobj):
            return Image(
                RGBA,
                1,
                1,
                loader,
            )

        def save_image(image, fobj):
            fobj.write('saved')

        register(Format(open_image, save_image, ['test']))
        img = Image.open(StringIO())
        self.assertIsInstance(img, Image)
        self.assertEqual(img.width, 1)
        self.assertEqual(img.height, 1)
        self.assertEqual(img.get_color(0, 0), Black)
        sio = StringIO()
        img.save(sio, 'test')
        self.assertEqual(sio.getvalue(), 'saved')
Exemple #11
0
                psd.header.height - (layer.bbox.y1 + start[1])
            )
            angle = math.atan2(start[1] - end[1], start[0] - end[0]) - 1.570796
            offsets.write(
                "    Offset { pos: Vec2::<GLfloat> { x: %i.0, y: %i.0 }, angle: %f },\n" % (abs_pos[0], abs_pos[1], angle)
            )

        if x + psd.header.width > width:
            y += psd.header.height
            x = 0
        else:
            x += psd.header.width

    offsets.write("];\n")
else:
    atlas = Image.new(colors.RGBA, width, height, Color.from_hexcode("#00000000"))

    metadata_filename = outpath + ".info"
    metadata = open(metadata_filename, "w")
    currentframe = None

    for name, number, index, layer in frames:
        if name != currentframe:
            currentframe = name
            metadata.write("%s frame %i offset = %i\n" % (name, number, index))

        atlas.blit(y + layer.bbox.y1, x + layer.bbox.x1, layer.as_pymaging())

        if x + psd.header.width > width:
            y += psd.header.height
            x = 0
Exemple #12
0
def bench_pymaging(context):
    with open(filename) as fobj:
        Image.open(fobj).resize(100, 100).save_to_path(context['tempfile'] + '.png')
Exemple #13
0
 def get_image(self):
     self.read_headers()
     return Image(self.mode, self.width, self.height, self.load,
                  {'source_format': 'png'})
def bench_pymaging(context):
    Image.open_from_path(filename).save_to_path(context["tempfile"] + ".png")
Exemple #15
0
                # NOTE game coords start from bottom left instead of top left
                psd.header.height - (layer.bbox.y1 + start[1]))
            angle = math.atan2(start[1] - end[1], start[0] - end[0]) - 1.570796
            offsets.write(
                "    Offset { pos: Vec2::<GLfloat> { x: %i.0, y: %i.0 }, angle: %f },\n"
                % (abs_pos[0], abs_pos[1], angle))

        if x + psd.header.width > width:
            y += psd.header.height
            x = 0
        else:
            x += psd.header.width

    offsets.write("];\n")
else:
    atlas = Image.new(colors.RGBA, width, height,
                      Color.from_hexcode("#00000000"))

    metadata_filename = outpath + ".info"
    metadata = open(metadata_filename, "w")
    currentframe = None

    for name, number, index, layer in frames:
        if name != currentframe:
            currentframe = name
            metadata.write("%s frame %i offset = %i\n" % (name, number, index))

        atlas.blit(y + layer.bbox.y1, x + layer.bbox.x1, layer.as_pymaging())

        if x + psd.header.width > width:
            y += psd.header.height
            x = 0