ponty / pyscreenshot

Python screenshot library, replacement for the Pillow ImageGrab module on Linux.
BSD 2-Clause "Simplified" License
499 stars 89 forks source link

Can't use pyscreenshot on Mac inside Django #35

Closed sliwinski-milosz closed 7 years ago

sliwinski-milosz commented 8 years ago

It freezes while I try to use grab or grab_to_file function.

Workaround is to use MacQuartzWrapper directly.

ponty commented 8 years ago

Please turn on logging for more info:

logging.basicConfig(level=logging.DEBUG)
sliwinski-milosz commented 7 years ago

Hi, Sorry for late response. Here are logs when for DEBUG level:

DEBUG:pyscreenshot:running "mac_quartz" in child process
DEBUG:pyscreenshot.loader:forcing:mac_quartz
DEBUG:pyscreenshot.plugins.mac_quartz:MacQuartzWrapper initialization...

I added some more debug log lines to make it clear. So above output if for following snippet: mac_quartz.py

class MacQuartzWrapper(object):
    name = 'mac_quartz'
    childprocess = False

    def __init__(self):
        log.debug('MacQuartzWrapper initialization...')
        import Quartz
        log.debug('Quartz')
        import LaunchServices
        log.debug('LaunchServices')
        from Cocoa import NSURL
        log.debug('NSURL')
        import Quartz.CoreGraphics as CG
        log.debug('CG')
        self.Quartz = Quartz
        self.LaunchServices = LaunchServices
        self.NSURL = NSURL
        self.CG = CG
        log.debug('Initialized')

So it seems that there is some problem with importing Quartz inside class. This explains also why it works when I import MacQuartzWrapper directly:

from pyscreenshot.plugins.mac_quartz import MacQuartzWrapper
quartz = MacQuartzWrapper()
quartz.grab_to_file(output_path)

I don't know how loader in pyscreenshot works but seems that there could be problems in "mac_quartz" backend. Importing it at the time when I want to make a screenshot, may cause also performance issues.

I have seen that for other backends we don't have to import modules inside class - which means that for other backends problem does not exist :).

ponty commented 7 years ago

Please don't use the grab_to_file function. Check #38. I don't have a Mac. Mac backends were implemented by contributors. Check the git history. So I can only accept pull requests for Mac.

All backends are imported inside wrapper classes because we don't want to import all the backends only one. There is only one import so this should not make a performance problem.

Are you using IDLE? (#38) Try to set childprocess=False:

import pyscreenshot as ImageGrab
im = ImageGrab.grab(bbox=(10, 10, 510, 510), childprocess=False)
im.save('img.png')
sliwinski-milosz commented 7 years ago

I just retested it using:

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

And it works fine.

If I set childprocess=True then it freezes in the same place as before.

So seems that switching childprocess to False fixes the problem.