Example #1
0
    def test_compression_RGBA(self):
        import pyTGA

        data = [[(0, 0, 0, 255), (0, 0, 0, 255), (0, 0, 0, 255),
                 (0, 0, 0, 255), (0, 0, 0, 255), (0, 0, 0, 255),
                 (0, 0, 0, 255)],
                [(0, 0, 0, 255), (255, 0, 0, 255), (0, 0, 0, 255),
                 (0, 0, 0, 255), (120, 0, 0, 255), (200, 0, 0, 255),
                 (250, 0, 0, 255)],
                [(0, 0, 0, 255), (0, 0, 0, 255), (255, 0, 0, 255),
                 (0, 0, 0, 255), (0, 0, 0, 255), (0, 0, 0, 255),
                 (100, 0, 0, 255)],
                [(255, 0, 0, 255), (255, 0, 0, 255), (255, 0, 0, 255),
                 (0, 0, 0, 255), (255, 0, 0, 255), (255, 0, 0, 255),
                 (255, 0, 0, 255)]]

        image = pyTGA.Image(data=data)
        image.save("test_compression_rgba", compress=True)

        image2 = pyTGA.Image()
        image2.load("test_compression_rgba.tga")

        self.assertEqual(image.get_pixels(), image2.get_pixels())

        os.remove("test_compression_rgba.tga")
Example #2
0
    def test_modify_pixel(self):
        import pyTGA

        data_rgba = [[(0, 0, 0, 0), (255, 0, 0, 150), (0, 0, 0, 0),
                      (0, 0, 0, 0)],
                     [(0, 0, 0, 0), (0, 0, 0, 0), (255, 0, 0, 150),
                      (0, 0, 0, 0)],
                     [(255, 0, 0, 150), (255, 0, 0, 150), (255, 0, 0, 150),
                      (0, 0, 0, 0)]]

        image = pyTGA.Image(data=data_rgba)
        image.save("test_mod_rgba")

        image2 = pyTGA.Image(data=data_rgba)
        image2.set_pixel(0, 3, (0, 255, 0, 55))
        image2.save("test_mod_rgba_2")

        image2 = pyTGA.Image()
        image2.load("test_mod_rgba_2.tga")

        self.assertNotEqual(image.get_pixels(), image2.get_pixels())
        self.assertEqual(image.get_pixel(0, 3), (0, 0, 0, 0))
        self.assertEqual(image2.get_pixel(0, 3), (0, 255, 0, 55))

        os.remove("test_mod_rgba.tga")
        os.remove("test_mod_rgba_2.tga")
Example #3
0
def main():
    data_bw = [[0, 255, 0, 0], [0, 0, 255, 0], [255, 255, 255, 0]]

    data_rgb = [[(0, 0, 0), (255, 0, 0), (0, 0, 0), (0, 0, 0)],
                [(0, 0, 0), (0, 0, 0), (255, 0, 0), (0, 0, 0)],
                [(255, 0, 0), (255, 0, 0), (255, 0, 0), (0, 0, 0)]]

    data_rgba = [[(0, 0, 0, 0), (255, 0, 0, 150), (0, 0, 0, 0), (0, 0, 0, 0)],
                 [(0, 0, 0, 0), (0, 0, 0, 0), (255, 0, 0, 150), (0, 0, 0, 0)],
                 [(255, 0, 0, 150), (255, 0, 0, 150), (255, 0, 0, 150),
                  (0, 0, 0, 0)]]

    ##
    # Create from grayscale data
    image = pyTGA.Image(data=data_bw)
    # Save as TGA
    image.save("image_black_and_white")

    ##
    # Create from RGB data
    image = pyTGA.Image(data=data_rgb)
    image.save("image_rgb")

    ##
    # Create from RGBA data
    image = pyTGA.Image(data=data_rgba)
    image.save("image_rgba")

    ##
    # Save with RLE compression
    image = pyTGA.Image(data=data_rgba)
    image.save("image_rgba_compressed", compress=True)

    ##
    # Save in original format
    image = pyTGA.Image(data=data_rgba)
    image.save("image_rgba_original", original_format=True)

    ##
    # Save with 16 bit depth
    # You can start also from RGB, but you will lose data
    image = pyTGA.Image(data=data_rgb)
    image.save("test_16", force_16_bit=True)

    data_rgb_16 = [[(0, 0, 0), (31, 0, 0), (0, 0, 0), (0, 0, 0)],
                   [(0, 0, 0), (0, 0, 0), (31, 0, 0), (0, 0, 0)],
                   [(31, 0, 0), (31, 0, 0), (31, 0, 0), (0, 0, 0)]]

    image = pyTGA.Image(data=data_rgb_16)
    image.save("image_16_bit", force_16_bit=True)

    ##
    # Load and modify an image
    image = pyTGA.Image()
    image.load("image_black_and_white.tga").set_pixel(0, 3, 175)
    image.save("image_black_and_white_mod.tga")

    # Get some data
    print(image.get_pixel(0, 3))
    print(image.get_pixels())
