1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
   | #!/usr/bin/env python
# -*- encoding: utf-8 -*-
 
from tkinter import *
import os
from PIL import Image  # $ pip install pillow
from PIL import ImageTk
 
#comment = true
 
#ouvrir est la photo selectionné au début
#de la on va dans son dossier et on liste les .jpg	
 
class Rendu_image(object):
	"""le process des images"""
	def __init__(self,master,list_photos,premiere,w,h):
		self.top = master
		self.files = list_photos
		self.index = premiere
		filename = self.files[self.index]
		self.w = w
		self.h = h
		Rendu_image.show_image(self,filename,w,h)
 
	def show_image(self,filename,w,h):
		im = Image.open(filename)
 
		size = im.size
		print(filename,size)
		if size[0] > size[1]:
			ratio = size[0] / size[1]
			image_h = int(size[1] * (self.h / size[1]))
			image_w = int(image_h * ratio)
		else:
			ratio = size[1] / size[0]
			image_w = int(size[0] * (self.w / size[0]))
			image_h = int(image_w * ratio)
		print(image_w,image_h,ratio, image_w/image_h)
		image_resized = im.resize((image_w, image_h), Image.ANTIALIAS)
		tk_image = ImageTk.PhotoImage(image_resized)
		label = Label(root, image=tk_image, width=w, height=h)
		label.image = tk_image
		label.pack(fill=BOTH, expand=1)
 
 
	def next_photo(self,*args):
		self.index += 1
		if self.index == len(self.files):
			self.index = 0
		filename = self.files[self.index]
		Rendu_image.show_image(self,filename,self.w,self.h)
 
 
#pour lister les photos dans le dossier
def get_pictures(ouvrir):
	destination = os.path.dirname(ouvrir) + "/"
	os.chdir(destination)  # go to "path"
	photo = []
	suffix = (".jpg",".jpeg",".png",".gif")   #prise en charge de formats.
	for filename in os.listdir():
		if filename.lower().endswith(suffix):
			photo.append(filename)
			photo.sort()
	return photo
 
 
 
w,h = 800,550
ouvrir = input()       #choix de la photo à ouvrir(et du dossier)
list_photos = get_pictures(ouvrir)   #liste photos(.jpg pour le moment)
ouverte =  os.path.basename(ouvrir)  #la choisie pour commencer par
premiere = list_photos.index(ouverte)
print(list_photos)
print(premiere)
 
 
root = Tk()
root.title("Title")
root.geometry("800x550")
root.configure(background="black")
image = PhotoImage(ouverte)
images = Label(root,image=image, bd=0)
images.pack()
 
e=Rendu_image(root,list_photos,premiere,w,h)
root.bind('<Right>', e.next_photo)
root.mainloop() |