Python Forum
Screenshot problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Forum & Off Topic (https://python-forum.io/forum-23.html)
+--- Forum: Bar (https://python-forum.io/forum-27.html)
+--- Thread: Screenshot problem (/thread-41713.html)



Screenshot problem - DPaul - Mar-05-2024

Hi,
When a user wants to save a document , that is shown in the tKinter canvas area of the gui,
he hits a button, and a screenshot is taken to the clipboard , and saved. (Using the box(...) method to copy only the canvas part.)
I'm using pyscreenshot. Works like a charm.
But, when the user does this a second time in a row, the app goes haywire. (eg. it starts a second instance of the app)

I'm testing what could cause this behaviour, and my question is:
Can I "flush" the clipboard of a PC, to empty it from the screenshot that was taken an saved a moment before.
I have not found anything, but maybe "flush" is the wrong term.
And maybe i have to look outside pyscreenshot.
ANy ideas ?
thx,
Paul


RE: Screenshot problem - Gribouillis - Mar-05-2024

Why not use the canvas postscript method to save the canvas as postscript then convert the postscript image to another format?
Output:
help(Canvas.postscript) Print the contents of the canvas to a postscript file. Valid options: colormap, colormode, file, fontmap, height, pageanchor, pageheight, pagewidth, pagex, pagey, rotate, width, x, y.
Also the pyscreenshot module says
Quote:The pyscreenshot module is obsolete in most cases. It was created because PIL ImageGrab module worked on Windows only, but now Linux and macOS are also supported.

An old trick to convert eps to postscript:

cv.postscript(file="circles.eps")
    from PIL import Image
    img = Image.open("circles.eps")
    img.save("circles.png", "png")
See also pillow image formats


RE: Screenshot problem - DPaul - Mar-06-2024

(Mar-05-2024, 08:11 AM)Gribouillis Wrote: Why not use the canvas postscript method
Yes, that is what I did before I turned to screenshots. It also works, but,
I found that this method caused a significant loss of quality in the image.
And since e.g. payer cards have already very small print, screenshots look much better when saved.
Paul

Edit: it would seem that ctypes is a possibility.


RE: Screenshot problem - DeaD_EyE - Mar-06-2024

Have you tried ImageGrab? It should work now on all Operating Systems. Before only Windows was supported for ImageGrab. It returns a Image object you can work with.

from PIL.ImageGrab import grab


def screenshot():
    grab().show()



screenshot()



RE: Screenshot problem - DPaul - Mar-06-2024

(Mar-06-2024, 01:32 PM)DeaD_EyE Wrote: Have you tried ImageGrab? It should work
No, I did not (with PIL).
I will try asap.
thx,
Paul


RE: Screenshot problem - DPaul - Mar-11-2024

(Mar-06-2024, 01:32 PM)DeaD_EyE Wrote: Have you tried ImageGrab? It should work now on all Operating System
Tried it today, yes it works.
My current method allows a box to be defined, so I grab only a part of the scren.
bbox = (canvasAreaStart, 10, screenWidth, verPixels)
        # Capture a specific region
        canvasArea = ImageGrab.grab(bbox=bbox)
Maybe that is possible via PIL too ?
Still am testing why I causes the app to start another instance.
thx,
paul


RE: Screenshot problem - DeaD_EyE - Mar-11-2024

There is a bbox argument.

img = PIL.ImageGrab.grab(bbox=(0, 0, 90, 60))
I use X11. It grabs all physical screens, even if all_screens=False.
Maybe the option works on Wayland. If you have only one screen, then you don't care.


RE: Screenshot problem - DPaul - Mar-12-2024

(Mar-11-2024, 08:32 PM)DeaD_EyE Wrote: have only one screen, then you don't care.
Thanks, good show, ... and no I don't care. Smile
Paul

Edit: I read that pyscreenshot should be considered obsolete.
PIL Imagegrab seems to do what it's supposed to.