Example #4
0
    def test_black_and_white_image(self):
        import pyTGA

        data_bw = [[0, 255, 0, 0], [0, 0, 255, 0], [255, 255, 255, 0]]

        image = pyTGA.Image(data=data_bw)
        image.save("test_bw")

        image2 = pyTGA.Image()
        image2.load("test_bw.tga")

        self.assertEqual(image.get_pixels(), image2.get_pixels())

        os.remove("test_bw.tga")
Example #5
0
    def test_compression_bw(self):
        import pyTGA

        data = [[0, 0, 0, 0, 0, 0, 0], [0, 255, 0, 0, 120, 200, 250],
                [0, 0, 255, 0, 0, 0, 100], [255, 255, 255, 0, 255, 255, 255]]

        image = pyTGA.Image(data=data)
        image.save("test_compression_bw", compress=True)

        image2 = pyTGA.Image()
        image2.load("test_compression_bw.tga")

        self.assertEqual(image.get_pixels(), image2.get_pixels())

        os.remove("test_compression_bw.tga")
Example #6
0
    def test_big_RLE_bw(self):
        import pyTGA

        data = [[0 for elm in range(500)], [0 for elm in range(500)],
                [0 for elm in range(500)], [0 for elm in range(500)],
                [0 for elm in range(500)]]

        image = pyTGA.Image(data=data)
        image.save("test_big_RLE_bw", compress=True)

        image2 = pyTGA.Image()
        image2.load("test_big_RLE_bw.tga")

        self.assertEqual(image.get_pixels(), image2.get_pixels())

        os.remove("test_big_RLE_bw.tga")
Example #7
0
    def test_RGB_image(self):
        import pyTGA

        data_rgb = [[(0, 0, 0), (255, 0, 0), (0, 0, 0), (0, 0, 0)],
                    [(0, 0, 0), (0, 0, 0), (255, 0, 0), (0, 0, 0)],
                    [(255, 0, 0), (255, 0, 0), (255, 0, 0), (0, 0, 0)]]

        image = pyTGA.Image(data=data_rgb)
        image.save("test_rgb")

        image2 = pyTGA.Image()
        image2.load("test_rgb.tga")

        self.assertEqual(image.get_pixels(), image2.get_pixels())

        os.remove("test_rgb.tga")
Example #8
0
def main():
    TGA_image = [[(0, 0, 0, 0) for i in range(100)] for i in range(100)]
    line(13, 20, 80, 40, TGA_image, TGA_white)
    # TGA_image[41][52] = TGA_red
    print(TGA_image)
    image = pyTGA.Image(data=TGA_image)
    image.save("output.tga")
Example #9
0
    def test_big_pack_rgba(self):
        import pyTGA

        data = [[(elm % 255, 0, 0, 255) for elm in range(500)],
                [(elm % 255, 0, 0, 255) for elm in reversed(range(500))],
                [(elm % 255, 0, 0, 255) for elm in range(500)],
                [(elm % 255, 0, 0, 255) for elm in reversed(range(500))],
                [(elm % 255, 0, 0, 255) for elm in range(500)]]

        image = pyTGA.Image(data=data)
        image.save("test_big_pack_rgba", compress=True)

        image2 = pyTGA.Image()
        image2.load("test_big_pack_rgba.tga")

        self.assertEqual(image.get_pixels(), image2.get_pixels())

        os.remove("test_big_pack_rgba.tga")
Example #10
0
    def test_big_RLE_16(self):
        import pyTGA

        data = [[(0, 0, 0) for elm in range(500)],
                [(0, 0, 0) for elm in range(500)],
                [(0, 0, 0) for elm in range(500)],
                [(0, 0, 0) for elm in range(500)],
                [(0, 0, 0) for elm in range(500)]]

        image = pyTGA.Image(data=data)
        image.save("test_big_RLE_16", compress=True, force_16_bit=True)

        image2 = pyTGA.Image()
        image2.load("test_big_RLE_16.tga")

        self.assertEqual(image.get_pixels(), image2.get_pixels())

        os.remove("test_big_RLE_16.tga")
