本文整理汇总了Python中PIL.ImageTk.PhotoImage类的典型用法代码示例。如果您正苦于以下问题:Python PhotoImage类的具体用法?Python PhotoImage怎么用?Python PhotoImage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PhotoImage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_next
def load_next(self):
try:
img = Image.open(self.paths[self.load_pos])
resize(img, self.thumb_h, self.thumb_w)
photo = PhotoImage(img)
col, row = self.column, self.row
self.photos.append(photo)
x, y = self.calculate_pos(self.column, self.row)
photo.cid = self._canvas.create_image(x, y, image=photo)
photo.index = self.load_pos
self._canvas.tag_bind(photo.cid, '<Button-1>', lambda e: self.button_callback(e, photo, col, row))
self._canvas.tag_bind(photo.cid, '<Double-Button-1>', self.activate)
self._canvas.addtag_withtag('photo', photo.cid)
self.column += 1
self.loaded += 1
except OSError:
logger.info('Could not open %s', self.paths[self.load_pos])
self.load_pos += 1
except Image.DecompressionBombWarning:
self.load_pos += 1
if self.column >= self.max_columns:
self.row += 1
self.column = 0
if self.load_pos < len(self.paths) - 1 and self.loaded < self.LIMIT:
self.load_pos += 1
self.widget.after_idle(self.load_next)
elif self.loaded >= self.LIMIT:
self.loaded = 0
x, y = self.calculate_pos(self.column, self.row)
button = Button(self._canvas, text='Load More', command=self.continue_loading)
cid = self._canvas.create_window(x, y, window=button)
self._canvas.addtag_withtag('loadbutton', cid)
self._canvas['scrollregion'] = self._canvas.bbox('all')
示例2: update
def update(self, model):
result = set()
all_prey = model.find(lambda item:isinstance(item, Prey))
for item in all_prey:
if self.contains(item):
result.add(item)
self._counter += 1
closest = self.sightDistance
existing = all_prey - result
for item in existing:
if self.distance(item.get_location()) < closest:
closest = self.distance(item.get_location())
self.set_angle(math.atan2(item.get_location()[1] - self.get_location()[1], item.get_location()[0] - self.get_location()[0]))
if len(result) > 0:
self.set_dimension(self.get_dimension()[0] + len(result), self.get_dimension()[1] + len(result))
self._image = PhotoImage(Image.open('deathstar.gif').convert('RGBA').resize((self.get_dimension()[0], self.get_dimension()[1]), Image.ANTIALIAS))
self.radius = (self._image.height()/2)+((self._image.width()**2)/(8 * self._image.height()))
self._counter = 1
if self._counter == 30:
self.radius -= 1
self.set_dimension(self.get_dimension()[0] - 1, self.get_dimension()[1] - 1)
self._image = PhotoImage(Image.open('deathstar.gif').convert('RGBA').resize((self.get_dimension()[0], self.get_dimension()[1]), Image.ANTIALIAS))
self.radius = (self._image.height()/2)+((self._image.width()**2)/(8 * self._image.height()))
self._counter = 1
self.move()
if self.get_dimension() < (Hunter.death,Hunter.death):
result.add(self)
return result
示例3: update
def update(self, model):
self._explosion = model.world()[1]/2
all_swallowed = True
if self.get_dimension() >= model.world():
model.big_bang = None
elif self.get_dimension() >= (self._explosion,self._explosion):
total = model.world()
for item in model.items:
item.set_location(random.uniform(1,total[0] - 1),random.uniform(1,total[1] - 1))
self.set_dimension(total[0], total[1])
else:
for item in model.items:
location = item.get_location()
location = (int(location[0]),int(location[1]))
if location != self._center:
all_swallowed = False
if location[0] < self._center[0]:
new_x = location[0] + 1
elif location[0] > self._center[0]:
new_x = location[0] - 1
else:
new_x = location[0]
if location[1] < self._center[1]:
new_y = location[1] + 1
elif location[1] > self._center[1]:
new_y = location[1] - 1
else:
new_y = location[1]
item.set_location(new_x,new_y)
else:
self.change_dimension(1, 1)
self._image = PhotoImage(Image.open('bigbang.gif').convert('RGBA').resize((self.get_dimension()[0], self.get_dimension()[1]), Image.ANTIALIAS))
if all_swallowed:
self.change_dimension(10, 10)
self._image = PhotoImage(Image.open('bigbang.gif').convert('RGBA').resize((self.get_dimension()[0], self.get_dimension()[1]), Image.ANTIALIAS))
示例4: make_canvas
def make_canvas(self):
"""
Criação do canvas para edição da máscara de correção
"""
h = Scrollbar(self.root, orient=HORIZONTAL)
v = Scrollbar(self.root, orient=VERTICAL)
self.canvas = Canvas(self.root, scrollregion=(0, 0, 1000, 1000), yscrollcommand=v.set, xscrollcommand=h.set)
h['command'] = self.canvas.xview
v['command'] = self.canvas.yview
self.canvas.grid(column=0, row=0, sticky=(N,W,E,S))
h.grid(column=0, row=1, sticky=(W,E))
v.grid(column=1, row=0, sticky=(N,S))
self.root.grid_columnconfigure(0, weight=1)
self.root.grid_rowconfigure(0, weight=1)
if self.image:
imgtk = PhotoImage(image=Image.open(self.image)) # not file=imgpath
imgwide = imgtk.width() # size in pixels
imghigh = imgtk.height() # same as imgpil.size
fullsize = (0, 0, imgwide, imghigh) # scrollable
self.canvas.delete('all') # clear prior photo
self.canvas.config(height=imgwide, width=imghigh) # viewable window size
self.canvas.config(scrollregion=fullsize) # scrollable area size
self.imageid=self.canvas.create_image(0, 0, image=imgtk, anchor=NW)
self.images.append(imgtk)
self.canvas.bind("<Button-1>", self.xy)
self.canvas.bind("<B1-Motion>", self.add_rectangle)
self.canvas.bind("<B1-ButtonRelease>", self.done_stroke)
self.canvas.bind_all('<Button-2>', self.select)
self.canvas.bind_all('<B2-Motion>', self.on_drag)
self.canvas.bind_all("<B2-ButtonRelease>", self.update_item)
self.canvas.bind('<Button-3>', self.config_item)
self.canvas.bind_all('<Delete>',self.delete_item)
self.canvas.focus()
示例5: drawImage
def drawImage(self, imgpil, forcesize=()):
"""
将图片绘制在窗体的canvas中
"""
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#否则若为windows系统则使窗体全屏
self.state('zoomed') # others use geometry()
self.saveimage = imgpil #PIL.Image类型
self.savephoto = imgtk # keep reference on me #PIL.ImageTk.PhotoImage,保持图片引用
trace((scrwide, scrhigh), imgpil.size)
示例6: pil_image_dir
def pil_image_dir():
from PIL.ImageTk import PhotoImage
imgdir = 'images'
if len(sys.argv) > 1:
imgdir = sys.argv[1]
imgfiles = os.listdir(imgdir)
main = Tk()
main.title('Image Viewer')
quit = Button(
main, text='Quit all', command=main.quit, font=('courier', 25))
quit.pack()
savephotos = []
for imgfile in imgfiles:
imgpath = os.path.join(imgdir, imgfile)
win = Toplevel()
win.title(imgfile)
try:
imgobj = PhotoImage(file=imgpath)
Label(win, image=imgobj).pack()
print(imgpath, imgobj.width(), imgobj.height())
savephotos.append(imgobj)
except Exception:
errmsg = 'skipping %s\n%s' % (imgfile, sys.exc_info()[1])
Label(win, text=errmsg).pack()
main.mainloop()
示例7: Black_Hole
class Black_Hole(Simulton):
def __init__(self,x,y):
self._image = PhotoImage(file='Black_Hole.gif')
Simulton.__init__(self,x,y,self._image.width(),self._image.height())
self.radius = (self._image.height()/2)+((self._image.width()**2)/(8 * self._image.height()))
def update(self, model):
from hunter import Hunter
result = set()
for item in model.find(lambda item:isinstance(item, Prey) or isinstance(item,Hunter)):
if self.contains(item):
result.add(item)
return result
def contains(self,s):
if not isinstance(s,tuple):
s = (s.get_location()[0],s.get_location()[1])
return math.sqrt((self.get_location()[0] - s[0])**2 + (self.get_location()[1] - s[1])**2) <= self.radius
def display(self,canvas):
canvas.create_image(*self.get_location(),image=self._image)
示例8: image_canvas_simple
def image_canvas_simple():
win = Tk()
img = PhotoImage(file=gifdir+'ora-lp4e.gif')
can = Canvas(win)
can.pack(fill=BOTH)
can.config(width=img.width(), height=img.height())
can.create_image(2, 2, image=img, anchor=NW)
win.mainloop()
示例9: __init__
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())
self.savephoto = imgobj #Keep reference on me
示例10: Pulsator
class Pulsator(Black_Hole):
counter = 30
death = 15
def __init__(self,x,y,size = 30):
Black_Hole.__init__(self,x,y)
self.set_dimension(size, size)
self._image = PhotoImage(Image.open('space_amoeba.gif').convert('RGBA').resize((size, size), Image.ANTIALIAS))
self.radius = (self._image.height()/2)+((self._image.width()**2)/(8 * self._image.height()))
self._counter = 1
def update(self, model):
result = Black_Hole.update(self,model)
self._counter += 1
if len(result) > 0:
self.set_dimension(self.get_dimension()[0] + len(result), self.get_dimension()[1] + len(result))
self._image = PhotoImage(Image.open('space_amoeba.gif').convert('RGBA').resize((self.get_dimension()[0], self.get_dimension()[1]), Image.ANTIALIAS))
self.radius = (self._image.height()/2)+((self._image.width()**2)/(8 * self._image.height()))
self._counter = 1
if self._counter == Pulsator.counter:
self.set_dimension(self.get_dimension()[0] - 1, self.get_dimension()[1] - 1)
self._image = PhotoImage(Image.open('space_amoeba.gif').convert('RGBA').resize((self.get_dimension()[0], self.get_dimension()[1]), Image.ANTIALIAS))
self.radius = (self._image.height()/2)+((self._image.width()**2)/(8 * self._image.height()))
self._counter = 1
if self.get_dimension() < (Pulsator.death,Pulsator.death):
result.add(self)
return result
示例11: pil_image_viewer
def pil_image_viewer():
from PIL.ImageTk import PhotoImage
imgdir = 'images'
imgfile = 'florida-2009-1.jpg'
if len(sys.argv) > 1:
imgfile = sys.argv[1]
imgpath = os.path.join(imgdir, imgfile)
win = Tk()
win.title(imgfile)
imgobj = PhotoImage(file=imgpath)
Label(win, image=imgobj).pack()
win.mainloop()
print(imgobj.width(), imgobj.height())
示例12: Special
class Special(Simulton):
def __init__(self,x,y):
self._image = PhotoImage(Image.open('bigbang.gif').convert('RGBA').resize((20, 20), Image.ANTIALIAS))
Simulton.__init__(self,x,y,self._image.width(),self._image.height())
self._center = (int(x),int(y))
self._explosion = 500
def update(self, model):
self._explosion = model.world()[1]/2
all_swallowed = True
if self.get_dimension() >= model.world():
model.big_bang = None
elif self.get_dimension() >= (self._explosion,self._explosion):
total = model.world()
for item in model.items:
item.set_location(random.uniform(1,total[0] - 1),random.uniform(1,total[1] - 1))
self.set_dimension(total[0], total[1])
else:
for item in model.items:
location = item.get_location()
location = (int(location[0]),int(location[1]))
if location != self._center:
all_swallowed = False
if location[0] < self._center[0]:
new_x = location[0] + 1
elif location[0] > self._center[0]:
new_x = location[0] - 1
else:
new_x = location[0]
if location[1] < self._center[1]:
new_y = location[1] + 1
elif location[1] > self._center[1]:
new_y = location[1] - 1
else:
new_y = location[1]
item.set_location(new_x,new_y)
else:
self.change_dimension(1, 1)
self._image = PhotoImage(Image.open('bigbang.gif').convert('RGBA').resize((self.get_dimension()[0], self.get_dimension()[1]), Image.ANTIALIAS))
if all_swallowed:
self.change_dimension(10, 10)
self._image = PhotoImage(Image.open('bigbang.gif').convert('RGBA').resize((self.get_dimension()[0], self.get_dimension()[1]), Image.ANTIALIAS))
def display(self,canvas):
canvas.create_image(*self.get_location(),image=self._image)
示例13: Ball
class Ball(Prey):
radius = 5
def __init__(self,x,y):
self._image = PhotoImage(file='asteroid.gif')
Prey.__init__(self,x,y,self._image.width(),self._image.height(),random.random()*math.pi*2,5)
def update(self,model):
self.move()
return set()
def display(self,canvas):
canvas.create_image(*self.get_location(),image=self._image)
示例14: Floater
class Floater(Prey):
def __init__(self,x,y):
self._image = PhotoImage(file='ufo.gif')
Prey.__init__(self,x,y,self._image.width(),self._image.height(),0,5)
self.randomize_angle()
def update(self,model):
if random() <= .3:
new_speed = min(7,max(3,self.get_speed() + random()-.5))
self.set_velocity(new_speed, self.get_angle()+random()-.5)
self.move()
def display(self,the_canvas):
the_canvas.create_image(self._x,self._y,image=self._image)
示例15: Floater
class Floater(Prey):
def __init__(self,x,y):
self._image = PhotoImage(file='enterprise.gif')
Prey.__init__(self,x,y,self._image.width(),self._image.height(),random.random()*math.pi*2,5)
def update(self,model):
if random.randint(1,10) <= 3:
speed_difference = random.uniform(-0.5, 0.5)
if self.get_speed() + speed_difference >= 3 and self.get_speed() + speed_difference <= 7:
self.set_speed(self.get_speed() + speed_difference)
self.set_angle(self.get_angle() + random.uniform(-0.5, 0.5))
self.move()
return set()
def display(self,canvas):
canvas.create_image(*self.get_location(),image=self._image)