-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSaveImage.py
More file actions
170 lines (137 loc) · 5.55 KB
/
SaveImage.py
File metadata and controls
170 lines (137 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from astropy.io import fits
import tifffile as tiff
from tkinter import filedialog
from save_files.SaveFIT import SaveFIT
from save_files.SaveTIFF import SaveTIFF
from save_files.SaveJPG import SaveJPG
from save_files.SavePNG import SavePNG
from util.ImageTypeUtil import ImageTypeUtil
from spikes.SpikeRenderer import SpikeRenderer
SPIKE_INTENSITY = 0.5
class SaveImage:
def __init__(self, processor):
# Pull everything we need from ImageProcessor
self.processor = processor
def display_preview(self, image, output_path=None):
if image.ndim == 3 and image.shape[0] in [3, 4]:
image = np.transpose(image, (1, 2, 0))
img = image.astype(np.float32)
img = img - np.percentile(img, 1)
img = img / (np.percentile(img, 99) + 1e-6)
img = np.clip(img, 0, 1)
plt.figure(figsize=(12, 12))
plt.imshow(img, interpolation="nearest")
title = os.path.basename(output_path) if output_path else "Preview"
plt.title(title)
plt.axis("off")
plt.show()
def save(self):
processor = self.processor
# Always operate from original data
if processor.original_fits_data is not None:
original = processor.original_fits_data.astype(np.float32)
else:
# fallback for non-FITS input
if processor.processed_image is None:
print("No data available to save.")
return
original = processor.processed_image.astype(np.float32)
output_path = (processor.output_image or "").strip()
if not output_path:
output_path = filedialog.asksaveasfilename(
defaultextension=".png",
filetypes=[
("PNG files", "*.png"),
("TIFF files", "*.tif *.tiff"),
("JPEG files", "*.jpg"),
("FITS files", "*.fit *.fits"),
("All files", "*.*"),
],
)
if not output_path:
print("Save cancelled.")
return
processor.output_image = output_path
ext = os.path.splitext(output_path)[1].lower()
if ext == ".":
output_path = output_path.rstrip(".") + ".png"
ext = ".png"
# Use ImageTypeUtil to determine renderer/type
img_type = ImageTypeUtil.get_image_type(output_path)
if img_type == "fit":
SaveFIT(processor).save(output_path)
return
elif img_type == "tiff":
SaveTIFF(processor).save(output_path)
return
elif img_type == "jpg":
SaveJPG(processor).save(output_path)
return
elif img_type == "png":
SavePNG(processor).save(output_path)
return
# --- BMP ---
if ext == ".bmp":
data = self._render_from_original(original, mode="standard")
norm = (data - np.min(data)) / (np.max(data) - np.min(data) + 1e-6)
img_8 = (norm * 255).astype(np.uint8)
img = Image.fromarray(img_8)
img.save(output_path)
print(f"Saved output to {output_path}")
return
# fallback
img = Image.fromarray(original)
fallback = output_path + ".png"
img.save(fallback)
print(f"Unknown extension. Saved as {fallback}")
def _render_from_original(self, original, mode="default"):
processor = self.processor
min_val = np.min(original)
max_val = np.max(original)
norm = (original - min_val) / (max_val - min_val + 1e-6)
display = (norm * 255).astype(np.uint8)
if display.ndim == 2:
display_rgb = np.stack([display] * 3, axis=-1)
elif display.ndim == 3 and display.shape[0] in (3, 4):
display_rgb = np.transpose(display[:3], (1, 2, 0))
else:
display_rgb = display
params = {
"min_threshold": processor.min_threshold,
"max_threshold": processor.max_threshold,
"spike_length_multiplier": processor.spike_length_multiplier,
"spike_thickness_multiplier": processor.spike_thickness_multiplier,
"blur_kernel_size": processor.blur_kernel_size,
"blur_multiplier": processor.blur_multiplier,
"rotation_angle": processor.rotation_angle,
}
renderer = SpikeRenderer(params)
sources = processor.detect_stars(original)
rendered = renderer.render(
image=display_rgb,
sources=sources,
input_path=processor.input_image.lower(),
)
rendered_gray = np.mean(rendered.astype(np.float32), axis=2)
rendered_norm = rendered_gray / 255.0
# TEMP: amplify spikes for debugging visibility
# Mode-specific intensity control
intensity = SPIKE_INTENSITY
if mode == "visual":
intensity = SPIKE_INTENSITY * 1.0
elif mode == "standard":
intensity = SPIKE_INTENSITY * 1.0
elif mode == "tiff":
intensity = SPIKE_INTENSITY
elif mode == "scientific":
intensity = SPIKE_INTENSITY
result = original + (rendered_norm * np.max(original) * intensity)
print("DEBUG:")
print("original min/max:", np.min(original), np.max(original))
print("rendered_norm min/max:", np.min(rendered_norm), np.max(rendered_norm))
print("final data min/max:", np.min(result), np.max(result))
return result