Example #11
0
    def test_original_format(self):
        import pyTGA

        data_rgba = [[(0, 0, 0, 0), (255, 0, 0, 150), (0, 0, 0, 0),
                      (0, 0, 0, 0)],
                     [(0, 0, 0, 0), (0, 0, 0, 0), (255, 0, 0, 150),
                      (0, 0, 0, 0)],
                     [(255, 0, 0, 150), (255, 0, 0, 150), (255, 0, 0, 150),
                      (0, 0, 0, 0)]]

        image = pyTGA.Image(data=data_rgba)
        image.save("test_original_rgba", original_format=True)

        image2 = pyTGA.Image()
        image2.load("test_original_rgba.tga")

        self.assertTrue(image2.is_original_format())

        os.remove("test_original_rgba.tga")
Example #12
0
    def test_first_pixel_destination(self):
        import pyTGA

        data = [[0, 255, 0, 0], [0, 0, 255, 0], [255, 255, 255, 0]]

        with self.assertRaises(pyTGA.ImageError) as img_e:
            pyTGA.Image(data=data).set_first_pixel_destination("blabla")

        the_exception = img_e.exception
        self.assertEqual(the_exception.errno, -10)
Example #13
0
    def test_16_bits(self):
        import pyTGA

        data_rgb = [[(0, 0, 0), (255, 0, 0), (0, 0, 0), (0, 0, 0)],
                    [(0, 0, 0), (0, 0, 0), (255, 0, 0), (0, 0, 0)],
                    [(255, 0, 0), (255, 0, 0), (255, 0, 0), (0, 0, 0)]]

        data_rgb_16 = [[(0, 0, 0), (31, 0, 0), (0, 0, 0), (0, 0, 0)],
                       [(0, 0, 0), (0, 0, 0), (31, 0, 0), (0, 0, 0)],
                       [(31, 0, 0), (31, 0, 0), (31, 0, 0), (0, 0, 0)]]

        image = pyTGA.Image(data=data_rgb)
        image.save("test_16", force_16_bit=True)

        image2 = pyTGA.Image()
        image2.load("test_16.tga")

        self.assertEqual(image2.get_pixels(), data_rgb_16)

        os.remove("test_16.tga")
Example #14
0
    def test_compression_16(self):
        import pyTGA

        data = [[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                 (0, 0, 0), (0, 0, 0)],
                [(0, 0, 0), (31, 0, 0), (0, 0, 0), (0, 0, 0), (31, 0, 0),
                 (31, 0, 0), (31, 0, 0)],
                [(0, 0, 0), (0, 0, 0), (31, 0, 0), (0, 0, 0), (0, 0, 0),
                 (0, 0, 0), (20, 0, 0)],
                [(31, 0, 0), (31, 0, 0), (31, 0, 0), (0, 0, 0), (10, 0, 0),
                 (10, 0, 0), (10, 0, 0)]]

        image = pyTGA.Image(data=data)
        image.save("test_compression_16", compress=True, force_16_bit=True)

        image2 = pyTGA.Image()
        image2.load("test_compression_16.tga")

        self.assertEqual(image.get_pixels(), image2.get_pixels())

        os.remove("test_compression_16.tga")
Example #15
0
    def test_data_exceptions(self):
        import pyTGA

        data_0 = [
            [0.0],
        ]

        data_1 = [[0], [0], [0, 1, 2]]

        data_2 = [
            [()],
        ]

        data_3 = [
            [(1, 2, 3, 4, 5)],
        ]

        with self.assertRaises(pyTGA.ImageError) as img_e:
            pyTGA.Image(data=data_0)

        the_exception = img_e.exception
        self.assertEqual(the_exception.errno, -23)

        with self.assertRaises(pyTGA.ImageError) as img_e:
            pyTGA.Image(data=data_1)

        the_exception = img_e.exception
        self.assertEqual(the_exception.errno, -21)

        with self.assertRaises(pyTGA.ImageError) as img_e:
            pyTGA.Image(data=data_2)

        the_exception = img_e.exception
        self.assertEqual(the_exception.errno, -22)

        with self.assertRaises(pyTGA.ImageError) as img_e:
            pyTGA.Image(data=data_3)

        the_exception = img_e.exception
        self.assertEqual(the_exception.errno, -22)
Example #16
0
import pyTGA
'''
For information about the library, consult :
https://github.com/MircoT/pyTGA 
'''

# Load image
# The image MUST ONLY BE BLACK AND WHITE,
# where white is the background, and black is the actual "object" to be mapped to pixels
image = pyTGA.Image()
image.load("test.tga")

# List of pixels to be put into the text file
pixels_to_render = []

# Get information about the image
pixels = image.get_pixels()

i = 0
j = 0
for row in pixels:
    i = 0
    for col in row:
        # If it's not white, put into the list
        if (col[0] != 255) and (col[1] != 255) and (col[2] != 255):
            pixels_to_render.append((i, j))
        i += 1
    j += 1

# "Dump" list to file
with open("test.txt", "w") as f: