That's very inefficient, on top of questionable tech stack
Why would you store the screenshot to disk, just grab what ever the compositor gives you and render it on a texture, skipping the whole disk write usage
https://blogs.windows.com/windowsdeveloper/2019/09/16/new-wa...
It's similar for both macOS and X11 (haven't looked at wayland yet)