kivy / kivy

Open source UI framework written in Python, running on Windows, Linux, macOS, Android and iOS
https://kivy.org
MIT License
17.74k stars 3.07k forks source link

Screenshot functions don't return images of their active kivy app #5991

Closed CFaldon closed 6 years ago

CFaldon commented 6 years ago

Versions

Description

I am trying to take a screenshot of the lines drawn in my Kivy paint app. None of the methods I have tried actually take a picture of the Kivy app itself. I have attempted the following methods:

self.export_to_png('image_1.png')
ImageGrab.grab_to_file('image_2.png')
self.screenshot('image_3.png') 

export_to_png() produces a random tiled image. ImageGrab() takes a screenshot of the screen underneath the kivy app. screenshot() produces an error, as I can't figure out its syntax.

Code and Logs

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Line
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen , ScreenManager

import pyscreenshot as ImageGrab

class Home(Screen):
    pass

class DrawInput(Widget):

    def on_touch_down(self, touch):
        with self.canvas:
            touch.ud["line"] = Line(points=(touch.x, touch.y))           

    def on_touch_move(self, touch):
        touch.ud["line"].points += (touch.x, touch.y)

    def clear_canvas(self):
        self.canvas.clear()

class Screenshot(Widget):   

    def take(self):
        self.export_to_png('image_1.png')
        ImageGrab.grab_to_file('image_2.png')
        self.screenshot('image_3.png')  

class SimpleKivy(App):
    def build(self):
        return

if __name__ == "__main__":
    SimpleKivy().run()

Kivy file:

<Button>:
    font_size: 40
    color: 1,1,1,1

ScreenManager:

    Home:

        name: 'home'
        DrawInput:
            id: widget_clear

        Screenshot:
            id: widget_screenshot

        FloatLayout:

            Button:
                text: "Clear"
                pos_hint: {"x": 0, 'top': 0.6666}
                size_hint: 0.2, 0.3333
                on_release: 
                    widget_clear.clear_canvas()

            Button:
                text: "Save"
                pos_hint: {"x": 0.8, 'top': 0.6666}
                size_hint: 0.2, 0.3333
                on_release:
                    widget_screenshot.take()
                    widget_clear.clear_canvas()
tito commented 6 years ago

May be support, but if your goal is to take a screenshot of your Kivy app, self.export_to_png will work only for "self" aka your Screenshot widget, not the entire app. (It put the widget in a fbo, render and export).

There is a method in window for that: https://kivy.org/doc/stable/api-kivy.core.window.html#kivy.core.window.WindowBase.screenshot

So:

from kivy.core.window import Window
Window.screenshot()
support[bot] commented 6 years ago

👋 We use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please use our support channels to get help with the project.

If you're having trouble installing Kivy, make sure to check out the installation docs for Windows, Linux and macOS.

Let us know if this comment was made in error, and we'll be happy to reopen the issue.