Skip to content Skip to sidebar Skip to footer

How To Make An Image And Display It On The Next Page

Good afternoon, i wrote a simple camera program using Python and Kivy, but I just can't get a shot. You need to take a snapshot of 'ScreenThree' (save it in the phone's memory) and

Solution 1:

Access photo

Reference the photo using the following:

classScreenFour(Screen):defon_pre_enter(self, *args):
        self.ids.img.source = self.manager.ids.entry.photo

Solution

Note

<class-name@widget>: 

This is a dynamic class. When using dynamic class, there is no need for class definition in Python script.

kv file

  • Change all dynamic classes to class rule by removing all @Screen.
  • Add a class rule, <ScreenManagement>:
  • Instantiate all the screens as child of class rule, <ScreenManagement>:
  • Add id: entry under instantiated child, ScreenThree:
  • Instantiate, Image: as child of FloatLayout: of class rule, <ScreenFour>:
  • Add id: img under instantiated child, Image:

Snippets

#:import MDIconButton kivymd.button.MDIconButton<ScreenManagement>:ScreenOne:name:"screen_one"ScreenTwo:name:"screen_two"ScreenThree:id:entryname:"screen_three"ScreenFour:name:"screen_four"...<ScreenFour>:canvas:Color:rgb: [.30,.50,.99]
        Rectangle:pos:self.possize:self.sizeFloatLayout:Image:id:img

Py file

  • Add import statement, from os.path import dirname
  • Add import statement, from kivy.properties import StringProperty
  • Add class ScreenManagement()
  • Remove all references to screen_manager
  • Declare class attribute, photo = StringProperty('') under class ScreenThree()
  • Initialize class attribute, self.photo = f"{dirname(__file__)}/IMG_{time.strftime('%Y%m%d_%H%M%S')}.png"
  • Implement method on_pre_enter() to display photo.

Snippets

from os.path import dirname
from kivy.properties import StringProperty
...
classScreenThree(Screen):
    photo = StringProperty('')

    defcapture(self):
        camera = self.ids['camera']
        self.photo = f"{dirname(__file__)}/IMG_{time.strftime('%Y%m%d_%H%M%S')}.png"
        camera.export_to_png(self.photo)
        print("Captured")


classScreenFour(Screen):

    defon_pre_enter(self, *args):
        self.ids.img.source = self.manager.ids.entry.photo


classScreenManagement(ScreenManager):
    passclassInterface(App):

    defbuild(self):
        return ScreenManagement()

Example

main.py

fromos.pathimportdirnamefromkivy.appimportAppfromkivy.langimportBuilderfromkivy.uix.screenmanagerimportScreenManager,Screenfromkivymd.themingimportThemeManagerfromkivy.propertiesimportStringPropertyimporttimeBuilder.load_string("""#:import MDFillRoundFlatButton kivymd.button.MDFillRoundFlatButton#:import MDIconButton kivymd.button.MDIconButton<ScreenManagement>:ScreenOne:name:"screen_one"ScreenTwo:name:"screen_two"ScreenThree:name:"screen_three"id:entryScreenFour:name:"screen_four"<ScreenOne>:canvas:Color:rgb: [.30,.50,.99]
        Rectangle:pos:self.possize:self.sizeFloatLayout:MDFillRoundFlatButton:color: [1,1,1,1]
            text:"Перейти к созданию фото"pos_hint: {'center_x':.50, 'center_y':.50}
            on_press:root.manager.transition.direction='up'root.manager.transition.duration=1root.manager.current='screen_two'<ScreenTwo>:canvas:Color:rgb: [.30,.50,.99]
        Rectangle:pos:self.possize:self.sizeFloatLayout:MDFillRoundFlatButton:color: [1,1,1,1]
            text:"Выбрать фон"pos_hint: {'center_x':.50, 'center_y':.10}
            on_press:root.manager.transition.direction='up'root.manager.transition.duration=1root.manager.current='screen_three'MDIconButton:icon:'chevron-double-right'pos_hint: {'center_x':.95, 'center_y':.10}
            on_press:root.manager.transition.direction='down'root.manager.transition.duration=1root.manager.current='screen_one'<ScreenThree>:id:entrycanvas:Color:rgb: [.30,.50,.99]
        Rectangle:pos:self.possize:self.sizeFloatLayout:Camera:id:cameraindex:0resolution:(1280,720)play:TrueMDFillRoundFlatButton:text:"take photo"pos_hint: {'center_x':0.50, 'center_y':.10}
            on_press:root.capture()#TAKE PHOTOroot.manager.transition.direction='up'root.manager.transition.duration=1root.manager.current='screen_four'MDIconButton:icon:'chevron-double-right'pos_hint: {'center_x':.95, 'center_y':.10}
            on_press:root.manager.transition.direction='down'root.manager.transition.duration=1root.manager.current='screen_two'<ScreenFour>:canvas:Color:rgb: [.30,.50,.99]
        Rectangle:pos:self.possize:self.sizeFloatLayout:Image:id:imgMDIconButton:icon:'chevron-double-right'pos_hint: {'center_x':.95, 'center_y':.10}
            on_press:root.manager.transition.direction='down'root.manager.transition.duration=1root.manager.current='screen_three'""")


class ScreenOne(Screen):
    pass


class ScreenTwo(Screen):
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'Blue'
    main_widget = None


class ScreenThree(Screen):
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'Blue'
    main_widget = None
    photo = StringProperty('')

    def capture(self):
        camera = self.ids['camera']
        self.photo = f"{dirname(__file__)}/IMG_{time.strftime('%Y%m%d_%H%M%S')}.png"camera.export_to_png(self.photo)print("Captured")classScreenFour(Screen):defon_pre_enter(self,*args):self.ids.img.source=self.manager.ids.entry.photoclassScreenManagement(ScreenManager):passclassInterface(App):defbuild(self):returnScreenManagement()sample_app=Interface()sample_app.run()

Output

Photo

Solution 2:

fromkivy.appimportAppfromkivy.langimportBuilderfromkivy.uix.screenmanagerimportScreenManager,Screenfromkivy.uix.cameraimportCamerafromkivymd.themingimportThemeManagerimporttimefilename=''screenfour=''Builder.load_string("""#:import MDFillRoundFlatButton kivymd.button.MDFillRoundFlatButton#:import MDIconButton kivymd.button.MDIconButton<ScreenOne@Screen>:canvas:Color:rgb: [.30,.50,.99]
        Rectangle:pos:self.possize:self.sizeFloatLayout:MDFillRoundFlatButton:color: [1,1,1,1]
            text:"Перейти к созданию фото"pos_hint: {'center_x':.50, 'center_y':.50}
            on_press:root.manager.transition.direction='up'root.manager.transition.duration=1root.manager.current='screen_two'<ScreenTwo@Screen>:canvas:Color:rgb: [.30,.50,.99]
        Rectangle:pos:self.possize:self.sizeFloatLayout:MDFillRoundFlatButton:color: [1,1,1,1]
            text:"Выбрать фон"pos_hint: {'center_x':.50, 'center_y':.10}
            on_press:root.manager.transition.direction='up'root.manager.transition.duration=1root.manager.current='screen_three'MDIconButton:icon:'chevron-double-right'pos_hint: {'center_x':.95, 'center_y':.10}
            on_press:root.manager.transition.direction='down'root.manager.transition.duration=1root.manager.current='screen_one'<ScreenThree@Screen>:id:entrycanvas:Color:rgb: [.30,.50,.99]
        Rectangle:pos:self.possize:self.sizeFloatLayout:Camera:id:cameraindex:0resolution:(1280,720)play:TrueMDFillRoundFlatButton:text:"take photo"pos_hint: {'center_x':0.50, 'center_y':.10}
            on_press:root.capture()#TAKE PHOTOroot.manager.transition.direction='up'root.manager.transition.duration=1root.manager.current='screen_four'MDIconButton:icon:'chevron-double-right'pos_hint: {'center_x':.95, 'center_y':.10}
            on_press:root.manager.transition.direction='down'root.manager.transition.duration=1root.manager.current='screen_two'<ScreenFour@Screen>:canvas:Color:rgb: [.30,.50,.99]
        Rectangle:pos:self.possize:self.sizeFloatLayout:Image:size:root.width,root.heightsource:''id:imageWidMDIconButton:icon:'chevron-double-right'pos_hint: {'center_x':.95, 'center_y':.10}
            on_press:root.manager.transition.direction='down'root.manager.transition.duration=1root.manager.current='screen_three'""")


class ScreenOne(Screen):
    pass


class ScreenTwo(Screen):
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'Blue'
    main_widget = None

class ScreenThree(Screen):
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'Blue'
    main_widget = None

    def capture(self):
        camera = self.ids['camera']
        timestr = time.strftime("%Y%m%d_%H%M%S")photo=camera.export_to_png("IMG_{}.png".format(timestr))globalfilename,screenfourfilename="IMG_{}.png".format(timestr)print("Captured")screenfour.update()classScreenFour(Screen):def__init__(self,**kwargs):super(ScreenFour,self).__init__(**kwargs)globalscreenfourscreenfour=selfdefupdate(self,*args):globalfilenameimageWidget=self.ids['imageWid']imageWidget.source=filenamescreen_manager=ScreenManager()screen_manager.add_widget(ScreenOne(name="screen_one"))screen_manager.add_widget(ScreenTwo(name="screen_two"))screen_manager.add_widget(ScreenThree(name="screen_three"))screen_manager.add_widget(ScreenFour(name="screen_four"))classInterface(App):defbuild(self):returnscreen_managersample_app=Interface()sample_app.run()

You'd create a global variable to save the picture's filename, and then an Image widget in your screenfour Screen, where you'd update the Image widget's source to the filename of the picture you took. Feel free to contact me if any issues arise

Post a Comment for "How To Make An Image And Display It On The Next Page"