Created
November 12, 2024 00:14
-
-
Save hradec/adb03ea9fd02ee2a8cde00a69fb26f62 to your computer and use it in GitHub Desktop.
tif-fp-image-display.py - display a floating point tif image without any color correction (mouse over shows pixel floating point value)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib.widgets import Button | |
from PIL import Image | |
import sys | |
import os | |
from PySide6.QtWidgets import QApplication | |
# Load the grayscale floating point TIFF image from command line | |
if len(sys.argv) != 2: | |
print("Usage: python script.py <image_path>") | |
sys.exit(1) | |
image_path = sys.argv[1] | |
image_dir = os.path.dirname(image_path) | |
image_files = sorted([f for f in os.listdir(image_dir) if f.endswith('.tif')]) | |
current_index = image_files.index(os.path.basename(image_path)) | |
# Function to load and display an image | |
def load_image(index): | |
global image_data, cax | |
image_path = os.path.join(image_dir, image_files[index]) | |
image = Image.open(image_path) | |
image_data = np.array(image, dtype=np.float32) | |
cax.set_data(image_data) | |
ax.set_title(image_files[index]) | |
fig.canvas.draw() | |
# Initial image load | |
image = Image.open(image_path) | |
image_data = np.array(image, dtype=np.float32) | |
# Set figure size to 80% of screen width and height using PySide6 to get screen size | |
app = QApplication.instance() or QApplication(sys.argv) | |
screen = app.primaryScreen().size() | |
screen_width = screen.width() | |
screen_height = screen.height() | |
fig_width = 0.8 * screen_width | |
fig_height = 0.8 * screen_height | |
# Close all existing figures to prevent extra windows | |
plt.close('all') | |
# Explicitly create the figure and add axes | |
fig = plt.figure(figsize=(fig_width / plt.gcf().dpi, fig_height / plt.gcf().dpi), num='Image Viewer') | |
ax = fig.add_subplot(111) | |
cax = ax.imshow(image_data, cmap='gray', interpolation='none') | |
fig.colorbar(cax, ax=ax) | |
ax.set_title(image_files[current_index]) | |
plt.close(1) | |
# Function to show pixel value on mouse hover | |
def format_coord(x, y): | |
col = int(x + 0.5) | |
row = int(y + 0.5) | |
if 0 <= col < image_data.shape[1] and 0 <= row < image_data.shape[0]: | |
z = image_data[row, col] | |
return f'x={x:.1f}, y={y:.1f}, value={z:.3f}' | |
else: | |
return f'x={x:.1f}, y={y:.1f}' | |
ax.format_coord = format_coord | |
# Function to load the next image | |
def next_image(event): | |
global current_index | |
current_index = (current_index + 1) % len(image_files) | |
load_image(current_index) | |
# Function to load the previous image | |
def prev_image(event): | |
global current_index | |
current_index = (current_index - 1) % len(image_files) | |
load_image(current_index) | |
# Add a button to load the next image | |
ax_next = fig.add_axes([0.8, 0.01, 0.1, 0.075]) | |
btn_next = Button(ax_next, 'Next') | |
btn_next.on_clicked(next_image) | |
# Add a button to load the previous image | |
ax_prev = fig.add_axes([0.69, 0.01, 0.1, 0.075]) | |
btn_prev = Button(ax_prev, 'Previous') | |
btn_prev.on_clicked(prev_image) | |
# Show the figure | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment