简体   繁体   中英

Python module that can open AND close an image

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

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

EDIT: I have no problem to do the same in 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()

EDIT: 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')

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.

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

Similar code with 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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