我已经有了我的代码主体,我可以在其中创建一个相册,然后直接从Pythonista拍摄一张照片。在那之后,我想把这张最近拍摄的照片转移到我刚刚创建的相册中。这就是我所拥有的:
import photos
import console
console.clear()
nom_album = contacts
photos.create_album(nom_album)
img=photos.capture_image()
发布于 2021-06-12 02:41:26
photos.create_album
方法返回一个Asset-Collection
对象。Asset-Collections有一个名为add_assets
的方法,该方法获取一个“资产”列表,即照片。具体地说,正如我所读到的,资产就是已经在iOS设备的照片库中的照片。若要将照片添加到相册,该照片必须已在设备的照片库中。
但是,capture_image
方法不返回资源对象。也就是说,它不会自动将新照片添加到设备的照片库中。你可以在你自己的代码中验证这一点:使用该代码拍摄的图像不应该出现在你设备的“recents”相册中。
取而代之的是,capture_image
返回一个PIL图像。我看不到任何方法可以将PIL图像直接添加到设备的图片库。我能够做的是在本地保存PIL图像,然后将保存的文件转换为Asset。也就是说,(1)使用create_image_asset
将保存的文件添加到设备的照片库中;然后(2)然后可以将该资产添加到资产集合。
下面是一个例子:
import photos
#a test album in the device’s library
#note that multiple runs of this script will create multiple albums with this name
testAlbum = 'Pythonista Test'
#the filename to save the test photograph to in Pythonista
testFile = testAlbum + '.jpg'
#create an album in the device’s photo library
newAlbum = photos.create_album(testAlbum)
#take a photo using the device’s camera
newImage = photos.capture_image()
#save that photo to a file in Pythonista
newImage.save(testFile)
#add that newly-created file to the device’s photo library
newAsset = photos.create_image_asset(testFile)
#add that newly-created library item to the previously-created album
newAlbum.add_assets([newAsset])
如果不想在Pythonista安装中保留该文件,可以使用os.remove
删除它。
import os
os.remove(testFile)
虽然首先将PIL图像保存到本地文件,然后将文件添加到设备的库中似乎是将PIL图像添加到设备的库中的一种复杂方法,但这似乎是完成此操作的预期方法。在Photo Library Access on iOS中,文档说:
要将新的图像资源添加到照片库中,请使用create_image_asset()函数,提供先前保存到磁盘的图像文件的路径。
https://stackoverflow.com/questions/67443151
复制相似问题