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 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?
-
@frankL said:
How can I show these as thumbnails in a subview?
All photos in one view? Or I don't understand
You have the code to display one in an ImageView. If it has small frame, the photo will be a thumbnail.
-
Yes, all photos in one view. Just like you see when you use
asset = photos.pick_asset(assets) -
@frankL very quick and dirty (up to you to compute correct frame and scales)
Or you add one ImageView as subview per photo in your view
import ui v = ui.ImageView() v.frame = (0,0,400,100) with ui.ImageContext(400,100) as ctx: u = ui.Image.named('test:Lenna').draw(0,0,200,100) u = ui.Image.named('test:Mandrill').draw(200,0,200,100) v.image = ctx.get_image() v.present('sheet')
-
@frankL you can also modify this script (try it so) to support your own photos instead of photos from camera roll.
Easy modification if you have an array of your paths- self.assets = array of your paths
- photo.image = ui.image.named(your array[row])
from objc_util import * import os import photos import ui class PhotosPickerTableView(ui.View): def __init__(self,w,h,d): self.width = w self.height = h self.selected = None ok = ui.ButtonItem() ok.image = ui.Image.named('iob:ios7_checkmark_outline_32') ok.action = self.ok_action self.right_button_items = (ok,) tbl = ui.TableView() tbl.frame = (0,0,d,h) tbl.row_height = d self.assets = photos.get_assets() tbl.data_source = ui.ListDataSource(items=range(len(self.assets))) tbl.separator_color=(1,0,0,0) tbl.delegate = self tbl.data_source.tableview_cell_for_row = self.tableview_cell_for_row tbl.background_color = (0,0,0,0) self.add_subview(tbl) pha =ui.View(name='photo_area') pha.frame = (d,0,w-d,h) self.add_subview(pha) img = ui.ImageView(name='selected_photo') img.frame = (0,0,w-d,h) img.content_mode=ui.CONTENT_SCALE_ASPECT_FIT img.background_color = (0.5,0.5,0.5,1) pha.add_subview(img) self.tableview_did_select(tbl,0,0) self.photo_scale = 1 def ok_action(self,sender): self.close() def tableview_did_select(self, tableview, section, row): ui_image = self.assets[row].get_ui_image() self['photo_area']['selected_photo'].image = ui_image self.selected = row def tableview_cell_for_row(self,tableview, section, row): cell = ui.TableViewCell() v = 0.4 + (row % 2)*0.4 cell.bg_color = (v,v,v,v) selected_cell = ui.View() #selected_cell.bg_color = 'blue' selected_cell.border_width = 2 selected_cell.border_color = 'blue' cell.selected_background_view = selected_cell photo = ui.ImageView() photo.frame = (0,0,tableview.row_height,tableview.row_height) photo.image = self.assets[row].get_ui_image() photo.content_mode = ui.CONTENT_SCALE_ASPECT_FIT cell.content_view.add_subview(photo) return cell def main(): # Hide script w,h = ui.get_screen_size() mi = min(w,h)*0.9 my_back = PhotosPickerTableView(mi,mi,100) my_back.background_color='white' my_back.name = 'Photos Picker via TableView' my_back.present('sheet',hide_title_bar=False) my_back.wait_modal() # Protect against import if __name__ == '__main__': main()
-
Thank you. That is cool looking. But I don't see where to input my array of paths. When I ran the script, it pulls from my camera roll.
-
from objc_util import * import os import photos import ui class PhotosPickerTableView(ui.View): def __init__(self,w,h,d): self.width = w self.height = h self.selected = None ok = ui.ButtonItem() ok.image = ui.Image.named('iob:ios7_checkmark_outline_32') ok.action = self.ok_action self.right_button_items = (ok,) tbl = ui.TableView() tbl.frame = (0,0,d,h) tbl.row_height = d self.assets = ['test:Lenna','test:Mandrill','test:Numbers','test:Pattern','test:Peppers','test:Boat','test:Bridge','test:Sailboat'] tbl.data_source = ui.ListDataSource(items=range(len(self.assets))) tbl.separator_color=(1,0,0,0) tbl.delegate = self tbl.data_source.tableview_cell_for_row = self.tableview_cell_for_row tbl.background_color = (0,0,0,0) self.add_subview(tbl) pha =ui.View(name='photo_area') pha.frame = (d,0,w-d,h) self.add_subview(pha) img = ui.ImageView(name='selected_photo') img.frame = (0,0,w-d,h) img.content_mode=ui.CONTENT_SCALE_ASPECT_FIT img.background_color = (0.5,0.5,0.5,1) pha.add_subview(img) self.tableview_did_select(tbl,0,0) self.photo_scale = 1 def ok_action(self,sender): self.close() def tableview_did_select(self, tableview, section, row): ui_image = ui.Image.named(self.assets[row]) self['photo_area']['selected_photo'].image = ui_image self.selected = row def tableview_cell_for_row(self,tableview, section, row): cell = ui.TableViewCell() v = 0.4 + (row % 2)*0.4 cell.bg_color = (v,v,v,v) selected_cell = ui.View() #selected_cell.bg_color = 'blue' selected_cell.border_width = 2 selected_cell.border_color = 'blue' cell.selected_background_view = selected_cell photo = ui.ImageView() photo.frame = (0,0,tableview.row_height,tableview.row_height) photo.image = ui.Image.named(self.assets[row]) photo.content_mode = ui.CONTENT_SCALE_ASPECT_FIT cell.content_view.add_subview(photo) return cell def main(): # Hide script w,h = ui.get_screen_size() mi = min(w,h)*0.9 my_back = PhotosPickerTableView(mi,mi,100) my_back.background_color='white' my_back.name = 'Photos Picker via TableView' my_back.present('sheet',hide_title_bar=False) my_back.wait_modal() # Protect against import if __name__ == '__main__': main()
-
That's awesome! Thank you