python bitmap_Python win32ui.CreateBitmap方法代码示例

本文详细介绍了Python中win32ui模块的CreateBitmap方法,提供12个实用代码示例,包括屏幕截图、窗口截图等应用场景。通过这些示例,读者可以了解如何创建和使用Bitmap对象进行图像处理。
摘要由CSDN通过智能技术生成

本文整理汇总了Python中win32ui.CreateBitmap方法的典型用法代码示例。如果您正苦于以下问题:Python win32ui.CreateBitmap方法的具体用法?Python win32ui.CreateBitmap怎么用?Python win32ui.CreateBitmap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块win32ui的用法示例。

在下文中一共展示了win32ui.CreateBitmap方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: get_bitmap

​点赞 5

# 需要导入模块: import win32ui [as 别名]

# 或者: from win32ui import CreateBitmap [as 别名]

def get_bitmap() -> image:

"""Get and return a bitmap of the Window."""

left, top, right, bot = win32gui.GetWindowRect(Window.id)

w = right - left

h = bot - top

hwnd_dc = win32gui.GetWindowDC(Window.id)

mfc_dc = win32ui.CreateDCFromHandle(hwnd_dc)

save_dc = mfc_dc.CreateCompatibleDC()

save_bitmap = win32ui.CreateBitmap()

save_bitmap.CreateCompatibleBitmap(mfc_dc, w, h)

save_dc.SelectObject(save_bitmap)

windll.user32.PrintWindow(Window.id, save_dc.GetSafeHdc(), 0)

bmpinfo = save_bitmap.GetInfo()

bmpstr = save_bitmap.GetBitmapBits(True)

# This creates an Image object from Pillow

bmp = image.frombuffer('RGB',

(bmpinfo['bmWidth'],

bmpinfo['bmHeight']),

bmpstr, 'raw', 'BGRX', 0, 1)

win32gui.DeleteObject(save_bitmap.GetHandle())

save_dc.DeleteDC()

mfc_dc.DeleteDC()

win32gui.ReleaseDC(Window.id, hwnd_dc)

# bmp.save("asdf.png")

return bmp

开发者ID:kujan,项目名称:NGU-scripts,代码行数:29,

示例2: GetScreenImg

​点赞 5

# 需要导入模块: import win32ui [as 别名]

# 或者: from win32ui import CreateBitmap [as 别名]

def GetScreenImg(self):

'''

Gets the screen of the window referenced by self.hwnd

'''

if self.hwnd is None:

raise Exception("HWND is none. HWND not called or invalid window name provided.")

self.l, self.t, self.r, self.b = win32gui.GetWindowRect(self.hwnd)

#Remove border around window (8 pixels on each side)

#Remove 4 extra pixels from left and right 16 + 8 = 24

w = self.r - self.l - self.br - self.bl

#Remove border on top and bottom (31 on top 8 on bottom)

#Remove 12 extra pixels from bottom 39 + 12 = 51

h = self.b - self.t - self.bt - self.bb

wDC = win32gui.GetWindowDC(self.hwnd)

dcObj = win32ui.CreateDCFromHandle(wDC)

cDC = dcObj.CreateCompatibleDC()

dataBitMap = win32ui.CreateBitmap()

dataBitMap.CreateCompatibleBitmap(dcObj, w, h)

cDC.SelectObject(dataBitMap)

#First 2 tuples are top-left and bottom-right of destination

#Third tuple is the start position in source

cDC.BitBlt((0,0), (w, h), dcObj, (self.bl, self.bt), win32con.SRCCOPY)

bmInfo = dataBitMap.GetInfo()

im = np.frombuffer(dataBitMap.GetBitmapBits(True), dtype = np.uint8)

dcObj.DeleteDC()

cDC.DeleteDC()

win32gui.ReleaseDC(self.hwnd, wDC)

win32gui.DeleteObject(dataBitMap.GetHandle())

#Bitmap has 4 channels like: BGRA. Discard Alpha and flip order to RGB

#31 pixels from border on top, 8 on bottom

#8 pixels from border on the left and 8 on right

#Remove 1 additional pixel from left and right so size is 1278 | 9

#Remove 14 additional pixels from bottom so size is 786 | 6

#return im.reshape(bmInfo['bmHeight'], bmInfo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值