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.
Help finding photos in album
-
Searched across the forum and Google but no luck finding out how to do this.
Is it possible to find all photos in an iOS album or run through the camera roll saying which albums the photos are tagged in?
I'm trying to find photos not tagged in an album and those tagged to a specific album.
Thanks for any pointers
-
As it is possible with Worlflow app, I think that it should also be possible with Pythonista using objc_util...
-
I'm busy to try, I'm optimist 😊
-
I'm really a beginner, surely using objc_util but even in Python(ista), thus don't accept my code as the bible, it's only a draft to show you how to use the Photos framework of Apple.
The first part uses standard Pythonista module "photos" to get all photos of th camera roll, only the "non deleted" photos.
This first part gets the metadata (or exif of the photos) and creates a dictionary: filename->(index,blank album).
There is a problem if you have photos with identic file name (ex: a jpg downloaded twice from Dropbox), problem not solved here.
The 2nd part uses the Photos Framework of Apple. The fetchAssetCollectionsWithType_subtype_options_ gets sets of collections. Please read https://developer.apple.com/library/ios/documentation/Photos/Reference/PHAssetCollection_Class/#//apple_ref/c/tdef/PHAssetCollectionType to try runs with different type and subtype. You can even reach deleted photos if wanted. In this case, each collection is an album.
The name of the album is given by localizedTitle.
And fetchAssetsInAssetCollection_options_ gets for each collection (album) its assets (photos). The name of the photo is given by valueForKey_('filename').
We replace the blank album name by its real name in the dictionary.Of course, this code does not display the photos.
Hoping this will help you.# coding: utf-8 import ui import photos import console from objc_util import * PHAssetCollection = ObjCClass('PHAssetCollection') PHAsset = ObjCClass('PHAsset') def main(): console.clear() # Build dictionary filename: index fn_index = {} for ip in range(photos.get_count()): m = photos.get_metadata(ip) #print m fn = m.get('filename') fn_index[fn] = (ip,'') # - type = 1: album # - subtype = 2: regular album result = PHAssetCollection.fetchAssetCollectionsWithType_subtype_options_(1,2, None) #print result for i in range(result.count()): coll = result.objectAtIndex_(i) album = str(coll.localizedTitle()) assets = PHAsset.fetchAssetsInAssetCollection_options_(coll, None) for j in range(assets.count()): a = assets.objectAtIndex_(j) fn = str(a.valueForKey_('filename')) tuple = fn_index[fn] ip = tuple[0] fn_index[fn] = (ip,album) # print filename -> album,index print fn_index main()
-
@cvp thanks for this. I got an error but found another of your posts with phassetcollection to add which fixed that. This gives me what I need to work this out now so thanks for your help.
-
fn_index = {photos.get_metadata(i)['filename']: (i, '') for i in range(photos.get_count())}
-
Thanks @ccc
As Pythonista is an interpreter, is this short code quicker than mine?
Anyway, thanks to help me for learning every day... -
@wcross Hello, happy to help!
Could you tell me how you have been obliged to modify my short code because I just run it without any error, but I only have a few photos. -
I had to add the line:
NSBundle.bundleWithPath_('/System/Library/Frameworks/Photos.framework').load()
Which I found at https://forum.omz-software.com/topic/3037/script-to-delete-selected-photos
You write nice code, you should get a free account on GitHub so you can share it more broadly.
-
@ccc Without this line, my code runs without error, but perhaps a previous run has executed it.
is it possible that this "line" remains active once it has been executed?Very kind with your "nice code" but I don't think it's nice, that's only draft of different tests.
I'll never dare share this kind of code because, for me, it is really not perfect. In the past, I've written a lot of applications during my (too) long life, in Fortran, Assembler, Cobol, APL, PL/1, Basic, CBasic, Basic, Delphi on a lot of (IBM only) platforms, but with a lot of "error interception" code to make them "as perfect as possible", and I don't want anymore to spend time to with this kind of code.
Anyway, since I'm retired, Pythonista is the most rentable purchase I did: 10€ for a ton hours of pleasure. -
If I remove/restart Pythonista app, my short code runs with error like you, thus this missing line remains active.
If I add this line, the run is ok.
I'm sure I'll learn everyday -
-
@ccc Thanks for your advice, I did subscribe to GitHub and uploaded this short script...
Hoping it'll help someone.
See IdentifyAlbumOfPhotos.py