Nick
Nick

Reputation: 675

Python color code values in grid and export as jpg

So i have this grid that i generate and i want each value in that grid to represent a color. This is the generating code:

for row in self.grid:
        for item in range(len(row)):
            row[item] = random.randint(-10, 20)

For example, I have this grid:

[1, -7, 20, 12, 6, 5, 11, -1, -3, 8]
[18, 10, 2, -3, -4, 5, 10, 12, 10, -6]
[11, 19, 8, 2, -8, 5, -4, -4, 16, 1]
[-7, -7, 11, 13, -6, -8, 0, 10, 14, 9]
[16, 16, 1, 8, -7, 19, 9, 20, 2, 2]
[-3, 2, 4, 16, 20, -4, -1, -10, 19, 4]
[9, -9, 11, 5, 7, -7, 5, 15, -1, -6]
[11, -5, -2, -9, 19, -7, 14, -3, -8, -8]
[1, 13, 19, 9, 13, 7, 9, 11, -3, 19]
[15, -3, -10, 13, 2, 6, -7, -8, -6, 15]

And i want each value (that is part of a range) to correspond to a certain color. For example the range from 1 to 5 to be green. After which to export it to a jpg image for which I assume the winner would be PIL but I am not really sure as how to proceed.

Upvotes: 1

Views: 862

Answers (2)

Tagc
Tagc

Reputation: 9076

Here's an implementation that I think does what you want. Note that the colours are altered when the image is saved in JPEG format.

import random

from PIL import Image

COLOUR_MAP = {
    range(-10, 0): (255, 0, 0),
    range(0, 10): (0, 255, 0),
    range(10, 21): (0, 0, 255)
}


def create_grid(n):
    return [[random.randint(-10, 20) for _ in range(n)] for _ in range(n)]


def map_to_colour(x):
    for r, c in COLOUR_MAP.items():
        if x in r:
            return c
    else:
        raise ValueError(x)


def map_values_to_colours(value_grid):
    return [[map_to_colour(x) for x in row] for row in value_grid]


def draw_image_from_colour_grid(colour_grid):
    im = Image.new(mode='RGB', size=(len(colour_grid), len(colour_grid[0])))
    im.putdata([x for row in colour_grid for x in row])
    im.show()
    im.save('out.jpg', 'JPEG')


if __name__ == '__main__':
    value_grid = create_grid(10)
    colour_grid = map_values_to_colours(value_grid)
    draw_image_from_colour_grid(colour_grid)

Output

Upvotes: 3

asongtoruin
asongtoruin

Reputation: 10359

You can use the Image.putdata() method in the Pillow module:

from PIL import Image

grid = [
    [1, -7, 20, 12, 6, 5, 11, -1, -3, 8],
    [18, 10, 2, -3, -4, 5, 10, 12, 10, -6],
    [11, 19, 8, 2, -8, 5, -4, -4, 16, 1],
    [-7, -7, 11, 13, -6, -8, 0, 10, 14, 9],
    [16, 16, 1, 8, -7, 19, 9, 20, 2, 2],
    [-3, 2, 4, 16, 20, -4, -1, -10, 19, 4],
    [9, -9, 11, 5, 7, -7, 5, 15, -1, -6],
    [11, -5, -2, -9, 19, -7, 14, -3, -8, -8],
    [1, 13, 19, 9, 13, 7, 9, 11, -3, 19],
    [15, -3, -10, 13, 2, 6, -7, -8, -6, 15],
    ]

# record the number of rows and columns in the data
rows = len(grid)
cols = len(grid[1])

# create a new image, in the correct size
im = Image.new('RGB', (rows, cols))

# convert to RGB tuples - for now we're just showing an image of different shades of red.
grid = [(abs(x)*20, 0, 0) for row in grid for x in row]

# using putdata we apply the values in the tuples from top left to bottom right
im.putdata(grid)
im.save('example.jpg')

Upvotes: 0

Related Questions