用pil库写个后台窗口截图
时间: 2023-05-22 16:06:51 浏览: 88
可以使用以下代码实现:
```
from PIL import ImageGrab
# 截取整个屏幕
img = ImageGrab.grab()
# 或者指定窗口坐标进行截取
# img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
# 保存截图
img.save("screenshot.png")
```
注意:如果你要在服务器上使用这个功能,请确保你的用户具有屏幕截取的权限。
相关问题
帮我用PIL库写一个给图片添加水印的代码
好的,以下是使用PIL库给图片添加水印的Python代码示例:
```python
from PIL import Image, ImageDraw, ImageFont
def add_watermark(text, input_image_path, output_image_path):
photo = Image.open(input_image_path)
# 设置文本字体
font = ImageFont.truetype("arial.ttf", 36)
# 创建绘图对象
drawing = ImageDraw.Draw(photo)
# 在图片指定位置绘制文本
textwidth, textheight = drawing.textsize(text, font)
width, height = photo.size
pos = (width - textwidth)//2, (height - textheight)//2
drawing.text(pos, text, fill=(255, 255, 255), font=font)
# 保存处理后的图片
photo.save(output_image_path)
# 测试
add_watermark("chit-chat AI", "input.jpg", "output.jpg")
```
以上代码将输入图片(input.jpg)中心位置添加了“chit-chat AI”水印,并将结果保存到输出图片(output.jpg)。如果需要修改水印文本、字体、字号、颜色、位置等参数,可以根据实际需求进行调整。
帮我用PIL库写一个给图片添加图片水印的代码
好的,以下是一份使用PIL库给图片添加图片水印的Python代码:
```
from PIL import Image, ImageDraw, ImageFont
def watermark_image(input_image_path, output_image_path, watermark_image_path, position):
base_image = Image.open(input_image_path)
watermark = Image.open(watermark_image_path)
width, height = base_image.size
transparent = Image.new('RGBA', (width, height), (0,0,0,0))
transparent.paste(base_image, (0,0))
transparent.paste(watermark, position, mask=watermark)
transparent.save(output_image_path)
if __name__ == '__main__':
input_image = 'input.jpg'
output_image = 'output.jpg'
watermark_image = 'watermark.png'
position = (0, 0) # left-top corner
watermark_image(input_image, output_image, watermark_image, position)
```
以上代码中,我们首先使用PIL库打开需要添加水印的图片和水印图片。然后创建一个空白透明图片,并将两个图片拷贝到这个透明图片上。最后将拷贝完水印图片的透明图片保存为输出图片。我们可以通过设置水印图片的放置位置来控制水印的位置。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)