seems just as fast for small directories, but saving to files makes startup much quicker for large image collections; saving may be needed in some apps (Web pages) """ import os, sys from PIL import Image from tkinter import Tk import viewer_thumbs def makeThumbs(imgdir, size=(100, 100), subdir='thumbs'): """ create thumbs in memory but don't cache files """ thumbs = [] for imgfile in os.listdir(imgdir): imgpath = os.path.join(imgdir, imgfile) try: imgobj = Image.open(imgpath) imgobj.thumbnail(size) thumbs.append((imgfile, imgobj)) except: print("Skipping: ", imgpath) return thumbs if __name__ == '__main__': imgdir = (len(sys.argv) > 1 and sys.argv[1]) or 'images' viewer_thumbs.makeThumbs = makeThumbs main, save = viewer_thumbs.viewer(imgdir, kind=Tk) main.mainloop()
same, but make thumb images in memory without saving to or loading from files: seems just as fast for small directories, but saving to files makes startup much quicker for large image collections; saving may be needed in some apps (web pages) """ import os, sys from PIL import Image from Tkinter import Tk import viewer_thumbs def makeThumbs(imgdir, size=(100, 100), subdir='thumbs'): """ create thumbs in memory but don't cache to files """ thumbs = [] for imgfile in os.listdir(imgdir): imgpath = os.path.join(imgdir, imgfile) try: imgobj = Image.open(imgpath) # make new thumb imgobj.thumbnail(size) thumbs.append((imgfile, imgobj)) except: print("Skipping: ", imgpath) return thumbs if __name__ == '__main__': imgdir = (len(sys.argv) > 1 and sys.argv[1]) or 'images' viewer_thumbs.makeThumbs = makeThumbs main, save = viewer_thumbs.viewer(imgdir, kind=Tk) main.mainloop()
""" import os import sys from tkinter import Tk from PIL import Image import viewer_thumbs def makeThumbs(imgdir, size=(100, 100)): """ create thumbs in memory but don't cache to files """ thumbs = [] for imgfile in os.listdir(imgdir): imgpath = os.path.join(imgdir, imgfile) try: imgobj = Image.open(imgpath) # make new thumb imgobj.thumbnail(size) thumbs.append((imgfile, imgobj)) except: print("Skipping: ", imgpath) return thumbs if __name__ == '__main__': IMGDIR = (len(sys.argv) > 1 and sys.argv[1]) or 'images' viewer_thumbs.makeThumbs = makeThumbs main, save = viewer_thumbs.viewer(IMGDIR, kind=Tk) main.mainloop()