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.
Managing photos
-
@frankL try this with a photo of which you know the name:
import ui import os path = '/var/mobile/Media/DCIM/'#108APPLE/IMG_8955.JPG' v = ui.ImageView() v.present('sheet') l = os.listdir(path) for apple in l: ll = os.listdir(path+apple) print(apple) for ff in ll: print(ff) if ff == 'IMG_8662.PNG': v.image = ui.Image.named(path+apple+'/'+ff)
-
@frankL or
import photos import ui import os assets = photos.get_assets() asset = photos.pick_asset(assets) fil = str(ObjCInstance(asset).filename()) print(fil) path = '/var/mobile/Media/DCIM/'#108APPLE/IMG_8955.JPG' v = ui.ImageView() v.present('sheet') l = os.listdir(path) for apple in l: ll = os.listdir(path+apple) print(apple) for ff in ll: print(ff) if ff == fil: v.image = ui.Image.named(path+apple+'/'+ff)
-
For the first script, how do I get the name of the photo? I ran the script as is and it printed thousands of items in the console each in the form of:
IMG_xxxx.HEIC
I assume that there is one of these for every photo in my camera roll.For the second script, after I picked a photo, I get an error message:
fil = str(ObjCInstance(asset).filename())
NameError: name 'ObjCInstance' is not defined. -
@frankL sorry, I forgot
from objc_util import *
I guess it was loaded on my Pythonista by another script I ran
-
-
With 'from objc_util import *' the second script runs and with the second and third print lines commented, I get the selected image name "IMG_1343.JPG" and the image is displayed in the subview. How do I find out which directory "/xxxAPPLE/ the image is in? When I uncommented the second print line, I get a list of about 30 directories.
-
@frankL that's all folks
import photos import ui from objc_util import * assets = photos.get_assets() asset = photos.pick_asset(assets) fil = str(ObjCInstance(asset).filename()) dir = str(ObjCInstance(asset).directory()) path = '/var/mobile/Media/' + dir + '/' + fil v = ui.ImageView() v.present('sheet') v.image = ui.Image.named(path)
-
Perfect! Thank you!
-
cvp, one more question: I have the following script in which I can pick a photo and save it to a csv file with a button push or show the previously saved photo with the other button push. The only problem is that I haven't been able to change the size or aspect ratio of the displayed photo. I tried setting the frame for the subview and that doesn't seem to work. How can I fix this? Thanks.
import photos import ui from objc_util import * import csv import os # def open_photo_ref(): matrix=[] my_path=os.path. abspath("photo_file.csv") with open(my_path,'r',encoding='utf-8') as reader: reader=csv.DictReader(reader) for row in reader: matrix.append([row['Index'], row['Path']]) return matrix # # def save_photo(photo_path): save_path='/private/var/mobile/Containers/Shared/AppGroup/99B62839-B39E-4ADF-9308-D5D986E28E91/Pythonista3/Documents/Frank/photo_file.csv' headings=['Index', 'Path'] with open(save_path, 'w', newline='',encoding='utf-8') as out_file: csv.writer(out_file).writerow(headings) new_row = [0, photo_path] csv.writer(out_file). writerow(new_row) return # # MAIN PROGRAM # # # w, h = ui.get_screen_size() h -= 64 bh = bw = 80 # label height and button width mg = 10 # margin view = ui.View(name='Show Photo', bg_color='#D98880', frame=(0, 0, w, h)) # def photo_action(sender): matrix = open_photo_ref() row= matrix[0] my_path = row[1] v = ui.ImageView(frame=(0,0,500,200)) v.image = ui.Image.named(my_path) v.present('sheet') return # photo_button = ui.Button(frame=(150,10,130,0),border_color='black', border_width=2, corner_radius = 10, tint_color = 'black', background_color='#EAECEE', action=photo_action, font=('Arial Rounded MT Bold',18)) photo_button.title='show' photo_button.width=100 photo_button.height=40 view.add_subview(photo_button) # def pick_action(sender): assets = photos.get_assets() asset = photos.pick_asset(assets) fil = str(ObjCInstance(asset).filename()) dir = str(ObjCInstance(asset).directory()) path = '/var/mobile/Media/' + dir + '/' + fil path_string = str(path) save_photo(path_string) return # pick_button = ui.Button(frame=(10,10,130,0),border_color='black', border_width=2, corner_radius = 10, tint_color = 'black', background_color='#EAECEE', action=pick_action, font=('Arial Rounded MT Bold',18)) pick_button.title='pick' pick_button.width=100 pick_button.height=40 view.add_subview(pick_button) ######################################### nav_view = ui.NavigationView(view) nav_view.present('sheet') #########################################
-
@frankL I could help but, first of all, please post your code between 2 lines of 3 backsticks or use button </> to insert your code.
Otherwise, we don't see your code indentation and can't copy it easily for testing.
-
Is this better?
import photos import ui from objc_util import * import csv import os # def open_photo_ref(): matrix=[] my_path=os.path. abspath("photo_file.csv") with open(my_path,'r',encoding='utf-8') as reader: reader=csv.DictReader(reader) for row in reader: matrix.append([row['Index'], row['Path']]) return matrix # # def save_photo(photo_path): save_path='/private/var/mobile/Containers/Shared/AppGroup/99B62839-B39E-4ADF-9308-D5D986E28E91/Pythonista3/Documents/Frank/photo_file.csv' headings=['Index', 'Path'] with open(save_path, 'w', newline='',encoding='utf-8') as out_file: csv.writer(out_file).writerow(headings) new_row = [0, photo_path] csv.writer(out_file). writerow(new_row) return # # MAIN PROGRAM # # # w, h = ui.get_screen_size() h -= 64 bh = bw = 80 # label height and button width mg = 10 # margin view = ui.View(name='Show Photo', bg_color='#D98880', frame=(0, 0, w, h)) # def photo_action(sender): matrix = open_photo_ref() row= matrix[0] my_path = row[1] v = ui.ImageView(frame=(0,0,500,200)) v.image = ui.Image.named(my_path) v.present('sheet') return # photo_button = ui.Button(frame=(150,10,130,0),border_color='black', border_width=2, corner_radius = 10, tint_color = 'black', background_color='#EAECEE', action=photo_action, font=('Arial Rounded MT Bold',18)) photo_button.title='show' photo_button.width=100 photo_button.height=40 view.add_subview(photo_button) # def pick_action(sender): assets = photos.get_assets() asset = photos.pick_asset(assets) fil = str(ObjCInstance(asset).filename()) dir = str(ObjCInstance(asset).directory()) path = '/var/mobile/Media/' + dir + '/' + fil path_string = str(path) save_photo(path_string) return # pick_button = ui.Button(frame=(10,10,130,0),border_color='black', border_width=2, corner_radius = 10, tint_color = 'black', background_color='#EAECEE', action=pick_action, font=('Arial Rounded MT Bold',18)) pick_button.title='pick' pick_button.width=100 pick_button.height=40 view.add_subview(pick_button) ######################################### nav_view = ui.NavigationView(view) nav_view.present('sheet') #########################################
-
@frankL in your show photo, see the doc of ui.ImageView.content_mode and try
v.content_mode = ui.CONTENT_SCALE_ASPECT_FIT
And if you want you can resize our frame with the same ratio as the photo
-
That worked for displaying the photo. How would I resize the frame the same as the photo?
-
@frankL if you photo is, for instance 3000x4000, it is not possible to display the same frame on the iDevice, thus you have to keep the ratio, not the size, wait 5' I come back
-
@frankL that should be ok
v = ui.ImageView() v.content_mode = ui.CONTENT_SCALE_ASPECT_FIT v.image = ui.Image.named(my_path) w,h = v.image.size ws = 500 # example only hs = ws * h/w v.frame=(0,0,ws,hs)
-
That doesn't seem to change anything.
-
@frankL said:
That doesn't seem to change anything.
True if your photo is ratio 5/2 with your frame 500,200.
What do you really want and which is the size width/height of your photo?
And did you read the doc of ui.ImageView.content_mode?
-
You're right about the photo ratio. It is doing the right thing.
I've been reading the image section in this doc:
http://omz-software.com/pythonista/docs/ios/ui.html#image
Which I just realized is the general section. But I found the Image module documentation (actually the list of all modules which I haven't seen before). I see that I have a lot to learn. Thanks for all of your help. -
@frankL be careful that there are two kinds of images:
- ui.Image which is the Pythonista version of UIImage standard iOS image
- PIL Image which comes from a Python module named PIL
The forum contains a lot of topics about tHem and their conversion in both directions.
Good luck
-
If I have the path to several photos on my iphone, for example:
/var/mobile/Media/DCIM/131APPLE/IMG_1415.HEIC
/var/mobile/Media/DCIM/131APPLE/IMG_1412.HEIC
/var/mobile/Media/DCIM/131APPLE/IMG_1389.HEIC
/var/mobile/Media/DCIM/131APPLE/IMG_1388.HEIC
/var/mobile/Media/DCIM/131APPLE/IMG_1373.JPGHow can I show these as thumbnails in a subview?