0

Given a simple example of code that draws lines between given coordinates with the use of ax.plot()

import matplotlib.pyplot as plt
x_values = [0, 2, 4, 5]
y_values = [3, 1, 5, 2]
fig, ax = plt.subplots(1, 1)
ax.set_xlim(0,5)
ax.set_ylim(0,5)
ax.plot(x_values, y_values)
plt.show()

the pixel size, an interpolation needs to be made. But what is the best and easiest way?

In order to do image processing, I need the raw pixel data (b/w is sufficient) of that image, in a given pixel size, let it be [MxN], given just the line endpoint coordinates The data I am looking at would be an array with the dimensions of [MxN] just containing out of 0 and 1.

Of cause there is interpolation needed, depending on the pixel size. In my specific use case, I have very much and complex lines that are drawn with ax.plot(), so I wont be able to interpolate each line manually.

As I saw, there is ax.imshow(), but this will not return a matrix/vector. Does anybody have an idea?

moosehead42
  • 411
  • 4
  • 18

1 Answers1

1

You can save the image as a jpg and then open it as an numpy array. You can check the shape to make sure it is to your liking.

import numpy as np 
from PIL import Image

i = Image.open('name of your jpg.jpg')
array = np.asarray(i)
print(array.shape)

If you want to change the shape:

array.flatten().reshape(#M dim, #N dim)
nav610
  • 781
  • 3
  • 8
  • 1
    Thanks! In order to import simple grayscale values, the command would be `i=Image.open('myjpg.jpy').convert('L')`, otherwise it is given in RBGA. – moosehead42 Jul 13 '20 at 08:35