Programming questio...
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] Programming question (Python)

15 Posts
12 Users
0 Reactions
52 Views
 Earl
Posts: 1902
Free Member
Topic starter
 

Sorry to ask this but I'm a bit suck.

I'm day 3 into learning Python on my tod. My project is to create a photo gallery app that loops through a folder displaying one photo at once.  I've imported PIL and tkinter.

I'm guessing at the beginning I need to read the entire folder using a list to store all the file names.  I display the first image, then, when the user clicks the Next button, I fetch the next one from the list and so on.  Is that the correct general approach? What if I have 1000 photos in the folder?  Is that to big for a list?


 
Posted : 03/09/2018 8:09 pm
Posts: 4675
Full Member
 

I'm no expert at Python, but that sounds about right. Don't forget to consider what happens if the folder contains another folder, and another, etc. And you should be checking that the files are images, and only store the names if they are.


 
Posted : 03/09/2018 8:14 pm
Posts: 11402
Free Member
 

What if I have 1000 photos in the folder? Is that to big for a list?

a bbc micro yes a modern pc no


 
Posted : 03/09/2018 8:17 pm
 IA
Posts: 563
Free Member
 

What if I have 1000 photos in the folder?  Is that to big for a list?

Good that you're thinking of these sorts of things, but if you're new to programming (rather than just python) TBH I'd start with getting it working, then think about how to make it better.

That said, if you want the long answer.....

....1000 images? you'll be fine. But for further reading:

https://docs.python.org/3/library/os.html is the relevant reference for file system ops.

You could use something like glob ( https://docs.python.org/2/library/glob.html) to restrict to just images (consider a folder might have other files).

Or there's os.walk() to get a generator you can just loop over, and not manage the list yourself, but that's creating larger data structures and more file system queries in the large-no-of-files case (1000 isn't large!) os.listdir() is better in this case, as it's doing less per-file, generating smaller data structures as it goes.

Speaking of generating, it can get fiddly if you need generators, in which case also look at  https://docs.python.org/3.4/library/pathlib.html


 
Posted : 03/09/2018 8:26 pm
Posts: 3171
Free Member
 

The concept will be the same for any language.  You will need to store a list of all images in the folder and if you're feeling adventurous, any sub-folders.  And that would be an ideal time to explore the concept of recursion.

Storing in a list is the most efficient way as you wouldn't want to be scanning the file system when clicking next/previous and actually makes it more difficult.

If you do scan sub-folders then you would need to include the path (i.e. folder) to the file.


 
Posted : 03/09/2018 8:28 pm
Posts: 7076
Full Member
 

You're going to want to put the index of the current image into the URL, then you can just read this from the query parameters, calculate the next index, and return the relevant image.

I think you might be able to do it all with http.server and a few lines of code.

https://docs.python.org/3/library/http.server.html


 
Posted : 03/09/2018 8:51 pm
 Earl
Posts: 1902
Free Member
Topic starter
 

Thanks folks. I'm not exactly new to coding but absolutely new to this modern stuff and anything remotely oo.

Obviously I'm just Frankenstein-ing code from google/stack overflow at the moment.

I'm using list.dir()  and glob to only retrieve .jpg files.

tkinter and frames/labels is a mindf***.   I've got the first image from the folder to display but I don't have much control over the size or position at the moment.

And subfolders will be pretty far down the line.

What I'm finding hard is the reference guides for various things. e.g. If I import tkinter, how do I see what is in tkinter? I'm using IDLE which came with Python. Is there a better IDE to use for beginners?


 
Posted : 03/09/2018 9:15 pm
 btbb
Posts: 0
Free Member
 

I'm attempting to learn Python at the moment and use Visual Studio Code. There may be better IDEs but it seems to work ok for me and it's free.


 
Posted : 03/09/2018 9:47 pm
 Earl
Posts: 1902
Free Member
Topic starter
 

Cool - I'll give that a go tomorrow.


 
Posted : 03/09/2018 10:21 pm
 tor5
Posts: 0
Free Member
 

Try pycharm if you want a full featured IDE


 
Posted : 03/09/2018 11:15 pm
 poly
Posts: 8699
Free Member
 

Sublime text is better than idle.

pycharm is useful but has a price tag if I recall.

have a look at Spyder (included if you install anaconda python - but available separately) it’s the best free solution I found.

tkinter is quite a learning curve if you are both new to OO and UX frameworks.

a python list can contain over 500 million elements - although you may run into some serious performance issues building such a list from a directory!


 
Posted : 03/09/2018 11:57 pm
Posts: 959
Full Member
 

If I import tkinter, how do I see what is in tkinter?

I've not used tkinter, but in general for Python:

help(modulename) will give you all kinds of info on the module (interactive)

dir(modulename) shows you all the functions and variables defined by that module.


 
Posted : 04/09/2018 1:37 pm
Posts: 23277
Free Member
 

i use spyder mostly but then I use it for data analysis rather than the kind of application your after.

juyter notebooks are pretty handy for sketching stuff out.


 
Posted : 04/09/2018 1:45 pm
 poly
Posts: 8699
Free Member
 

If I import tkinter, how do I see what is in tkinter?

Its huge.  Rather than trying to see what is in it (if that is what you meant) then the documentation (or a tutorial) is probably best for seeing how to use it?  finding your way in tkinter with just the module help will be really hard unless you are really experienced understanding technical docs for modules.

juyter notebooks are pretty handy for sketching stuff out.

But AFAIK there is no way to use notebooks with tkinter - if that is really a plan.


 
Posted : 04/09/2018 2:23 pm
Posts: 7656
Full Member
 

 it’s the best free solution I found.

The full fat Visual Studio 2017 is free for personal use (sign up via MS dev essentials). Might be a tad overkill in many cases though. I tend to use it mostly since I am used to it from work.


 
Posted : 04/09/2018 2:27 pm
Posts: 23277
Free Member
 

But AFAIK there is no way to use notebooks with tkinter – if that is really a plan.

Never used it but if it’s python module, surely you just import it like any other module?


 
Posted : 04/09/2018 4:52 pm

6 DAYS LEFT
We are currently at 95% of our target!