gpt4 book ai didi

python - 将Numpy图像数组转换为1px高版本

转载 作者:行者123 更新时间:2023-12-01 01:03:20 25 4
gpt4 key购买 nike

上下文:

Google 有一个 MNIST 数据的 spritesheet。他们拍摄了一张 (28, 28, 1) 图像,并将其转换为 (1, 784, 1) 行数据(784 是 28*28)。然后他们对所有 65k 图像执行了此操作。所以它适合一个漂亮的 Sprite 表,如下所示: https://storage.googleapis.com/learnjs-data/model-builder/mnist_images.png

我正在寻找制作自己的数据 Sprite 表。

我正在使用 numpy/PIL,所以当我将图像转换为 numpy 时,具有 3 个 channel 。

问题:我如何将其展平,然后连接该平面图像,使其变成宽度 = 784、高度 = 图像数量的图像,全部为 RGB。

此处的伪代码:

# Load image image
image_data = image.load_img("/path/to.png", target_size=(28, 28))

# Create shape (28, 28, 3)
np_train = np.array(image_data)

# Goal change (28, 28, 3) into (1, 784, 3)
# then add that to some final_image, building to final_image (num_images, 784, 3)

# then
img = Image.fromarray(final_image)
img=.show # spritesheet of image data for consumption

编辑:结果:https://github.com/GantMan/rps_tfjs_demo/blob/master/spritemaker/makerps.py

最佳答案

你并不完全需要 numpy 来做到这一点,虽然我不知道是否有必要使用它,但有一种方法可以用简单的 Python 来做到这一点:

from PIL import Image

src_image = Image.open('test_image.png') # Image of size (28,28)

pixels = list(src_image.getdata()) # Get all pixel in 1D array.

dst_image = Image.new('RGB', (1,src_image.size[0] * src_image.size[1])) # Create new image with new size.

dst_image.putdata(pixels) # Place pixels in the new image.

dst_image.save('result.png') # Save the new image.

示例:

源图片:

enter image description here

目标图像:

enter image description here

关于python - 将Numpy图像数组转换为1px高版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55597628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com