2

I'm trying to convert a .jpg image to .txt but when I run the code below I get the error "ValueError: Expected 1D or 2D array, got 3D array instead". What am I doing wrong?

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=" ")
NotEinstein
  • 59
  • 1
  • 6

1 Answers1

1

You could convert your image into grayscale, which is a 1D array. This can be saved into a text file. This answer provides to ways of converting an image to grayscale. One using Pillow and the other doing the calculation on your own.

FreshD
  • 2,914
  • 2
  • 23
  • 34