我正在尝试将.jpg图像转换为.txt,但是当我运行下面的代码时,出现错误“ ValueError:预期的1D或2D数组,而是3D数组”。我究竟做错了什么?
from PIL import Image
import numpy as np
im = Image.open('Moon.jpg')
pixels = list(im.getdata())
width, height = im.size
pixels = [pixels[i * width:(i + 1) * width] for i in range(height)]
np.savetxt("Moon_data.txt", pixels, fmt='%d', delimiter=" ")