可以打开和关闭图像的 Python 模块 [英] Python module that can open AND close an image

查看:99
本文介绍了可以打开和关闭图像的 Python 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过 PIL、Matplotlib 和 pygame,它们都可以轻松地在新窗口中打开图像,但它要么无法通过命令关闭,要么 - 在 pygame 的情况下 - 无法使用新图像重新打开.是否有任何模块可以做到这一点?

I've tried out PIL, Matplotlib, and pygame, they all can open an image in a new window easy, but it either can't be closed via command, or - in pygame's case - cannot reopen with a new image. Are there any module(s) that can do this?

这是它通过的代码示例:

Here's an example of the code it goes through:

#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)

这样的事情有可能吗?唯一的要求是它打开显示该窗口的任何窗口都可以关闭,并用不同的图像重新打开

Is anything like this possible? the only requirement is that whatever window it opens to display the window can be closed, and reopened with a different image

推荐答案

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

I don't have problem to reopen window in PyGame with new image on Linux but maybe it depends on system. (BTW: Some systems may need to get events to show 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])时关闭窗口.


Pygame version which closes window on pressing key Enter/Return (or using button [X]).

但它会阻止其他代码,它必须等到您关闭窗口.

But it blocks other code and it has to wait till you close 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')

<小时>

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


To show window which can be closed by Enter and run other commands at the same time it would have to run PyGame in thread.

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

This code runs PyGame in thread and you can close it with Enter/Return. If you don't close it then code will close it using imclose() after few other commands (emulated by 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

这篇关于可以打开和关闭图像的 Python 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