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.
PIL.Image in ListDataSource?
-
I’m making a TableView with ListDataSource. Some of the items will have a named image, and some will not. In order to keep the item titles lined up neatly, I’m trying to give the items without images a default transparent image. However, it doesn’t seem to be working.
Here’s roughly what the relevant piece of my code looks like:
# item_list is a list of everything to go on the table # data_list is a dictionary that will go to ListDataSource for item in item_list: if check_for_star(item): img = ui.Image.named('iob:ios7_star_outline_24') else: img = PIL.Image.new('RGBA',(24,24), (255, 255, 255, 0)) data_list.append({'title' : item, 'image' : img}) view['tableview'].data_source = ListDataSource(data_list)
The TableView displays as if the PIL images are
None
. I thought maybe it needs to be a ui.Image instead, so I tried doing this:img = ui.Image.from_data(PIL.Image.new('RGBA',(24,24), (255, 255, 255, 0)).toString())
The
img
variable is still treated likeNone
. Am I missing something? I can useimg.show()
with the PIL images and they look fine.In case this helps, here’s an example of what the output looks like. I want all of the title text to line up neatly.
-
Per the code/doocs, the image field should either be a string (which is used as the argument to ui.Image.named, or else a ui.Image.
def tableview_cell_for_row(self, tv, section, row): item = self.items[row] cell = TableViewCell() cell.text_label.number_of_lines = self.number_of_lines if isinstance(item, dict): cell.text_label.text = item.get('title', '') img = item.get('image', None) if img: if isinstance(img, basestring): cell.image_view.image = Image.named(img) elif isinstance(img, Image): cell.image_view.image = img accessory = item.get('accessory_type', 'none') cell.accessory_type = accessory
You should take a look at the items you are sending to ListDataSource to verify that it is a list of dicts, which have a key named
image
, which contains a string or ui.Image.If not, check that your star flag is being properly set.
If you want more control, you may need to implement your own datasource that uses content_view
-
Thanks, that makes sense. I might just try making my own datasource then.
I was confused because in the documentation on ListDataSource.items, the link to the Image object goes to the PIL.Image module, not ui.Image. That’s what prompted me to try a PIL image in the first place. http://omz-software.com/pythonista/docs/ios/ui.html#ui.ListDataSource.items
-
yeah, that is an error in the docs -- everything in UI that refers to images are ui.Image, not PIL.Image.
The ListDataSource should work, in theory, though it may or may not put the image where you would like.