Viewing 16 posts - 1 through 16 (of 16 total)
  • Programming question (Python)
  • Earl
    Free Member

    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?

    richmars
    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.

    Klunk
    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

    IA
    Full 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

    GlennQuagmire
    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.

    oldnpastit
    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

    Earl
    Free Member

    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?

    btbb
    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.

    Earl
    Free Member

    Cool – I’ll give that a go tomorrow.

    tor5
    Free Member

    Try pycharm if you want a full featured IDE

    poly
    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!

    GHill
    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.

    jam-bo
    Full 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.

    poly
    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.

    dissonance
    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.

    jam-bo
    Full 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?

Viewing 16 posts - 1 through 16 (of 16 total)

The topic ‘Programming question (Python)’ is closed to new replies.