繁体   English   中英

Python 可以打开和关闭图像的模块

[英]Python module that can open AND close an image

我已经尝试过 PIL、Matplotlib 和 pygame,它们都可以在新的 window 中轻松打开图像,但它要么无法通过命令关闭,要么使用新图像无法重新打开。 有没有可以做到这一点的模块?

这是它通过的代码示例:

#this just waits for input from the user to continue
def pause():
  ps = input("\n[Press 'Enter' to continue]\n")


image = eval(unit).image
#yes, I know eval is bad but shhhhh, it works

#the main function
def function(foo):
  #Code displays information, nothing that should cause problems
  something.display(image.png)
  #line or several lines of code to hide image()
  page(pg)

这样的事情可能吗? 唯一的要求是,无论它打开什么 window 以显示 window 都可以关闭,然后用不同的图像重新打开

PyGame上使用新图像重新打开 PyGame 中的 window 没有问题,但可能取决于系统。 (顺便说一句:某些系统可能需要获取事件才能显示窗口)

import pygame
import time

#pygame.init()

def imshow(filename):
    #pygame.init()
    img = pygame.image.load(filename)
    size = img.get_rect().size
    screen = pygame.display.set_mode(size)
    screen.blit(img, (0, 0))
    pygame.display.flip()
    #pygame.event.clear()

def imclose():
    #pygame.quit()
    pygame.display.quit()

imshow('image1.jpg')
time.sleep(3)
imclose()

imshow('image2.png')
time.sleep(3)
imclose()

编辑:我在matplotlib中做同样的事情没有问题

import matplotlib.pyplot as plt

img = plt.imread('image1.jpg')
plt.imshow(img)
plt.pause(3)
plt.close()

img = plt.imread('image2.png')
plt.imshow(img)
plt.pause(3)
plt.close()

编辑: Pygame 版本,在按下Enter/Return键(或使用按钮[X] )时关闭 window。

但它会阻止其他代码,它必须等到您关闭 window。

import pygame

#pygame.init()

def imshow(filename):
    #pygame.init()
    img = pygame.image.load(filename)
    size = img.get_rect().size
    screen = pygame.display.set_mode(size)
    screen.blit(img, (0, 0))
    pygame.display.flip()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: # close by button [X]
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    running = False
    pygame.display.quit()
    #pygame.quit()


imshow('image1.jpg')
imshow('image2.png')

要显示 window 可以通过Enter关闭并同时运行其他命令,它必须在线程中运行PyGame

此代码在线程中运行PyGame ,您可以使用Enter/Return将其关闭。 如果你不关闭它,那么代码将在其他几个命令之后使用imclose()关闭它(由sleep()模拟)

import pygame

import threading
import time

def window(filename):
    global running 

    running = True

    #pygame.init()
    img = pygame.image.load(filename)
    size = img.get_rect().size
    screen = pygame.display.set_mode(size)
    screen.blit(img, (0, 0))
    pygame.display.flip()

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: # close by button [X]
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN: # close by ENTER
                    running = False

    pygame.display.quit()
    #pygame.quit()

def imshow(filename):
    threading.Thread(target=window, args=(filename,)).start()

def imclose():
    global running
    running = False

# ----------------------------------------------------------------------------

imshow('image1.jpg')
# emulate other commands
for x in range(3):
    print('1. other command ...', x)
    time.sleep(1)
imclose() # close by command

# emulate other commands
for x in range(3):
    print('2. other command ...', x)
    time.sleep(1)

imshow('image2.jpg')
# emulate other commands
for x in range(3):
    print('3. other command ...', x)
    time.sleep(1) # emulate other code
imclose() # close by command

Tkinter类似的代码

import tkinter as tk
from PIL import Image, ImageTk
import threading
import time

def window(filename):
    global running

    running = True

    def on_press(event):
        global running
        running = False

    root = tk.Tk()    
    photo = ImageTk.PhotoImage(Image.open(filename))
    label = tk.Label(root, image=photo)
    label.photo = photo
    label.pack()
    root.bind('<Return>', on_press) # close by ENTER
    #root.mainloop()

    while running:
        root.update()

    root.destroy()

def imshow(filename):
    threading.Thread(target=window, args=(filename,)).start()

def imclose():
    global running
    running = False

# ----------------------------------------------------------------------------

imshow('image1.jpg')
# emulate other commands
for x in range(3):
    print('1. other command ...', x)
    time.sleep(1)
imclose() # close by command

# emulate other commands
for x in range(3):
    print('2. other command ...', x)
    time.sleep(1)

imshow('image2.jpg')
# emulate other commands
for x in range(3):
    print('3. other command ...', x)
    time.sleep(1) # emulate other code
imclose() # close by command

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM