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.


    Managing photos

    Pythonista
    2
    31
    6096
    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.
    • frankL
      frankL last edited by ccc

      I am trying to pick a photo from my iphone camera roll and then store the location of the photo in a csv file. Then be able to open the csv file, retrieve the location, and view the photo. I am able to pick the photo, print the “_photos2.Asset” class to the console which looks like this:
      <Asset E00EEA1D-F532-4315-9331-5E2718A533B/L0/001 – image (1002 x 894)>
      And the photo is shown in the console. Here is my script:

      import photos
      import ui
      import csv
      all_assets = photos.get_assets()
      assets = photos.pick_asset(assets=all_assets, title=’Pick a photo’)
      new_asset = assets[0]
      print(new_asset)
      img = new_asset.get_ui_image()
      img.show()
      

      How can I get the photo location in a csv storable form, store it in a csv file, and once I open the csv and retrieve the location, show the photo either in the console or preferably in a subview?

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

        @frankL I thought you already did it in the topic

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

          That was for videos. Photos seems to be different.

          cvp 3 Replies Last reply Reply Quote 0
          • cvp
            cvp @frankL last edited by

            @frankL ok, sorry, mistake

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

              @frankL try this with a photo of which you know the name:

              import ui
              import os
              
              path = '/var/mobile/Media/DCIM/'#108APPLE/IMG_8955.JPG'
              
              v = ui.ImageView()
              v.present('sheet')
              l = os.listdir(path)
              for apple in l:
              	ll = os.listdir(path+apple)
              	print(apple)
              	for ff in ll:
              		print(ff)
              		if ff == 'IMG_8662.PNG':
              			v.image = ui.Image.named(path+apple+'/'+ff)
              
              
              1 Reply Last reply Reply Quote 0
              • cvp
                cvp @frankL last edited by

                @frankL or

                import photos
                import ui
                import os
                
                assets = photos.get_assets()
                asset = photos.pick_asset(assets)
                fil = str(ObjCInstance(asset).filename())
                print(fil)
                
                path = '/var/mobile/Media/DCIM/'#108APPLE/IMG_8955.JPG'
                
                v = ui.ImageView()
                v.present('sheet')
                l = os.listdir(path)
                for apple in l:
                	ll = os.listdir(path+apple)
                	print(apple)
                	for ff in ll:
                		print(ff)
                		if ff == fil:
                			v.image = ui.Image.named(path+apple+'/'+ff)
                
                1 Reply Last reply Reply Quote 0
                • frankL
                  frankL last edited by

                  For the first script, how do I get the name of the photo? I ran the script as is and it printed thousands of items in the console each in the form of:
                  IMG_xxxx.HEIC
                  I assume that there is one of these for every photo in my camera roll.

                  For the second script, after I picked a photo, I get an error message:

                  fil = str(ObjCInstance(asset).filename())
                  NameError: name 'ObjCInstance' is not defined.

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

                    @frankL sorry, I forgot

                    from objc_util import *
                    

                    I guess it was loaded on my Pythonista by another script I ran

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

                      @frankL said:

                      thousands of items

                      Ok, comment the print lines

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

                        With 'from objc_util import *' the second script runs and with the second and third print lines commented, I get the selected image name "IMG_1343.JPG" and the image is displayed in the subview. How do I find out which directory "/xxxAPPLE/ the image is in? When I uncommented the second print line, I get a list of about 30 directories.

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

                          @frankL that's all folks

                          import photos
                          import ui
                          from objc_util import *
                          assets = photos.get_assets()
                          asset = photos.pick_asset(assets)
                          fil = str(ObjCInstance(asset).filename())
                          dir = str(ObjCInstance(asset).directory())
                          path = '/var/mobile/Media/' + dir + '/' + fil
                          v = ui.ImageView()
                          v.present('sheet')
                          v.image = ui.Image.named(path)			
                          
                          1 Reply Last reply Reply Quote 0
                          • frankL
                            frankL last edited by

                            Perfect! Thank you!

                            1 Reply Last reply Reply Quote 0
                            • frankL
                              frankL last edited by ccc

                              cvp, one more question: I have the following script in which I can pick a photo and save it to a csv file with a button push or show the previously saved photo with the other button push. The only problem is that I haven't been able to change the size or aspect ratio of the displayed photo. I tried setting the frame for the subview and that doesn't seem to work. How can I fix this? Thanks.

                              import photos
                              import ui
                              from objc_util import *
                              import csv
                              import os
                              #
                              def open_photo_ref():
                              	matrix=[]
                              	my_path=os.path. abspath("photo_file.csv")
                              	with open(my_path,'r',encoding='utf-8') as reader:
                              		reader=csv.DictReader(reader)
                              		for row in reader:
                              			matrix.append([row['Index'], row['Path']])
                              	return matrix
                              #
                              #
                              def save_photo(photo_path):
                              	save_path='/private/var/mobile/Containers/Shared/AppGroup/99B62839-B39E-4ADF-9308-D5D986E28E91/Pythonista3/Documents/Frank/photo_file.csv'
                              	headings=['Index', 'Path']
                              	with open(save_path, 'w', newline='',encoding='utf-8') as out_file:
                              			csv.writer(out_file).writerow(headings)
                              			new_row = [0, photo_path]
                              			csv.writer(out_file). writerow(new_row)
                              	return
                              
                              
                              #
                              #		       MAIN PROGRAM             #
                              #
                              #
                              w, h = ui.get_screen_size()
                              h -= 64
                              bh = bw = 80  # label height and button width 
                              mg = 10  # margin 
                              view = ui.View(name='Show Photo', bg_color='#D98880', frame=(0, 0, w, h)) 
                              #
                              def photo_action(sender):
                              	matrix = open_photo_ref()
                              	row= matrix[0]
                              	my_path = row[1]
                              	v = ui.ImageView(frame=(0,0,500,200))
                              	v.image = ui.Image.named(my_path)
                              	v.present('sheet')
                              	return
                              #
                              photo_button = ui.Button(frame=(150,10,130,0),border_color='black', border_width=2, corner_radius = 10, tint_color = 'black', background_color='#EAECEE', action=photo_action, font=('Arial Rounded MT Bold',18)) photo_button.title='show'
                              photo_button.width=100
                              photo_button.height=40
                              view.add_subview(photo_button)
                              
                              #
                              def pick_action(sender):
                              	assets = photos.get_assets()
                              	asset = photos.pick_asset(assets)
                              	fil = str(ObjCInstance(asset).filename())
                              	dir = str(ObjCInstance(asset).directory())
                              	path = '/var/mobile/Media/' + dir + '/' + fil
                              	path_string = str(path)
                              	save_photo(path_string)
                              	return
                              #
                              pick_button = ui.Button(frame=(10,10,130,0),border_color='black', border_width=2, corner_radius = 10, tint_color = 'black', background_color='#EAECEE', action=pick_action, font=('Arial Rounded MT Bold',18)) pick_button.title='pick'
                              pick_button.width=100
                              pick_button.height=40
                              view.add_subview(pick_button)
                              
                              #########################################
                              nav_view = ui.NavigationView(view)
                              nav_view.present('sheet')
                              #########################################
                              
                              cvp 1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @frankL last edited by cvp

                                @frankL I could help but, first of all, please post your code between 2 lines of 3 backsticks or use button </> to insert your code.

                                Otherwise, we don't see your code indentation and can't copy it easily for testing.

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

                                  Is this better?

                                  import photos
                                  import ui
                                  from objc_util import *
                                  import csv
                                  import os
                                  #
                                  def open_photo_ref():
                                  	matrix=[]
                                  	my_path=os.path. abspath("photo_file.csv")
                                  	with open(my_path,'r',encoding='utf-8') as reader:
                                  		reader=csv.DictReader(reader)
                                  		for row in reader:
                                  			matrix.append([row['Index'], row['Path']])
                                  	return matrix
                                  #
                                  #
                                  def save_photo(photo_path):
                                  	save_path='/private/var/mobile/Containers/Shared/AppGroup/99B62839-B39E-4ADF-9308-D5D986E28E91/Pythonista3/Documents/Frank/photo_file.csv'
                                  	headings=['Index', 'Path']
                                  	with open(save_path, 'w', newline='',encoding='utf-8') as out_file:
                                  			csv.writer(out_file).writerow(headings)
                                  			new_row = [0, photo_path]
                                  			csv.writer(out_file). writerow(new_row)
                                  	return
                                  
                                  
                                  #
                                  #		       MAIN PROGRAM             #
                                  #
                                  #
                                  w, h = ui.get_screen_size()
                                  h -= 64
                                  bh = bw = 80  # label height and button width 
                                  mg = 10  # margin 
                                  view = ui.View(name='Show Photo', bg_color='#D98880', frame=(0, 0, w, h)) 
                                  #
                                  def photo_action(sender):
                                  	matrix = open_photo_ref()
                                  	row= matrix[0]
                                  	my_path = row[1]
                                  	v = ui.ImageView(frame=(0,0,500,200))
                                  	v.image = ui.Image.named(my_path)
                                  	v.present('sheet')
                                  	return
                                  #
                                  photo_button = ui.Button(frame=(150,10,130,0),border_color='black', border_width=2, corner_radius = 10, tint_color = 'black', background_color='#EAECEE', action=photo_action, font=('Arial Rounded MT Bold',18)) photo_button.title='show'
                                  photo_button.width=100
                                  photo_button.height=40
                                  view.add_subview(photo_button)
                                  
                                  #
                                  def pick_action(sender):
                                  	assets = photos.get_assets()
                                  	asset = photos.pick_asset(assets)
                                  	fil = str(ObjCInstance(asset).filename())
                                  	dir = str(ObjCInstance(asset).directory())
                                  	path = '/var/mobile/Media/' + dir + '/' + fil
                                  	path_string = str(path)
                                  	save_photo(path_string)
                                  	return
                                  #
                                  pick_button = ui.Button(frame=(10,10,130,0),border_color='black', border_width=2, corner_radius = 10, tint_color = 'black', background_color='#EAECEE', action=pick_action, font=('Arial Rounded MT Bold',18)) pick_button.title='pick'
                                  pick_button.width=100
                                  pick_button.height=40
                                  view.add_subview(pick_button)
                                  
                                  #########################################
                                  nav_view = ui.NavigationView(view)
                                  nav_view.present('sheet')
                                  #########################################
                                  
                                  cvp 1 Reply Last reply Reply Quote 0
                                  • cvp
                                    cvp @frankL last edited by cvp

                                    @frankL in your show photo, see the doc of ui.ImageView.content_mode and try

                                        v.content_mode = ui.CONTENT_SCALE_ASPECT_FIT
                                    

                                    And if you want you can resize our frame with the same ratio as the photo

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

                                      That worked for displaying the photo. How would I resize the frame the same as the photo?

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

                                        @frankL if you photo is, for instance 3000x4000, it is not possible to display the same frame on the iDevice, thus you have to keep the ratio, not the size, wait 5' I come back

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

                                          @frankL that should be ok

                                              v = ui.ImageView()
                                              v.content_mode = ui.CONTENT_SCALE_ASPECT_FIT
                                              v.image = ui.Image.named(my_path)
                                              w,h = v.image.size
                                              ws = 500	# example only
                                              hs = ws * h/w
                                              v.frame=(0,0,ws,hs)
                                          
                                          
                                          1 Reply Last reply Reply Quote 0
                                          • frankL
                                            frankL last edited by

                                            That doesn't seem to change anything.

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