omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    How to add image(s) from iPad camera roll to Pythonista image library

    Pythonista
    4
    21
    9382
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • themusicman
      themusicman last edited by

      Hi All

      So, how do I do this please? I simply wish to import an image to use as the background layer to a game I am attempting to code. Specifically, it is the background to a Connect4 type game which essentially shows an empty Connect4 board. I want to use that as the background, and then overlay sprites as each player makes their turn. I have done the code for the Connect4 game, now figuring out the correct way to present this on the screen.

      So, to simply import an image from camera roll to use within a pythonista script.

      Ta

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @themusicman last edited by cvp

        @themusicman In the file browser, bottom left, a "+" icon, then import, then photo library

        Edit: and sorry if I did not understand correctly your question

        1 Reply Last reply Reply Quote 0
        • cvp
          cvp last edited by

          You can also work on split view, then drag and drop a photo from your camera roll to Pythonista Files browser

          1 Reply Last reply Reply Quote 0
          • themusicman
            themusicman last edited by themusicman

            So @cvp - I have done this but I am not sure where the image I allegedly imported has been stored within Pythonista. When I try to select that image as I would for any image selection, the image does not appear anywhere.

            So, say I want to use 'emj:Black_Circle' as an image, when I click the bottom right to get the Pythonista dialogue box for adding new image / sound / colour / font etc, the image I just imported is nowhere to be found.

            Specifically, I want to add an image from my photo library to use as the background image for a Pythonista app.

            Am I doing this correctly?

            1 Reply Last reply Reply Quote 0
            • cvp
              cvp last edited by

              When you use the bottom right + button, you don’t import the icon but only it’s name that you can use as ui.Image. named(‘xxx’)

              themusicman 1 Reply Last reply Reply Quote 1
              • themusicman
                themusicman @cvp last edited by

                @cvp said:

                When you use the bottom right + button, you don’t import the icon but only it’s name that you can use as ui.Image. named(‘xxx’)

                Aha... thanks for the clarification. Is there a way one can add to the library of images/sounds etc available within Pythonista?

                I am not planning on using he UI module in this particular script, but have found a way of displaying and possibly using an image for a screen background using the PIL image library.

                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp last edited by cvp

                  The +, Images, Files shows also your imported images

                  My first answer in this topic shows how you can import photos from your camera roll. Import stores in the current folder with the photo name, like IMG01244.JPG

                  1 Reply Last reply Reply Quote 0
                  • themusicman
                    themusicman last edited by

                    Thanks @cvp - when I checked originally, after adding the files via the method you describe, I wasn't seeing any files in the +, images, files area. However, and very typically... when I checked immediately after reading your last reply they are indeed there.

                    Thanks again.

                    PinhoBD 1 Reply Last reply Reply Quote 1
                    • Karina
                      Karina last edited by

                      @cvp and how to add sounds that I need? For example, short guitar chords?

                      cvp 2 Replies Last reply Reply Quote 0
                      • cvp
                        cvp @Karina last edited by cvp

                        @Karina Sorry, do you want to import sound files into Pythonista? And, if yes, where are they?

                        1 Reply Last reply Reply Quote 0
                        • Karina
                          Karina last edited by Karina

                          @cvp I want to get more musical sounds like those that are already embedded, but don't know from where to take it and where to save. It's for a program in which I have musical instruments on the screen, and when tap, they play. I also want to do multi touch
                          Did you ever used your sounds in pythonista?

                          cvp 2 Replies Last reply Reply Quote 0
                          • cvp
                            cvp @Karina last edited by

                            @Karina you can search mp3 in Google, import them in Pythonista and play them.
                            Or even play them as url. Try to search mp3 in the Pythonista forum

                            1 Reply Last reply Reply Quote 0
                            • cvp
                              cvp @Karina last edited by

                              @Karina just for the fun

                              import tempfile
                              import sound
                              import requests
                              import ui
                              
                              def play(url):
                              	with tempfile.NamedTemporaryFile(suffix='.caf') as fp:
                              		r = requests.get(url)
                              		r.raise_for_status()
                              		fp.write(r.content)
                              		sound.play_effect(fp.name)
                              
                              v = ui.View()
                              v.background_color = 'white'
                              
                              b_piano = ui.ButtonItem()
                              b_piano.title = '🎹'
                              def b_piano_action(sender):
                              	play('https://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3')
                              b_piano.action = b_piano_action
                              
                              b_violon = ui.ButtonItem()
                              b_violon.title = '🎻'
                              def b_violon_action(sender):
                              	play('https://ccrma.stanford.edu/~jos/mp3/violin.mp3')
                              b_violon.action = b_violon_action
                              
                              v.right_button_items = (b_piano,b_violon)
                              v.present('fullscreen') 
                              
                              1 Reply Last reply Reply Quote 0
                              • Karina
                                Karina last edited by

                                I didn't understood how it should work)
                                This is what I have now

                                import scene
                                import sound
                                import random
                                import objc_util
                                
                                sw = scene.get_screen_size()[0]
                                sh = scene.get_screen_size()[1]
                                
                                piano_sounds = ['piano:A3', 'piano:C3', 'piano:C4#', 'piano:D4', 'piano:E4', 'piano:F4', 'piano:G3#']
                                guitar_sounds = ['8ve:8ve-beep-warmguitar']
                                drum_sounds = ['drums:Drums_01', 'drums:Drums_04', 'drums:Drums_07', 'drums:Drums_10', 'drums:Drums_13', 'drums:Drums_16']
                                
                                class ButtonNode(scene.ShapeNode):
                                    def __init__(self,
                                                    action,
                                                    icon,
                                                    text_color,
                                                    sounds,
                                                    corner_radius=8,
                                                    border_size=20,
                                                    text='',
                                                    name=f'ButtonNode',
                                                    bg_color='white',
                                                    anchor_point=(0.5, 0.5),
                                                    borderColor=None,
                                                    parent=None,
                                                    position=(0, 0),
                                                    size=(120, 45),
                                                    enabled=True,
                                                    animated_icon=False,
                                                    icon_animation=None,
                                                    *args, **kwargs):
                                
                                        # these mainly for call to super()
                                        self.x, self.y = position
                                        self.w, self.h = size
                                        super().__init__(
                                        	    path=scene.ui.Path.rounded_rect(self.x, self.y, self.w, self.h, corner_radius),
                                                fill_color=bg_color,
                                                stroke_color=borderColor,
                                                shadow=None,
                                                parent=parent,
                                                *args, **kwargs)
                                        
                                        # Normal Properties for Instance()
                                        self.sounds=sounds
                                        self.enabled=enabled
                                        self.button_action=action
                                        self.name=name
                                        self.position=position
                                        self.size=size
                                        self.anchor_point=anchor_point
                                        
                                         # for border
                                        self.border_size=border_size
                                        self.borderColor=bg_color
                                        self.corner_radius=corner_radius
                                        
                                        # for icon
                                        self.icon_animation=icon_animation
                                        self.animated_icon=animated_icon
                                        self.icon=self._init_textures(icon)
                                        
                                        # for Label
                                        self.text=text 
                                        self.text_color=text_color
                                        
                                        # Container to hold each component. 
                                        # is just a dict version of self.children but specific.
                                        self.components=dict({
                                                'icon':None,
                                                'label':None})
                                        
                                        self._setup(self.icon, self.components)
                                
                                    # Type Check to make sure img is a string or ui.Image
                                    def _init_textures(self, img):
                                        if type(img) == str or type(img) == scene.ui.Image:
                                            return scene.Texture(img)
                                        else:
                                            return None
                                
                                    # setup our Components
                                    def _setup(self, i, c):
                                        if i != None:
                                            # button image
                                            c['icon']=scene.SpriteNode(
                                                    texture=i,
                                                    size=scene.Size(self.size[1]/100*80, self.size[1]/100*80), 
                                                    position=scene.Point(0, 0),                                
                                                    parent=self,
                                                    anchor_point=(0.5, 0.5),
                                                    z_position=9)
                                                    
                                        if self.text:
                                            # button text..
                                            c['label']=scene.LabelNode(
                                                    text=self.text,
                                                    position=scene.Point(0 , 0),
                                                    anchor_point=(0.5, 0.5),
                                                    color=self.text_color,
                                                    parent=self,
                                                    z_position=10)
                                    
                                    # called when you tap the button
                                    def Button_Tapped(self):
                                    	if self.components['icon']:
                                    		if self.animated_icon and self.icon_animation:
                                    			self.components['icon'].run_action(self.icon_animation())
                                    	if self.enabled:
                                    		self.button_action(self)
                                
                                # custom action
                                def my_button_action(sender):
                                    random_tone=random.choice(piano_sounds)
                                    play = random.choice(sender.sounds)
                                    if sender.sounds == piano_sounds:
                                    	sound.set_volume(0.3)
                                    elif sender.sounds == guitar_sounds:
                                    	sound.set_volume(1)
                                    sound.play_effect(play)
                                    sound.set_volume(0.5)
                                    return
                                
                                
                                def Animation_Shake(duration=1.5):
                                    action_list=[]
                                    action_list.append(
                                            scene.Action.rotate_to(0.25, duration/10/2))
                                    action_list.append(
                                            scene.Action.sequence(
                                                scene.Action.rotate_to(-0.5, duration/50),
                                                scene.Action.rotate_to(0.5, duration/50)))
                                    action_list.append(
                                        scene.Action.group(
                                            scene.Action.scale_to(1.0, duration/10/2),
                                            scene.Action.rotate_to(0.0, duration/10/2)))
                                    return scene.Action.sequence(action_list)
                                
                                
                                def Animation_Pulse(duration=1.5):
                                    action_list=[]
                                    action_list.append(
                                    	scene.Action.sequence(
                                            scene.Action.scale_to(1.2, duration/10/2),
                                            scene.Action.scale_to(0.5, duration/10/2)))
                                    action_list.append(
                                                scene.Action.scale_to(1.2, duration/50))
                                    action_list.append(
                                        scene.Action.group(
                                            scene.Action.scale_to(1.0, duration/10/2)))
                                    
                                    return scene.Action.sequence(action_list)
                                
                                def Animation_Jump(duration=1.5):
                                    action_list=[]
                                    action_list.append(
                                            scene.Action.sequence(
                                                scene.Action.move_by(0, 20, duration/20),
                                                scene.Action.move_to(0, 0, duration/20)))
                                            
                                    return scene.Action.sequence(action_list)
                                
                                class main(scene.Scene):
                                    def setup(self):
                                        self.buttons=list([])
                                        self.background_color='white'
                                        
                                        self.guitar=ButtonNode(size=scene.Size(160, 112), icon='emj:Guitar',
                                            text=None, text_color=self.background_color, parent=self, action=my_button_action, sounds=guitar_sounds,
                                            position=scene.Point(sw/6*1.5, sh/3), animated_icon=True,
                                            icon_animation=Animation_Shake)
                                        self.buttons.append(self.guitar)
                                        
                                        self.drum=ButtonNode(size=scene.Size(160, 112), icon='IMG_0098.JPG', 
                                            sounds=drum_sounds, text=None, text_color=self.background_color, parent=self, 
                                            action=my_button_action, position=scene.Point(sw/6*3, sh/2), 
                                            animated_icon=True, icon_animation=Animation_Pulse)
                                        self.buttons.append(self.drum)
                                        
                                        self.piano=ButtonNode(size=scene.Size(100, 70), icon='emj:Musical_Keyboard',
                                            text=None, text_color=self.background_color, parent=self, action=my_button_action, sounds=piano_sounds,
                                            position=scene.Point(sw/6*5, sh/2), animated_icon=True,
                                            icon_animation=Animation_Jump)
                                        self.buttons.append(self.piano)
                                        
                                        self.quiter=ButtonNode(size=scene.Size(32, 32),
                                            text='X', text_color='black', parent=self, action=self.quit, sounds=None,
                                            position=scene.Point(sw-50, sh-50), borderColor='black', icon=None)
                                        self.buttons.append(self.quiter)
                                    
                                    def quit(self, sender):
                                        self.view.close()
                                    
                                    def touch_began(self, touch):
                                        for btn in self.buttons:
                                            if self.point_from_scene(touch.location) in btn.frame:
                                                btn.Button_Tapped()
                                
                                scene.run(main())
                                
                                cvp 2 Replies Last reply Reply Quote 0
                                • cvp
                                  cvp @Karina last edited by

                                  @Karina Sorry, I didn't understand your request. What I show is how to play an entire mp3, not a sound like standard ones. Sincerely sorry, forget my post.

                                  1 Reply Last reply Reply Quote 0
                                  • cvp
                                    cvp @Karina last edited by

                                    @Karina you can download a mp3 file and store it in your folders, like an image.
                                    And then, play it like a standard Pythonista file.

                                    1 Reply Last reply Reply Quote 0
                                    • cvp
                                      cvp @Karina last edited by cvp

                                      @Karina run this little script once to download a mp3 as a Pythonista file

                                      import requests
                                      url = 'https://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3'
                                      with open('mypiano.mp3',mode='wb') as fil:
                                      	r = requests.get(url)
                                      	fil.write(r.content) 
                                      

                                      then run your script with this little modif

                                      piano_sounds = ['mypiano.mp3']#piano:A3', 'piano:C3', 'piano:C4#', 'piano:D4', 'piano:E4', 'piano:F4', 'piano:G3#'] 
                                      
                                      1 Reply Last reply Reply Quote 0
                                      • Karina
                                        Karina last edited by

                                        @cvp thanks, I'll try

                                        1 Reply Last reply Reply Quote 0
                                        • PinhoBD
                                          PinhoBD @themusicman last edited by

                                          @cvp Hi, I tried your approach: +, import, files, Browse, and pick an image file, say, abc.jpg. It imported the file into the current directory, say, /Examples/abc.jpg and the image of abc. But when I checked the + at the lower right corner of the script editor, then clicked open files. There is neither image of abc nor abc.jpg file. I would like to import an image file into + / images/files, so that I can easily enter the image using ui.Imag e.named().I did try many times and some how I did succeed once,but I couldn’t repeat. Please help. Thanks a lot.

                                          cvp 1 Reply Last reply Reply Quote 0
                                          • cvp
                                            cvp @PinhoBD last edited by

                                            @PinhoBD strange. In + / images / files, I see my thousands images, after some time, I agree

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors