python pyphoto图片查看器

image.png

"""
############################################################################
PyPhoto 1.1: thumbnail image viewer with resizing and saves.

Supports multiple image directory thumb windows - the initial img dir
is passed in as cmd arg, uses "images" default, or is selected via main
window button; later directories are opened by pressing "D" in image view
or thumbnail windows.

Viewer also scrolls popped-up images that are too large for the screen;
still to do: (1) rearrange thumbnails when window resized, based on current
window size; (2) [DONE] option to resize images to fit current window size?
(3) avoid scrolls if image size is less than window max size: use Label
if imgwide <= scrwide and imghigh <= scrhigh?

New in 1.1: updated to run in Python 3.1 and latest PIL;

New in 1.0: now does a form of (2) above: image is resized to one of the
display's dimensions if clicked, and zoomed in or out in 10% increments
on key presses; generalize me; caveat: seems to lose quality, pixels
after many resizes (this is probably a limitation of PIL)

The following scaler adapted from PIL's thumbnail code is similar to the
screen height scaler here, but only shrinks:
x, y = imgwide, imghigh
if x > scrwide: y = max(y * scrwide // x, 1); x = scrwide
if y > scrhigh: x = max(x * scrhigh // y, 1); y = scrhigh
############################################################################
"""

import sys, math, os
from tkinter import *
from tkinter.filedialog import SaveAs, Directory

from PIL import Image # PIL Image: also in tkinter
from PIL.ImageTk import PhotoImage # PIL photo widget replacement

def makeThumbs(imgdir, size=(100, 100), subdir='thumbs'):
"""
get thumbnail images for all images in a directory; for each image, create
and save a new thumb, or load and return an existing thumb; makes thumb
dir if needed; returns a list of (image filename, thumb image object);
caller can also run listdir on thumb dir to load; on bad file types may
raise IOError, or other; caveat: could also check file timestamps;
"""
thumbdir = os.path.join(imgdir, subdir)
if not os.path.exists(thumbdir):
os.mkdir(thumbdir)

thumbs = []
for imgfile in os.listdir(imgdir):
    thumbpath = os.path.join(thumbdir, imgfile)
    if os.path.exists(thumbpath):
        thumbobj = Image.open(thumbpath)            # use already created
        thumbs.append((imgfile, thumbobj))
    else:
        print('making', thumbpath)
        imgpath = os.path.join(imgdir, imgfile)
        try:
            imgobj = Image.open(imgpath)            # make new thumb
            imgobj.thumbnail(size, Image.ANTIALIAS) # best downsize filter
            imgobj.save(thumbpath)                  # type via ext or passed
            thumbs.append((imgfile, imgobj))
        except:                                     # not always IOError
            print("Skipping: ", imgpath)
return thumbs

class ViewOne(Toplevel):
"""
open a single image in a pop-up window when created; photoimage
object must be saved: images are erased if object is reclaimed;
"""
def init(self, imgdir, imgfile):
Toplevel.init(self)
self.title(imgfile)
imgpath = os.path.join(imgdir, imgfile)
imgobj = PhotoImage(file=imgpath)
Label(self, image=imgobj).pack()
print(imgpath, imgobj.width(), imgobj.height()) # size in pixels
self.savephoto = imgobj # keep reference on me

def viewer(imgdir, kind=Toplevel, cols=None):
"""
make thumb links window for an image directory: one thumb button per image;
use kind=Tk to show in main app window, or Frame container (pack); imgfile
differs per loop: must save with a default; photoimage objs must be saved:
erased if reclaimed; packed row frames (versus grids, fixed-sizes, canvas);
"""
win = kind()
win.title('Viewer: ' + imgdir)
quit = Button(win, text='Quit', command=win.quit, bg='beige') # pack first
quit.pack(fill=X, side=BOTTOM) # so clip last
thumbs = makeThumbs(imgdir)
if not cols:
cols = int(math.ceil(math.sqrt(len(thumbs)))) # fixed or N x N

savephotos = []
while thumbs:
    thumbsrow, thumbs = thumbs[:cols], thumbs[cols:]
    row = Frame(win)
    row.pack(fill=BOTH)
    for (imgfile, imgobj) in thumbsrow:
        photo   = PhotoImage(imgobj)
        link    = Button(row, image=photo)
        handler = lambda savefile=imgfile: ViewOne(imgdir, savefile)
        link.config(command=handler)
        link.pack(side=LEFT, expand=YES)
        savephotos.append(photo)
