尝试这样的事情:
from PIL import Image
import numpy as np
def image_to_txt(image_path, output_file):
# Open the image
image = Image.open(image_path)
# Convert the image to grayscale
image = image.convert("L")
# Convert the image to a NumPy array
image_array = np.array(image)
# Write the NumPy array to a text file
with open(output_file, "w") as file:
np.savetxt(file, image_array, fmt='%d')
# Replace 'input_image.png' with your image file and 'output.txt' with the desired output file name
image_to_txt('input_image.png', 'output.txt')