Skip to content

Instantly share code, notes, and snippets.

@Uberi
Created November 13, 2014 02:32
Show Gist options
  • Save Uberi/4885a318e7ef2afa7f22 to your computer and use it in GitHub Desktop.
Save Uberi/4885a318e7ef2afa7f22 to your computer and use it in GitHub Desktop.
Scatter plot the RGB values of an image in 3D! Requires Pillow and matplotlib, running on Python 3.
image_path = "HOPE.png"
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot
from PIL import Image
fig = pyplot.figure()
axis = fig.add_subplot(1, 1, 1, projection="3d") # 3D plot with scalar values in each axis
im = Image.open(image_path)
r, g, b = list(im.getdata(0)), list(im.getdata(1)), list(im.getdata(2))
axis.scatter(r, g, b, c="#ff0000", marker="o")
axis.set_xlabel("Red")
axis.set_ylabel("Green")
axis.set_zlabel("Blue")
pyplot.show()
@FatemehMirshahi
Copy link

Thank you, it was really helpful for me!

@gersmit
Copy link

gersmit commented Nov 22, 2021

@Uberi small question from me, Ger from the Netherlands.
Is it also possible to get rgb value from plot when i have this script:
crs = new_scn[chan].attrs[‘area’].to_cartopy_crs()
ax = plt.axes(projection=crs)

ax.coastlines()
ax.gridlines()
ax.set_global()
chan_data = new_scn[chan]-273.15 # plot data
im = plt.imshow(chan_data, transform=crs, extent=crs.bounds, origin=‘upper’, cmap=“coolwarm”)
cbar = plt.colorbar()
cbar.set_label(“Kelvin”)

lon = 4.666
lat = 52.496
area = new_scn[chan].attrs[‘area’]
print('lon-lat ',lon, lat)
col, row = area.get_xy_from_lonlat(lon, lat)
print('col-row ',col, row)

global temp
temp = int(new_scn[chan].values[row, col]-273.15) #<-- Here i read the data of the pixel temperature
print('temp-col-row ',temp, col, row)
How can i also get the pixel RGB color value

Thanks in advance for answer
Ger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment