Search code examples
pythonkivyimage-gallery

How to make gallery in kivy?


I want to this gallery in kivy how to make gallery view How to add this kivy then is showing many display and carousel in gallery? How to make an image and display it on the next page Anyone can help this?

class Gallery(Screen):
        pass 

.kv

<MyTile@SmartTile>:
        size_hint_y: None
        height: "240dp"
    
<Gallery>
    ScrollView:
        MDGridLayout:
            cols: 3
            row_default_height: (self.width - self.cols*self.spacing[0]) / self.cols
            row_force_default: True
            adaptive_height: True
            padding: dp(4), dp(4)
            spacing: dp(4)
            MyTile:
                source:'*.jpg'

https://i.sstatic.net/loupw.png

Here's got problem..

[ WARN:1] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-vi271kac\opencv\modules\videoio\src\cap_msmf.cpp (376) `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): OnReadSample() is called with error status: -1072873821
[ WARN:1] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-vi271kac\opencv\modules\videoio\src\cap_msmf.cpp (388) `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): async ReadSample() call is failed with error status: -1072873821
[INFO   ] [Loader      ] using a thread pool of 2 workers
[ERROR  ] [AsyncImage  ] Not found <*.jpg>
[INFO   ] [Base        ] Start application main loop
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-vi271kac\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

Here's gallery but is not showing any picture in here.. Anyone can tell this code what is problem in codes?


Solution

  • I sketched an example of a gallery, I think this is what you want.

    from kivymd.app import MDApp
    
    from kivy.metrics import dp
    from kivy.lang.builder import Builder
    from kivy.uix.boxlayout import BoxLayout
    
    import os
    
    KV = """
    <ImageButton@ButtonBehavior+FitImage>
    
    <ImageManager>
        path: ""
        orientation: "vertical"
        size_hint_y: None
        height: root.height
        padding: dp(10)
    
        ImageButton:
            source: root.path
            
    BoxLayout:
        RecycleView:
            id: rv
            key_viewclass: "viewclass"
            RecycleGridLayout:
                padding: dp(2)
                cols: 3
                default_size: None, dp(48)
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
    """
    
    
    class ImageManager(BoxLayout):
        pass
    
    
    class GalleryApp(MDApp):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
    
            self.manager_list = []
            self.dir = os.getcwd()
            self.available_image_format = ['.png', '.jpg', '.jpeg', '.bmp']  # etc
    
        def build(self):
            return Builder.load_string(KV)
    
        def on_start(self):
            self.load_images()
    
        def load_images(self):
            if not self.manager_list:
                for image in os.listdir(self.dir):
                    target_filename, target_file_extension = os.path.splitext(image)
                    if target_file_extension in self.available_image_format:
                        path_to_image = os.path.join(self.dir, image)
                        self.manager_list.append(
                            {
                                "viewclass": "ImageManager",
                                "path": path_to_image,
                                "height": dp(200),
                            }
                        )
                self.root.ids.rv.data = self.manager_list
    
    
    GalleryApp().run()