簡體   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