0

I'm trying to run a code that divides a video into frames while filtering it to greyscale (using threads) and I've got this error trying to run my code:

File "C:\Users\USER\PycharmProjects\ASCIIPICproject\venv\lib\site-packages\matplotlib\artist.py", line 1160, in _update_props raise AttributeError( AttributeError: Line2D.set() got an unexpected keyword argument 'cmap'

this is my code (of the function for the filtering thread):

def saveFramesFiltered():
currentFrame = 0
framemax = 215

while currentFrame < framemax:
    while not os.path.exists("./framesBefore/frame" + str(currentFrame) + '.jpg'):
        time.sleep(0.01)
    lock.acquire()
    image = pltim.imread("./framesBefore/frame" + str(currentFrame) + '.jpg')
    lock.release()
    r, g, b = image[:, :, 0], image[:, :, 1], image[:, :, 2]
    grayImage = 0.299 * r + 0.587 * g + 0.114 * b
    plt.plot(grayImage, cmap="gray")
    plt.axis("off")
    lock.acquire()
    plt.savefig("./framesAfter/grayImage" + str(currentFrame) + ".jpg", bbox_inches='tight', pad_inches=0)
    lock.release()
    time.sleep(0.01)
MR. potato35
  • 77
  • 1
  • 7

1 Answers1

0

Your error comes from plt.plot(grayImage, cmap="gray") , You can find this yourself usually by checking the line of the error. Plot plots a curve not an image so it cannot be associated to a colormap. Try plt.imshow()

Then i would avoid the use of a second while to check if you created the file, (you check if plt.savefig worked...) Maybe savefig returns an argument that you can check. If you don't need to open the image you can win some time by doing :

# a colormap and a normalization instance
cmap = plt.cm.jet
norm = plt.Normalize(vmin=data.min(), vmax=data.max())

# map the normalized data to colors
# image is now RGBA (512x512x4) 
image = cmap(norm(data))

# save the image
plt.imsave('test.png', image)

See Saving an imshow-like image while preserving resolution

If really you want to keep it put it at the end of the loop instead of the beginning, it's clearer, how does the loop even pass the first iteration ?

I am also not sure your lock.acquire method is needed while you save theimage ?

Adrien Mau
  • 181
  • 5