【问题标题】:Click screenshot of part of the screen using python kivy使用python kivy点击部分屏幕截图
【发布时间】:2020-10-27 15:25:50
【问题描述】:

目前,下面的代码显示了使用笔记本电脑网络摄像头的相机布局 - 我想在相机窗口中显示一个矩形框架 - 用户将拿着一本书与框架对齐,我需要捕捉这本书的图像即捕获帧内部分的图像。我正在努力

  1. 将透明矩形显示为框架(这是在相机浮动布局内的框布局内)
  2. 仅抓取框架内部分的图像

在相机布局下方有一个按钮,单击该按钮,图像将保存到机器上的文件夹中 请有人指导我如何继续以及是否可以使用任何其他模块以任何其他方式实现这一点

import kivy
from PIL import ImageGrab
from kivy.uix.boxlayout import BoxLayout
from numpy import shape

kivy.require('1.7.2')

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.camera import Camera
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle



class CamApp(App):
# Function to take a screenshot
def screengrab(self, *largs):
    im2 = ImageGrab.grab(bbox=None)
    im2.show()
    #outname = self.fileprefix + '_%(counter)04d.png'
    #Window.screenshot(name=outname)



def build(self):
    # create a floating layout as base
    camlayout = FloatLayout(size=(600, 600))
    cam = Camera()  # Get the camera
    cam = Camera(resolution=(1024, 1024), size=(300, 300))
    cam.play = True  # Start the camera
    camlayout.add_widget(cam)
    boxlayout = BoxLayout(id='imageBox', size_hint=[0.5, 0.7], pos_hint={'center_x': .5, 'center_y': .5})
    boxlayout.bind(size=self.update_rect, pos=self.update_rect)
    with boxlayout.canvas.before:
        #Color(0, 1, 0, 1)  # green; colors range from 0-1 instead of 0-255
        self.rect = Rectangle(size=boxlayout.size,
                              pos=boxlayout.pos, outline='black')

    camlayout.add_widget(boxlayout)

    button = Button(text='Take Picture', size_hint=(0.12, 0.12))
    button.bind(on_press=self.screengrab)
    camlayout.add_widget(button)  # Add button to Camera Layout

    self.fileprefix = 'snap'

    return camlayout
def update_rect(self,instance, value):
    self.rect.pos = instance.pos
    self.rect.size = instance.size
if __name__ == '__main__':
    CamApp().run() 

Click to see output

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    在 kivy 中,Color() 函数接受表示 RGBA 的参数。这意味着最后一个参数是 alpha,传递一个浮点数将使其透明。 因此,Color(0, 1, 0, 1) 是纯色,Color(0, 1, 0, .5) 将是半透明的。

    至于只抓取部分帧的图像,您可以考虑抓取整个帧,然后使用 PIL 裁剪图像

    from PIL import Image 
    
    im = Image.open(r"C:\path\to\picture\my_screenshot.png") 
    im.crop((left, top, right, bottom))
    

    只需将左、上、右和下替换为您要剪切的尺寸即可。

    【讨论】:

      猜你喜欢
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多