Forum Archive

Get all photos including hidden photos in a specific album

masscode

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

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

masscode

Thank you for trying!

cvp

@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()  
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()  
cvp

@emma0122 ๐Ÿ‘

masscode

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

cvp

@masscode ๐Ÿ‘