What value are you encountering for list 'D' when you get the error? As you can see from the code, not really production-grade coding and certainly not heavily tested, proof-of-concept more than anything else.
I tried running it with jupyterlab. I had issues with executing imagemagick commands which is why the temp.jpg file didn't get created causing the error (list D is empty).
ValueError: invalid literal for int() with base 10: ''
Changed it to float before int casting but still the same error.