python-mss icon indicating copy to clipboard operation
python-mss copied to clipboard

Add export methods for PIL and popular array libraries to the ScreenShot object

Open jholveck opened this issue 9 months ago • 5 comments

Currently, this shares memory with the .raw bytearray in some cases. We should consider whether we want to change that to always copy, or just wait until we have the full shared buffer support.

This implementation always does the array manipulation on the CPU. For PyTorch or TensorFlow, the user is likely to want to do the array manipulation on the GPU for performance. This is very easy to implement; I just wanted to get this draft out for now.

Changes proposed in this PR

Fixes #478

  • [X] Tests added/updated
  • [X] Documentation updated
  • [ ] Changelog entry added
  • [X] ./check.sh passed

jholveck avatar Feb 24 '26 08:02 jholveck

Python 3.9 will be dropped in the next major release, so no need to take care of test failures.

I think we can do a release without this patch, right?

BoboTiG avatar Feb 24 '26 15:02 BoboTiG

Certainly!

jholveck avatar Feb 25 '26 02:02 jholveck

We can still wait to merge this, but the fix for Python 3.9 was simple, so I went ahead and committed it.

jholveck avatar Feb 26 '26 06:02 jholveck

You can revert the Python 3.9 fix commit ;)

BoboTiG avatar Apr 25 '26 13:04 BoboTiG

I've updated this a bit, but should probably review it before committing. It's been a while!

jholveck avatar Jun 02 '26 04:06 jholveck

This is on hold pending a resolution to #536, which will affect how we should document these methods and their sharing semantics.

jholveck avatar Jun 25 '26 00:06 jholveck

My last commit added a lot to the docs. There's a couple more things I want to add to the docs before closing this, even if they're somewhat out of scope for this PR.

jholveck avatar Jun 26 '26 05:06 jholveck

Great! The documentation is very very good since you both started to work on the project with @halldorfannar 🤟

Go ahead for out-of-scope commits, I'll merge when you're ready.

BoboTiG avatar Jun 26 '26 06:06 BoboTiG

Well, that test was weird. Glad you caught it.

All the stages seem to have completed in seconds. So I think this was a transient issue in GitHub's orchestration, not a problem with MSS. Orchestrating CI/CD on macOS is hard.

If it recurs, we can look more at it. But I'm not concerned yet.

jholveck avatar Jun 29 '26 05:06 jholveck

I think this is now feature-complete, and ready for @BoboTiG and @halldorfannar 's reviews. Sorry it's such a long PR!

jholveck avatar Jun 30 '26 06:06 jholveck

It seems that one of the macOS tests is stuck again.

Also, I previously said the stages had completed, but was mistaken: this is hanging during PyTest. If the CI/CD logs are complete (that is, not buffering several lines at a time), then it seems like it's now executing src/tests/test_issue_220.py::test_regression.

I'm not sure if this is a change in macOS, Python, or what. It could conceivably be related to my code, but I'm not sure how.

jholveck avatar Jun 30 '26 08:06 jholveck

This test stressing tkinter, and the OS being macOS, we are not yet ready for all potential suprprises we could hit. Lets see if it happens on a regular basis, for now if retrying manually on timout fixes it, that's good.

BoboTiG avatar Jun 30 '26 09:06 BoboTiG

The original issue, #220, was Linux-specific. You also tracked it down to an issue specific to Xlib error handling. Error handling is one big reason I wanted to migrate to XCB: it has a model that's much easier to work with when you have multiple libraries in a single program, let alone multiple threads.

I'll take a bit of a closer look at the issue and see if maybe it's making assumptions about things like event ordering that tkinter doesn't actually promise, and so has a race condition that just didn't happen to be a problem until recently. But if I don't find a way to fix the hang, I also wouldn't feel bad about limiting that test to just Linux.

Edit: if I do pursue changes related to this, it'll be in a separate PR. I just put the timeout in this PR so it didn't keep bugging us as we close it up.

jholveck avatar Jun 30 '26 10:06 jholveck

Looks good to me!

halldorfannar avatar Jun 30 '26 12:06 halldorfannar

Ok! I think there's always more to improve, but I feel like it's at a good point to merge.

jholveck avatar Jun 30 '26 19:06 jholveck