python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.3k stars 2.23k forks source link

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

Closed c4tz closed 8 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

wiredfool commented 8 years ago

I'd say that this is likely a bug in pyscreenshot.

wiredfool commented 8 years ago

Closing. Reopen or submit again if there's a smoking gun pointing at Pillow.