简体   繁体   English

如何使用python窗口将屏幕的可裁剪png图像保存到桌面?

[英]How do you save a croppable png image of your screen to your desktop using python windows?

I am having trouble saving a full screen image/screenshot to my desktop using python. 我在使用python将全屏图像/屏幕截图保存到桌面时遇到麻烦。 I have tried 我努力了

from PIL import ImageGrab
import os
img = img.crop((10, 10, 650, 2000))
mageGrab.grab().save(r"screen_capture.png")
stuffs = process_image(Image.open(r"C:\Users\jacro\pycharmprojects\untitled\screen_capture.png"))
stuffs.save(SCREEN_DIR + r"\screen.png")

But get the picture attached. 但是附上图片。 This happens when I crop the last parameter differently too. 当我也不同地裁剪最后一个参数时,会发生这种情况。 Plz help. 请帮助。

image 1 图片1

I am assuming you need to crop the screenshot to desired size. 我假设您需要将屏幕截图裁剪为所需的大小。 If you are using static values, you will need to be careful as values may change with resolution. 如果您使用静态值,则由于数值可能会随着分辨率的变化而变化,因此需要小心。 You can do this statically as below (re-arranging your values). 您可以按以下方式静态执行此操作(重新排列值)。

img2 = img.crop((0, -1000, 2000, 650))

However, preferred way is dynamically calculating crop co-ordinates. 但是,首选方法是动态计算作物坐标。 like below example ( Inspiration from here ) 像下面的例子( 从这里得到启示

from PIL import ImageGrab,Image
import os

ImageGrab.grab().save(r"screen_capture.png")
img = Image.open(r".//screen_capture.png")
image_width = img.size[0]
image_height = img.size[1]
horizontal_padding = (max(img.size) - image_width) / 2  #horizontal center 
vertical_padding = (max(img.size) - image_height) / 2    #vertical center

img3 = img.crop(
    (   -horizontal_padding,
        -vertical_padding,
        image_width + horizontal_padding,
        image_height + vertical_padding
    ))
img3.save("img3.png")

Centrally Cropped Screen-Capture 集中裁剪的屏幕捕获 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM