Matplotlib return color at a point

Viewed 141

I'm plotting some shapes using matplotlib.patches and need to determine the color at a particular point.

In the following simple example of a canvas that is half blue, and half white, is it possible to query the point (0.25,0.5) and get some RGB values that are different than if I queried the point (0.75,0.5)?

import numpy as np
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
import commands
import sys

patches = []
SquareList = []
StartingSquare = np.array([0,0,1,1]) 

SquareList.append(StartingSquare)

fig = plt.subplots()
polygon = plt.Polygon(np.reshape(np.array([0,0,.5,0,.5,1,0,1]),(4,2)))
plt.gca().add_patch(polygon)    

#plt.axis('off')
plt.savefig("test.png")

Thanks.

1 Answers

Use the Python Image Library, which can be installed easily by pip install pillow.

from PIL import Image
img = Image.open("test.png")
pix = img.load() 
print (pix[0.75,0.5])
Related