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.


    Get all photos including hidden photos in a specific album

    Pythonista
    2
    8
    1952
    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.
    • masscode
      masscode last edited by

      Hey guys,

      I am trying to get all photos in a specific album including all hidden photos.
      But the the code below does not include hidden photos:

      for album in photos.get_albums():
      	if album.title == "album title":
      		assets = album.assets
      		break
      print(len(assets))
      

      and the code below includes all photos outside the target album, so it doesnโ€™t serve my purposes either:

      assets = photos.get_assets(media_type='image', include_hidden=True)
      print(len(assets))
      

      Any idea how I can achieve this?

      Thanks!

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

        @masscode I've tried in objectiveC but without success, sorry for you.

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

          Thank you for trying!

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

            @masscode for info, if you want to go deeper

            import photos
            import console
            from objc_util import *
            
            NSBundle.bundleWithPath_('/System/Library/Frameworks/Photos.framework').load()
            PHAssetCollection = ObjCClass('PHAssetCollection')
            PHAsset = ObjCClass('PHAsset')
            PHFetchOptions = ObjCClass('PHFetchOptions').alloc()
            
            def main():
            	
            	console.clear()		
            	PHAssetCollectionType = 1      # album
            	PHAssetCollectionSubtype = 2   # albumregular
            	album = 'Test'
            	NSPredicate = ObjCClass('NSPredicate').predicateWithFormat_argumentArray_("title = %@", [album])
            	PHFetchOptions.setPredicate_(NSPredicate)
            	PHFetchOptions.includeHiddenAssets = True
            	fetchresult = PHAssetCollection.fetchAssetCollectionsWithType_subtype_options_(PHAssetCollectionType, PHAssetCollectionSubtype, PHFetchOptions)
            	n_albums = fetchresult.count()
            	for i in range(n_albums):
            		coll = fetchresult.objectAtIndex_(i)
            		album = str(coll.localizedTitle())
            		assets = PHAsset.fetchAssetsInAssetCollection_options_(coll, None)
            		print(album)
            		for j in range(assets.count()):
            			a = assets.objectAtIndex_(j)		
            			fn = str(a.valueForKey_('filename'))
            			print(fn,a)
            
            main()	
            
            1 Reply Last reply Reply Quote 0
            • cvp
              cvp @masscode last edited by cvp

              @masscode could you try please this script

              import photos
              import console
              from objc_util import *
              
              NSBundle.bundleWithPath_('/System/Library/Frameworks/Photos.framework').load()
              PHAssetCollection = ObjCClass('PHAssetCollection')
              PHAsset = ObjCClass('PHAsset')
              PHFetchOptions = ObjCClass('PHFetchOptions').alloc()
              
              def main():
              	
              	console.clear()					
              	assets = photos.get_assets(media_type='image', include_hidden=True)
              	for asset in assets:
              		if asset.hidden:
              			ao = ObjCInstance(asset)
              			fn = str(ao.valueForKey_('filename'))
              			#print(ao)
              			PHAssetCollectionType = 1      # album
              			fetchresult = PHAssetCollection.fetchAssetCollectionsContainingAsset_withType_options_(ao, PHAssetCollectionType,None)
              			n_albums = fetchresult.count()
              			for i in range(n_albums):
              				coll = fetchresult.objectAtIndex_(i)
              				album = str(coll.localizedTitle())
              				print(f" hidden {fn} belongs to album {album}")
              
              main()	
              

              Thus your request is

              import photos
              import console
              from objc_util import *
              
              NSBundle.bundleWithPath_('/System/Library/Frameworks/Photos.framework').load()
              PHAssetCollection = ObjCClass('PHAssetCollection')
              PHAsset = ObjCClass('PHAsset')
              PHFetchOptions = ObjCClass('PHFetchOptions').alloc()
              
              def main():
              	
              	console.clear()	
              	album_assets = []
              	for album in photos.get_albums():
              		if album.title == "Test":
              			album_assets.append(album.assets)
              			break
              	print(len(album_assets))				
              	assets = photos.get_assets(media_type='image', include_hidden=True)
              	for asset in assets:
              		if asset.hidden:
              			ao = ObjCInstance(asset)
              			fn = str(ao.valueForKey_('filename'))
              			#print(ao)
              			PHAssetCollectionType = 1      # album
              			fetchresult = PHAssetCollection.fetchAssetCollectionsContainingAsset_withType_options_(ao, PHAssetCollectionType,None)
              			n_albums = fetchresult.count()
              			for i in range(n_albums):
              				coll = fetchresult.objectAtIndex_(i)
              				album = str(coll.localizedTitle())
              				print(f" hidden {fn} belongs to album {album}")
              				if album == 'Test':
              					album_assets.append(asset)
              					print(len(album_assets))				
              
              main()	
              
              1 Reply Last reply Reply Quote 0
              • cvp
                cvp @Guest last edited by

                @emma0122 ๐Ÿ‘

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

                  @cvp
                  This is beautiful!!! Thank you!!!
                  I will read up about these modules and study.

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

                    @masscode ๐Ÿ‘

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