By Eugene Liscio via Discussions on Python.org at 21Jun2022 02:10:
Ok, so i was experimenting with this snippet of code (after installing
Pycharm and getting modules installed). So, I can likely do a brute
force method where I just put all the coordinates arranged in Excel and
then copy paste into the code.
import numpy, imageio
X,Y = 1920,1080
image = numpy.zeros((Y, X, 3), dtype=numpy.uint8)
image[40, 10, :] = (0xFF, 0xFF, 0x0)
image[50, 10, :] = (0xFF, 0xFF, 0x0)
image[40, 20, :] = (0xFF, 0xFF, 0x0)
imageio.imwrite(‘output.png’, image)
So, sidestepping Pillow and going to NumPy? Ok.
The zeros function:
https://numpy.org/doc/stable/reference/generated/numpy.zeros.html
However, I am guessing I need to call the csv module to read an Excel file? I am not going to pretend I undersatand exactly what is going on here, but I would want to read the Excel file coordinates into the
The openpyxl module will read and write Excel:
https://openpyxl.readthedocs.io/en/latest/
The Pandas package has a read_excel() function like its read_csv()
function: each returns a DateFrame.
image[xcoord, ycoord, :] = (0xFF, 0xFF, 0x0)
What does the “:” do in the above line?
Are you familiar with Python slicing in normal Python lists? The
[a:b:c] syntax is a slice, indicating the portion of the array from
a through to b (inclusively and exclusively respectively) in steps
if c (default 1 i.e. all the elements - you could use 2 to get
every other element, etc).
NumPy array are very flexible things and use the Python slice syntax
to select various views of the NumPy array. In your example:
xcoord,ycoord, is a 2-tuple for the a part, and the b part is
empty.
In a regular Python sequence the empty b would mean the end of the
array, thus the slice gets all the elements from a onwards. In a NumPy
array and given your example, I’d take it to mean the remaining
domensions of the array shape, you you’d be left with the length-3 thing
at (xcoord,ycoord), which I expect is “a pixel”.
There’s a tutorial on NumPy array slicing here:
https://numpy.org/doc/stable/user/absolute_beginners.html#indexing-and-slicing
which should explain what is going on above. I would think (guessing)
that what you have is a 2-tuple of (xcoord,ycoord) with the empty part
meaning “all the array at that 2-tuple”, so your 3-value pixel because
your array has the shape (Y,X,3) from when you defined it.
I do notice that your shape is Y and then X, but your example above
seems to be xcoord then ycoord. You might have something swapped.
You could test this by setting xcoord higher than 1080 (i.e. out of
range for the y-ordinates) and seeing what numpy does.
Cheers,
Cameron Simpson cs@cskk.id.au