Forum Archive

How to use create_image_asset for downloaded image instead of save_image since it does not accept PIL image?

idchlife

I see that photos.save_image became deprecated, so I tried using create_image_asset, but it does not work with image urls or PIL image that was created by downloading url: Image.open(BytesIO(urlopen(photo_url).read()))

How .save_image could be deprecated, if not tool available to replace it?

Thanks!

cvp

@idchlife see here

idchlife

Tried it and it works! Thanks!

Any info or idea why .save_image with functionality making image from data was depreacted?
It seems unreasonable to save temporary file to app folder just to have it inside photos duplicated (of course temporary file could be deleted after)

cvp

@idchlife No idea, sorry, hoping @omz will answer to you

cvp

@idchlife Try this

# img is a PIL image
with tempfile.NamedTemporaryFile(suffix='.jpg') as temp:
    img.save(temp.name, format="JPEG")
    photos.create_image_asset(temp.name)
idchlife

Thank you! This is very helpful!

cvp

@idchlife read this and it's important paragraph
I think the save of a pil or ui. Image is deprecated because in this case, you only save the image, not its metadata, as described in Apple doc

cvp

@idchlife If you really want to create a photo from an ui.Image without passing via a temporary file, you can use Objective-C but it it is not a nice code 😂

# only to have an ui.Image
import ui
img = ui.Image.named('test:Bridge')

# Create a PHAsset from an ui.Image (not from a PIL Image)
from objc_util import *
import threading

NSBundle.bundleWithPath_('/System/Library/Frameworks/Photos.framework').load()
PHPhotoLibrary = ObjCClass('PHPhotoLibrary')
PHAssetChangeRequest = ObjCClass('PHAssetChangeRequest')

lib = PHPhotoLibrary.sharedPhotoLibrary()
def change_block():
    req = PHAssetChangeRequest.creationRequestForAssetFromImage_(img)
def perform_changes():
    lib.performChangesAndWait_error_(change_block, None)

t = threading.Thread(target=perform_changes)
t.start()
t.join()