Forum Archive

Sample sketch, not working?

JoeJack

I cannot use Sketch .py in samples.The moment I put on 'Save image', shut down program.
I found function:save_image(92 row in code) was in document 'Deprecated Functions' category.
I change function:save_image() to create_image_asset() instead as written in document.
But, it does not work.there are a code(from row89)and an error code:

with ui.ImageContext(self.width, self.height) as ctx:
                self.image_view.image.draw()
                img = ctx.get_image()
                photos.create_image_asset(img)
                #photos.save_image(img)
                console.hud_alert('Saved')
Traceback (most recent call last):
  File "/private/var/mobile/Containers/Shared/AppGroup/738A76EA-5C4E-4440-85BA-1FD3DC0E35E9/Pythonista3/Documents/Examples/User Interface/Sketch.py", line 95, in save_action
    photos.create_image_asset(img)
TypeError: argument 1 must be str, not _ui.Image

I am beginner, I don't know what to do,even if I think a few days.
Is any way to work this program?

cvp

@JoeJack
photos.create_image_asset is the inverse of a save, it reads a file and creates an asset.
It's parameter is thus a string containing the path of the file to read...

JoeJack

@cvp
O.k.thank you. I will look for method of how to 'save' in different way.

cvp

@JoeJack See here
How to save your img ui.Image.

    with open('test.jpg', 'wb') as f:
        quality = 0.9 # From 0.0 (more compression) to 1.0 (better quality)
        f.write(img.to_jpeg(quality))
JoeJack

@cvp
Thank you very much. I could save ui.image as jpeg image. It is very easy....I thought too much. Thanks for your help.

cvp

@JoeJack Do not thank me. Thank this wonderful forum full of pearls written by gurus and thank @omz for his invaluable application.