-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_image_load.py
More file actions
92 lines (73 loc) · 2.86 KB
/
test_image_load.py
File metadata and controls
92 lines (73 loc) · 2.86 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
"""
Script to analyze blood sample images with robust file handling.
"""
import cv2
import numpy as np
import os
import glob
from pathlib import Path
import shutil
def find_blood_sample():
"""Find the blood sample image in raw directory."""
raw_dir = Path('data/raw')
png_files = list(raw_dir.glob('*.png'))
if not png_files:
return None
# Return the largest PNG file (likely our screenshot)
return max(png_files, key=lambda p: p.stat().st_size)
def save_image_safely(image, output_path):
"""Save image with special character handling."""
try:
# Convert path to string and normalize it
output_path = str(Path(output_path).resolve())
# Try imencode/imdecode approach first
is_success, buffer = cv2.imencode('.png', image)
if not is_success:
return False
with open(output_path, 'wb') as f:
f.write(buffer)
return True
except Exception as e:
print(f"Error saving image: {e}")
return False
def main():
try:
print("Starting blood sample analysis...")
# Locate the blood sample image
input_path = find_blood_sample()
if not input_path:
raise FileNotFoundError("No PNG files found in data/raw directory")
print(f'Found image: {input_path.name}')
print(f'File size: {input_path.stat().st_size:,} bytes')
# Ensure the processed directory exists
output_dir = Path('data/processed')
output_dir.mkdir(parents=True, exist_ok=True)
# Read the image file in binary mode
with open(input_path, 'rb') as f:
data = f.read()
# Convert to numpy array and decode
img_array = np.frombuffer(data, dtype=np.uint8)
image = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
if image is None:
raise ValueError("Failed to decode image data")
print(f'Successfully loaded image: {image.shape}')
# Try to save a verification image
verification_path = output_dir / 'verification.png'
if save_image_safely(image, verification_path):
print(f'Successfully saved verification image to {verification_path}')
# Verify the saved image can be loaded
test_load = cv2.imread(str(verification_path))
if test_load is not None:
print('Verification image loaded successfully')
else:
print('Warning: Could not reload verification image')
else:
print('Failed to save verification image')
except Exception as e:
print(f"Error: {type(e).__name__}: {e}")
import traceback
traceback.print_exc()
finally:
print("\nProgram complete.")
if __name__ == '__main__':
main()