return win, savephotos

remember last dirs across all windows

saveDialog = SaveAs(title='Save As (filename gives image type)')
openDialog = Directory(title='Select Image Directory To Open')

trace = print # or lambda *x: None
appname = 'PyPhoto 1.1: '

class ScrolledCanvas(Canvas):
"""
a canvas in a container that automatically makes
vertical and horizontal scroll bars for itself
"""
def init(self, container):
Canvas.init(self, container)
self.config(borderwidth=0)
vbar = Scrollbar(container)
hbar = Scrollbar(container, orient='horizontal')

    vbar.pack(side=RIGHT,  fill=Y)                 # pack canvas after bars
    hbar.pack(side=BOTTOM, fill=X)                 # so clipped first
    self.pack(side=TOP, fill=BOTH, expand=YES)

    vbar.config(command=self.yview)                # call on scroll move
    hbar.config(command=self.xview)
    self.config(yscrollcommand=vbar.set)           # call on canvas move
    self.config(xscrollcommand=hbar.set)

class ViewOne(Toplevel):
"""
open a single image in a pop-up window when created;
a class because photoimage obj must be saved, else
erased if reclaimed; scroll if too big for display;
on mouse clicks, resizes to window's height or width:
stretches or shrinks; on I/O keypress, zooms in/out;
both resizing schemes maintain original aspect ratio;
code is factored to avoid redundancy here as possible;
"""
def init(self, imgdir, imgfile, forcesize=()):
Toplevel.init(self)
helptxt = '(click L/R or press I/O to resize, S to save, D to open)'
self.title(appname + imgfile + ' ' + helptxt)
imgpath = os.path.join(imgdir, imgfile)
imgpil = Image.open(imgpath)
self.canvas = ScrolledCanvas(self)
self.drawImage(imgpil, forcesize)
self.canvas.bind('', self.onSizeToDisplayHeight)
self.canvas.bind('', self.onSizeToDisplayWidth)
self.bind('', self.onZoomIn)
self.bind('', self.onZoomOut)
self.bind('', self.onSaveImage)
self.bind('', onDirectoryOpen)
self.focus()

def drawImage(self, imgpil, forcesize=()):
    imgtk = PhotoImage(image=imgpil)                 # not file=imgpath
    scrwide, scrhigh = forcesize or self.maxsize()   # wm screen size x,y
    imgwide  = imgtk.width()                         # size in pixels
    imghigh  = imgtk.height()                        # same as imgpil.size

    fullsize = (0, 0, imgwide, imghigh)              # scrollable
    viewwide = min(imgwide, scrwide)                 # viewable
    viewhigh = min(imghigh, scrhigh)

    canvas = self.canvas
    canvas.delete('all')                             # clear prior photo
    canvas.config(height=viewhigh, width=viewwide)   # viewable window size
    canvas.config(scrollregion=fullsize)             # scrollable area size
    canvas.create_image(0, 0, image=imgtk, anchor=NW)

    if imgwide <= scrwide and imghigh <= scrhigh:    # too big for display?
        self.state('normal')                         # no: win size per img
    elif sys.platform[:3] == 'win':                  # do windows fullscreen
        self.state('zoomed')                         # others use geometry()
    self.saveimage = imgpil
    self.savephoto = imgtk                           # keep reference on me
    trace((scrwide, scrhigh), imgpil.size)

def sizeToDisplaySide(self, scaler):
    # resize to fill one side of the display
    imgpil = self.saveimage
    scrwide, scrhigh = self.maxsize()                 # wm screen size x,y
    imgwide, imghigh = imgpil.size                    # img size in pixels
    newwide, newhigh = scaler(scrwide, scrhigh, imgwide, imghigh)
    if (newwide * newhigh < imgwide * imghigh):
        filter = Image.ANTIALIAS                      # shrink: antialias
    else:                                             # grow: bicub sharper
        filter = Image.BICUBIC
    imgnew  = imgpil.resize((newwide, newhigh), filter)
    self.drawImage(imgnew)

