0

The file has the following structure:

h

w

P1,1_r P1,1_g P1,1_b

P1,2_r P1,2_g P1,2_b

...

P1,w_r P1,w_g P1,w_b

P2,1_r P2,1_g P2,1_b

...

Ph,w_r Ph,w_g Ph,w_b

The first line contains one integer, h, which is the height of the image. The second line contains one integer, w, which is the width of the image. The next h x w lines contain the pixel value as a list of three values corresponding to red, green, and blue color of the pixel. The list of pixels are arranged in the top-to-bottom then left-to-right order.

How can convert this to a np.array using np.genfromtxt?

FinleyGibson
  • 911
  • 5
  • 18
  • This is not the way to use stack overflow. Your question should ask about a single, specific thing you want to know or are struggling with. First break your problem down into steps, make an attempt at doing the first step, until you find something that you cant do or an error is raised, then see if you can find the solution by searching google/stack overflow to see if someone else has already asked it. If you cant find it then post a specific question here, showing what you have attempted so far. – FinleyGibson Oct 26 '20 at 19:29
  • It seems the first step is to load data from a file in python. But what kind of file is it? a .txt for example? If so, then search for how to read text files into python, [here](https://www.w3schools.com/python/python_file_open.asp), for example. Make an attempt, and if you cant get it to work, and cant find an answer to your question on stack overflow already, then post a question showing your code and any error messages. – FinleyGibson Oct 26 '20 at 19:35
  • so I change the question : How to get only first line and second line from text file and convert them to int – johnywalker Oct 27 '20 at 17:28

1 Answers1

0

If you want to read in the data from a .txt file as a numpy array, you need to first read the file data in, then use np.genfromtext on the resulting object:

file = open("filename.txt")

#separate the first two lines containing h and w
hw = file.readlines(2)

# get h and w, as integers from hw by indexing them by line numbers
h = int(hw[0])
w = int(hw[1])

This leaves the rest of file containing the np.array with the image data in. Use np.genfromtxt on this:

figure = np.genfromtxt(file)
FinleyGibson
  • 911
  • 5
  • 18