ponty / pyscreenshot

Python screenshot library, replacement for the Pillow ImageGrab module on Linux.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pyscreenshot.grab_to_file issues and can't run pyscreenshot command in IDLE

sunbearc22 opened this issue · comments

I submitted an answer on stackoverflow that showed how I used pyscreenshot to take screenshots of a tkinter.Canvas. I have shared the code here. I encountered a few problems and hope you can help me understand why they occurred and how to overcome them.

1. pyscreenshot.grab_to_file.
I wanted to use this function to directly save a screenshoot of the tkinter canvas into a file.
ImageGrab.grab_to_file("out_grabtofile.png", ImageGrab.grab(bbox=canvas))
Issues:

  1. I could not save a screen shot of the bbox used. A screenshot of the entire screen was returned.
  2. Saved image format always defaulted to .png. Other format like jpg gave errors.

2. After executing the script from IDLE. The GUI appears. When i click on the buttons that activates pyscreenshot's command, the IDLE GUI will hang.
Example:
After clicking 'SAVE' button, terminal will show a kill? pop up window saying "Your program is still running. Do you want to kill it?" After clicking yes, the IDLE terminal will close and the tkinter GUI hangs; I cannot close the GUI even though i click on the 'x' button of the window. Instead, I have to go to terminal and run the command ps -ef | grep python3. It will show something like:

sunbear+ 6797 1870 0 16:56 ? 00:00:00 /usr/bin/python3 -c import('idlelib.run').run.main(True) 43166

Thereafter, I have to manually kill the GUI on commandline by killing the above process.
If the 'Cancel' button on kill pop-up window was clicked, the IDLE terminal will remain but the GUI will still hand as previously described.

Appreciate if you can explain the causes of these issues and your advice on how to overcome them.

commented

Please read the documentation.
This library is only for grabbing and not for saving or converting images.
grab_to_file is a helper function, please don't use it.
You need Image.save http://pillow.readthedocs.io/en/4.0.x/reference/Image.html#PIL.Image.Image.save

Example:

import pyscreenshot as ImageGrab
im = ImageGrab.grab(bbox=(10, 10, 510, 510))
im.save('img.png')
  1. Noted. This library is only for grabbing image and not saving or converting images. Those I do using PIL/Pillow. Also not to use `grab_to_file.
  2. I have commented the code to show image and just let the snap button grab the canvas screen which is defined by your code. I have run my code using IDLE, and the GUI appears. I then click the SNAP button which runs your code (shown below). What happen next is that issue 2 mentioned in my earlier question appears. It does not go away. Do you know why? Issue 2 does not occur when I run my file with your command directly from terminal. Can you explain why your command have issue running from IDLE and not a terminal?
    def _snapCanvas(self):
        print('\n def _snapCanvas(self):')
        canvas = self._canvas() # Get Window Coordinates of Canvas
        self.grabcanvas = ImageGrab.grab(bbox=canvas)
        #self.grabcanvas.show()
commented

IDLE has problem with multiprocessing:
http://stackoverflow.com/questions/35293178/can-multiprocessing-process-class-be-run-from-idle/35297841#35297841
I don't know how to fix it.

You can however turn off multiprocessing in pyscreenshot with setting childprocess=False:

import pyscreenshot as ImageGrab
im = ImageGrab.grab(bbox=(10, 10, 510, 510), childprocess=False)
im.save('img.png')

I tested this, and it worked.

commented

I set "childprocess" default value to False, if IDLE is detected, so this should work now:

import pyscreenshot as ImageGrab
im = ImageGrab.grab(bbox=(10, 10, 510, 510))
im.save('img.png')