def onSizeToDisplayHeight(self, event):
    def scaleHigh(scrwide, scrhigh, imgwide, imghigh):
        newhigh = scrhigh
        newwide = int(scrhigh * (imgwide / imghigh))        # 3.x true div
        return (newwide, newhigh)                           # proportional
    self.sizeToDisplaySide(scaleHigh)

def onSizeToDisplayWidth(self, event):
    def scaleWide(scrwide, scrhigh, imgwide, imghigh):
        newwide = scrwide
        newhigh = int(scrwide * (imghigh / imgwide))        # 3.x true div
        return (newwide, newhigh)
    self.sizeToDisplaySide(scaleWide)

def zoom(self, factor):
    # zoom in or out in increments
    imgpil = self.saveimage
    wide, high = imgpil.size
    if factor < 1.0:                     # antialias best if shrink
        filter = Image.ANTIALIAS         # also nearest, bilinear
    else:
        filter = Image.BICUBIC
    new = imgpil.resize((int(wide * factor), int(high * factor)), filter)
    self.drawImage(new)

def onZoomIn(self, event, incr=.10):
    self.zoom(1.0 + incr)

def onZoomOut(self, event, decr=.10):
    self.zoom(1.0 - decr)

def onSaveImage(self, event):
    # save current image state to file
    filename = saveDialog.show()
    if filename:
       self.saveimage.save(filename)

def onDirectoryOpen(event):
"""
open a new image directory in new pop up
available in both thumb and img windows
"""
dirname = openDialog.show()
if dirname:
viewThumbs(dirname, kind=Toplevel)

def viewThumbs(imgdir, kind=Toplevel, numcols=None, height=400, width=500):
"""
make main or pop-up thumbnail buttons window;
uses fixed-size buttons, scrollable canvas;
sets scrollable (full) size, and places
thumbs at abs x,y coordinates in canvas;
no longer assumes all thumbs are same size:
gets max of all (x,y), some may be smaller;
"""
win = kind()
helptxt = '(press D to open other)'
win.title(appname + imgdir + ' ' + helptxt)
quit = Button(win, text='Quit', command=win.quit, bg='beige')
quit.pack(side=BOTTOM, fill=X)
canvas = ScrolledCanvas(win)
canvas.config(height=height, width=width) # init viewable window size
# changes if user resizes
thumbs = makeThumbs(imgdir) # [(imgfile, imgobj)]
numthumbs = len(thumbs)
if not numcols:
numcols = int(math.ceil(math.sqrt(numthumbs))) # fixed or N x N
numrows = int(math.ceil(numthumbs / numcols)) # 3.x true div

# max w|h: thumb=(name, obj), thumb.size=(width, height)
linksize = max(max(thumb[1].size) for thumb in thumbs)
trace(linksize)
fullsize = (0, 0,                                   # upper left  X,Y
    (linksize * numcols), (linksize * numrows) )    # lower right X,Y
canvas.config(scrollregion=fullsize)                # scrollable area size

rowpos = 0
savephotos = []
while thumbs:
    thumbsrow, thumbs = thumbs[:numcols], thumbs[numcols:]
    colpos = 0
    for (imgfile, imgobj) in thumbsrow:
        photo   = PhotoImage(imgobj)
        link    = Button(canvas, image=photo)
        def handler(savefile=imgfile):
            ViewOne(imgdir, savefile)
        link.config(command=handler, width=linksize, height=linksize)
        link.pack(side=LEFT, expand=YES)
        canvas.create_window(colpos, rowpos, anchor=NW,
                window=link, width=linksize, height=linksize)
        colpos += linksize
        savephotos.append(photo)
    rowpos += linksize
win.bind('', onDirectoryOpen)
win.savephotos = savephotos
return win

if name == 'main':
"""
open dir = default or cmdline arg
else show simple window to select
"""
imgdir = 'images'
if len(sys.argv) > 1: imgdir = sys.argv[1]
if os.path.exists(imgdir):
mainwin = viewThumbs(imgdir, kind=Tk)
else:
mainwin = Tk()
mainwin.title(appname + 'Open')
handler = lambda: onDirectoryOpen(None)
Button(mainwin, text='Open Image Directory', command=handler).pack()
mainwin.mainloop()

你可能感兴趣的:(python pyphoto图片查看器)