Create png Image from Coordinates

Basically this:

image[40, 10, :] = (0xFF, 0xFF, 0x0)

Is a shorthand (called slicing) for this:

image[40, 10, 0] = 0xFF # Red
image[40, 10, 1] = 0xFF # Green
image[40, 10, 2] = 0x0  # Blue

Typically that last index in the numpy array is 0 for red, 1 for green, 2 for blue for RGB images.

numpy uses row-major (C) layout by default (“the later index changes more rapidly”) so typically images use image[ycoord, xcoord], not image[xcoord, ycoord].

(If that bothers you, pillow hides all this in image.putpixel((x, y), (r,g,b)). But internally it’s the same.)


pandas.read_excel() (with openpyxl installed) is indeed the way to go for Excel files.

import pandas
dataframe = pandas.read_excel("example.xlsx")
xcoords = dataframe["Your X coordinates column name here"]
ycoords = dataframe["Your Y coordinates column name here"]

Basically:

  • numpy is the low level foundation for almost all image processing in Python.
  • imageio is the foundation for reading and writing image files.
  • Other libraries often also use numpy and imageio internally.
  • pandas is for Excel-like table functionality. (openpyxl is the Excel backend.)
  • matplotlib is for plotting. If you want to create typical charts, scatter plots, etc. as described above, there’s no need to do it pixel by pixel yourself. matplotlib is the way to go, as this example demonstrates.
1 Like