ponty / pyscreenshot

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

PngImageFile.tobytes() does not work when setting SDL_VIDEO_WINDOW_POS #34

Closed c4tz closed 5 years ago

c4tz commented 8 years ago

Hey guys,

What did you do?

Here's my code (yes, it's redundant, I know):

import io, os, pygame, sys
import pyscreenshot as ImageGrab
from PIL import Image
from time import sleep

w = 100
h = 1080
wh = (w,h)
x = 5660
y = 0
xy = (x,y)
box = (x,y,x+w,y+h)

os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)

pygame.init()
pygame.display.init()
screen = pygame.display.set_mode(wh, pygame.NOFRAME)

while True:
    im = ImageGrab.grab(bbox=box)
    buffer = im.tobytes("raw", 'RGB')

    for_trans = pygame.image.fromstring(buffer, wh, 'RGB')

    screen.blit(for_trans, (0,0))
    pygame.display.flip()
    sleep(1)

pygame.quit()
sys.exit()

What did you expect to happen?

When I comment out os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y), everyting works as expected, a 100x1080 window will show, and blit the image taken before on its surface. But when I re-add the line, the following happens:

What actually happened?

Traceback (most recent call last):
  File "test.py", line 22, in <module>
    buffer = im.tobytes("raw", 'RGB')
  File "/usr/lib/python3.5/site-packages/PIL/Image.py", line 681, in tobytes
    e = _getencoder(self.mode, encoder_name, args)
  File "/usr/lib/python3.5/site-packages/PIL/Image.py", line 439, in _getencoder
    return encoder(mode, *args + extra)
ValueError: No packer found from 1 to RGB

Where No packer found from 1 to RGB sometimes is No packer found from P to RGB, too.

I also just noticed, that when I use a 100x100 windows at 0x0, the same error occures, with the line commented out.

When I do im = ImageGrab.grab(bbox=box).convert('RGB'), the error does not appear, but the windows stays black.

Any idea what I'm doing wrong here?

What versions of Pillow and Python are you using?

Python 3.5.2 Pillow 3.4.1 Pygame 1.9.2 pyscreenshot 0.4.2

OS: Arch Linux / Antergos, XFCE, x64

(I already posted this bug in Pillow, but they sent me here)

ponty commented 8 years ago

I could not reproduce this error.

You can try different backends:

# backends=['wx', 'pygtk', 'pyqt', 'scrot', 'imagemagick']
im = ImageGrab.grab(bbox=box, backend='scrot')

Turn on logging for more info:

logging.basicConfig(level=logging.DEBUG)
c4tz commented 8 years ago

I'm trying to keep this as generell as possible, therefore I don't want to rely on a specfic backend to ensure multi-platform support for my application.

I ran it with DEBUG now, that's the output:

DEBUG:pyscreenshot:running "None" in child process
DEBUG:pyscreenshot.loader:forcing:None
DEBUG:easyprocess:param: "['scrot', '-version']" 
DEBUG:easyprocess:command: ['scrot', '-version']
DEBUG:easyprocess:joined command: scrot -version
DEBUG:easyprocess:OSError exception: [Errno 2] No such file or directory: 'scrot'
DEBUG:easyprocess:param: "['import', '-version']" 
DEBUG:easyprocess:command: ['import', '-version']
DEBUG:easyprocess:joined command: import -version
DEBUG:easyprocess:process was started (pid=5607)
DEBUG:easyprocess:process has ended
DEBUG:easyprocess:return code=0
DEBUG:easyprocess:stdout=Version: ImageMagick 6.9.6-0 Q16 x86_64 2016-10-06 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenCL OpenMP 
Delegates (built-in): bzlib cairo fontconfig freetype gslib jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png ps rsvg tiff webp wmf x xml zlib
DEBUG:easyprocess:stderr=
DEBUG:pyscreenshot.loader:selecting plugin:imagemagick
DEBUG:easyprocess:param: "import -silent -window root  -crop '100x1080+5660+0' /tmp/pyscreenshot_imagemagick_nlbpfb4g.png" 
DEBUG:easyprocess:command: ['import', '-silent', '-window', 'root', '-crop', '100x1080+5660+0', '/tmp/pyscreenshot_imagemagick_nlbpfb4g.png']
DEBUG:easyprocess:joined command: import -silent -window root -crop 100x1080+5660+0 /tmp/pyscreenshot_imagemagick_nlbpfb4g.png
DEBUG:easyprocess:process was started (pid=5608)
DEBUG:easyprocess:process has ended
DEBUG:easyprocess:return code=0
DEBUG:easyprocess:stdout=
DEBUG:easyprocess:stderr=
DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13
DEBUG:PIL.PngImagePlugin:STREAM b'gAMA' 41 4
DEBUG:PIL.PngImagePlugin:STREAM b'cHRM' 57 32
DEBUG:PIL.PngImagePlugin:b'cHRM' 57 32 (unknown)
DEBUG:PIL.PngImagePlugin:STREAM b'bKGD' 101 2
DEBUG:PIL.PngImagePlugin:b'bKGD' 101 2 (unknown)
DEBUG:PIL.PngImagePlugin:STREAM b'oFFs' 115 9
DEBUG:PIL.PngImagePlugin:b'oFFs' 115 9 (unknown)
DEBUG:PIL.PngImagePlugin:STREAM b'vpAg' 136 9
DEBUG:PIL.PngImagePlugin:b'vpAg' 136 9 (unknown)
DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 157 38
DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13
DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 41 38
Traceback (most recent call last):
  File "test.py", line 56, in <module>
    buffer = im.tobytes("raw", 'RGB')
  File "/usr/lib/python3.5/site-packages/PIL/Image.py", line 681, in tobytes
    e = _getencoder(self.mode, encoder_name, args)
  File "/usr/lib/python3.5/site-packages/PIL/Image.py", line 439, in _getencoder
    return encoder(mode, *args + extra)
ValueError: No packer found from 1 to RGB

Seems like I don't have scrot installed, anyway ;)

The other message don't seem to be aying something bad to me, though. Can you tell me if something is wrong?

Edit: When I print the image, it gives me:

<PIL.PngImagePlugin.PngImageFile image mode=1 size=100x1080 at 0x7F43A4964198>
ponty commented 8 years ago

Please install scrot and check if it works.