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

这一生的挚爱 提交于 2021-02-02 03:41:21

问题


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?


回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/62391656/how-can-i-reshape-an-array-from-280-280-3-to-28-28-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!