Skip to content

Commit 0f21d6f

Browse files
committed
Fix screenshot resizing with modern Pillow
1 parent 0ad507f commit 0f21d6f

3 files changed

Lines changed: 56 additions & 22 deletions

File tree

pythonhere/magic_here/shortcuts.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def screenshot(ctx, width, output):
6666
img = PILImage.open(BytesIO(data)).convert("RGB")
6767
if width:
6868
height = int(width * img.size[1] // img.size[0])
69-
img = img.resize((width, height), PILImage.ANTIALIAS)
69+
resampling = getattr(PILImage, "Resampling", PILImage)
70+
resample_filter = getattr(resampling, "LANCZOS", PILImage.LANCZOS)
71+
img = img.resize((width, height), resample_filter)
7072

7173
if output:
7274
img.save(output)

tests/test_magic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ async def test_screenshot_saved_to_file(tmpdir, app_instance, call_there_group):
3939
assert os.path.exists(output)
4040

4141

42+
@pytest.mark.asyncio
43+
async def test_screenshot_resized_to_width(tmpdir, app_instance, call_there_group):
44+
output = Path(tmpdir) / "test.png"
45+
46+
call_there_group(["screenshot", "--width", "100", "-o", output], "")
47+
48+
image = shortcuts.PILImage.open(output)
49+
assert image.size == (100, 75)
50+
51+
4252
@pytest.mark.asyncio
4353
async def test_pin_command_pin_shortcut_called(
4454
mocker, capfd, mocked_android_modules, call_there_group, test_py_script

uv.lock

Lines changed: 43 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)