0

Say I have a five .dat files that look something like this (but with different rows, I've just c&p'd them out of laziness):

0.34 5.19 5.07 11.0 1.4
0.34 5.19 5.07 11.0 1.4
0.34 5.19 5.07 11.0 1.4
0.34 5.19 5.07 11.0 1.4
0.34 5.19 5.07 11.0 1.4

What's the best way to turn these data files into an animation where each cell of the data file corresponds to a single pixel?

Dog
  • 15
  • 3
  • 1
    What exactly do you want to do ? What kind of animation. Does each number represent a color ? You have to give more information. – Aditya Singh Rathore Apr 24 '21 at 17:39
  • Let's say each number represents a shade of black. 20 represents black and everything below represents something lighter between black and white. Type of animation? A gif, mp4 or any other format that displays a sequence of frames. – Dog Apr 24 '21 at 17:43
  • You could define an Animation class which takes the data file name as a parameter to the class constructor. You could then implement an animate() method to perform the requires animation. – Nicholas Hunter Apr 24 '21 at 18:28

1 Answers1

1

Based on your comment here is an answer. It is this example mapped to your problem. Here is an explanation.

Here is what I did. To represent color of a pixel, Images use RGB colors. It is basically representing each Red, Green and Blue using a integer from 0(black) to 255(white). Since you wanted black and shades of black and white, or grayscale, all RGB values are same. As your number go from 0 to 20, we map this range 0 to 20 to 0 to 255. 0 meaning black and 20 meaning white. This is based on another stackoverflow question.

def map(input):
    input_start = 0
    input_end = 20

    output_start = 0
    output_end = 255

    output = output_start + ((output_end - output_start) // (input_end - input_start)) * (input - input_start)
    return output
  • Next, we create an image and color each square with the color we want. I am using random number but in your case, it will be the array. Read the above blog to understand.
def next_shape(im, num_squares, pixel_per_square,image_width ):
    pix = im.load()    #initialise plot location
    startx, starty = 0, 0    
    for i in range(num_squares):
        startx += pixel_per_square
        for j in range(num_squares):
            starty += pixel_per_square
            color = np.random.randint(0, 21)    
            colorRGB = map(color)
            for x in range(pixel_per_square):
                for y in range(pixel_per_square):
                    value = (colorRGB, colorRGB, colorRGB)
                    pix[(startx + x) % image_width, (starty + y) % image_width] = value 
    return list(im.getdata())

Rest see the blog on details of implementation.