问题描述
对于此项目,我正在使用Windows API截屏(以处理多屏)并将其转换为PIL图像;然后根据需要在窗口周围添加阴影.
For this project, I'm taking screenshots with the Windows API (to deal with multi-screens) and convert it to a PIL image; then I add a shadow around the window if wanted.
我的问题是,屏幕截图实际上是窗口矩形的;意思是我捕捉到了四舍五入的角落背后的背景,我不想要那样.我在谷歌上搜索了很多,并找到了有关透明度的文档和教程,我想我应该找到一种方法来获取窗口的形状,以便使其成为我要应用于(矩形)图像的蒙版.得到.但是我还是没找到那个面具.有人可以帮忙吗?
My problem is, the screenshot is actually of the window's rectangle; meaning I capture the background behind it around rounded-corners and I don't want that. I googled quite a bit and found docs and tuts around transparency, and I'm guessing I should find a way to get the shape of the window in order to make it a mask that I would apply to the (rectangle) image i've got. But I found noway to get that mask. Could any one help?
下面是我的代码:
hwnd = win32gui.GetForegroundWindow()
l, t, r, b = win32gui.GetWindowRect(hwnd)
w = r - l
h = b - t
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
saveDC.SelectObject(saveBitMap)
saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY)
#add cursor
if showcursor:
curFlags, curH, (curX, curY) = win32gui.GetCursorInfo()
saveDC.DrawIcon((curX, curY), curH)
#load into PIL image
"""http://stackoverflow.com/questions/4199497/image-frombuffer-with-16-bit-image-data"""
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
im = Image.frombuffer(
'RGB',
(bmpinfo['bmWidth'], bmpinfo['bmHeight']),
bmpstr, 'raw', 'BGRX', 0, 1)
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)
return im
下面是蓝色背景上方的窗口的屏幕截图:
Below is a slightly magnified screenshot of a window above a blue background:
您会看到不应该有蓝色的角.
As you can see there are blue corners that shouldn't be there.
推荐答案
为什么不使用边缘检测用于检测窗口边缘的算法(例如Prewitt或Sobel),您只需将alpha通道设置为图像限制和窗口边缘限制之间的像素即可.
Why not use a Edge detection algorithm (f.e. Prewitt or Sobel) to detect the window edge, you only need to set the alpha channel to the pixels between the image limits and the window edge limits.
这篇关于使用Python PIL和Windows API的活动窗口屏幕截图:如何处理圆角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!