How can i reshape an array from (280, 280, 3) to (28, 28, 3)

583 Views Asked by At

Hi i tried to write a code, where i write a number on the screen with pygame and then a neural Network predicts the number i wrote. My problem is that i trained my neural network with image arrays in a (28, 28, 3). So i tried to reshape my (280, 280, 3) array. but when i do so my array is None. I use Python 3.7

string_image = pygame.image.tostring(screen, 'RGB')
temp_surf = pygame.image.fromstring(string_image, (280, 280), 'RGB')
array = pygame.surfarray.array3d(temp_surf)
array = array.resize((28, 28, 3))

Can anyone help?

2

There are 2 best solutions below

0
On

I am not familiar with pygame but it looks like pygame.surfarray.array3d returns a numpy-array.

How to iterate through a pygame 3d surfarray and change the individual color of the pixels if they're less than a specific value?

To keep the same number of data points and just change the shape you can use numpy.reshape.

import numpy as np
c = 28*28*3 # just to start off with right amount of data
a = np.arange(c) # this just creates the initial data you want to format
b = a.reshape((28,28,3)) # this actually reshapes the data
d = b.shape #this just stores the new shape in variable d
print(d) #shows you the new shape

To interpolate the data to a new size then there are a number of ways like CV or continuing a numpy example.

ary = np.resize(b,(18,18,1))
e = ary.shape
print(e)

Hope that helps.

0
On

If you just want to scale a pygame.Surface, then I recommend to use pygame.transform.scale() or pygame.transform.smoothscale().

For instance:

temp_surf = pygame.image.fromstring(string_image, (280, 280), 'RGB')
scaled_surf = pygame.transform.smoothscale(temp_surf, (28, 28))
array = pygame.surfarray.array3d(scaled_surf)