본문 바로가기
Python

Dragon Moving Banner

by YJHTPII 2024. 5. 7.
반응형

 

#Dragon Moving Banner


import matplotlib.pyplot as plt
import numpy as np
from matplotlib.image import imread

# Load two PNG images
image1_path = "1.png"
image2_path = "2.png"
x1 = imread(image1_path)
x2 = imread(image2_path)
'''
# two images x1 is initially visible, x2 is not
x1 = np.random.random((100, 100))
x2 = np.random.random((150, 175))
'''

# Set the size of the figure
plt.figure(figsize=(10, 2))

# arbitrary extent - both images must have the same extent if you want
# them to be resampled into the same axes space
extent = (0, 1, 0, 1)
im1 = plt.imshow(x1, extent=extent, aspect='auto')
im2 = plt.imshow(x2, extent=extent, aspect='auto')
im2.set_visible(False)

# Hide ticks
plt.xticks([])
plt.yticks([])

# Variable to track the currently visible image
current_image = im1


def toggle_images(event):
    'toggle the visible state of the two images'
    if event.key != 't':
        return
    b1 = im1.get_visible()
    b2 = im2.get_visible()
    im1.set_visible(not b1)
    im2.set_visible(not b2)
    plt.draw()
plt.connect('key_press_event', toggle_images)
'''
# Function to toggle between images
def toggle_images_auto():
    global current_image
    if current_image == im1:
        im1.set_visible(False)
        im2.set_visible(True)
        current_image = im2
    else:
        im1.set_visible(True)
        im2.set_visible(False)
        current_image = im1
    plt.draw()

# Set up a timer to toggle images every 0.6 seconds (600 milliseconds)
timer = plt.gcf().canvas.new_timer(interval=600)
timer.add_callback(toggle_images_auto)
timer.start()
'''
plt.show()
반응형

댓글