File size: 3,432 Bytes
534218d | 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 | #!/usr/bin/env python3
"""
Test script to verify image loading works correctly.
Run this to diagnose JPG rendering issues.
"""
import sys
from pathlib import Path
# Check Python version
print(f"Python version: {sys.version}")
print(f"Python executable: {sys.executable}")
print("-" * 50)
# Test imports
print("Testing imports...")
try:
import numpy as np
print(f"β
numpy: {np.__version__}")
except ImportError as e:
print(f"β numpy: {e}")
try:
import cv2
print(f"β
opencv: {cv2.__version__}")
except ImportError as e:
print(f"β opencv: {e}")
try:
from PIL import Image
import PIL
print(f"β
Pillow: {PIL.__version__}")
except ImportError as e:
print(f"β Pillow: {e}")
try:
import streamlit as st
print(f"β
streamlit: {st.__version__}")
except ImportError as e:
print(f"β streamlit: {e}")
print("-" * 50)
# Test image loading functions
def test_load_image(image_path):
"""Test different methods of loading an image."""
image_path = Path(image_path)
print(f"\nTesting: {image_path}")
print(f" Exists: {image_path.exists()}")
if not image_path.exists():
print(" β File not found!")
return None
# Method 1: PIL
try:
with Image.open(image_path) as pil_img:
if pil_img.mode != 'RGB':
pil_img = pil_img.convert('RGB')
img = np.array(pil_img)
print(f" β
PIL loaded: shape={img.shape}, dtype={img.dtype}")
except Exception as e:
print(f" β PIL failed: {e}")
# Method 2: cv2.imread
try:
img = cv2.imread(str(image_path))
if img is not None:
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print(f" β
cv2.imread loaded: shape={img_rgb.shape}, dtype={img_rgb.dtype}")
else:
print(f" β cv2.imread returned None")
except Exception as e:
print(f" β cv2.imread failed: {e}")
# Method 3: cv2.imdecode (bytes)
try:
with open(image_path, 'rb') as f:
file_bytes = f.read()
nparr = np.frombuffer(file_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img is not None:
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print(f" β
cv2.imdecode loaded: shape={img_rgb.shape}, dtype={img_rgb.dtype}")
else:
print(f" β cv2.imdecode returned None")
except Exception as e:
print(f" β cv2.imdecode failed: {e}")
return img
# Find and test images
print("\nSearching for test images...")
base_dirs = [
Path("./uploaded_images"),
Path("./Pacientes"),
Path("."),
]
test_images = []
for base in base_dirs:
if base.exists():
for ext in ["*.jpg", "*.JPG", "*.jpeg", "*.png"]:
test_images.extend(list(base.glob(f"**/{ext}"))[:2])
if test_images:
print(f"Found {len(test_images)} images to test")
for img_path in test_images[:5]: # Test up to 5 images
test_load_image(img_path)
else:
print("No images found. Creating a test image...")
# Create a simple test image
test_img = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
test_path = Path("./test_image.jpg")
cv2.imwrite(str(test_path), test_img)
print(f"Created test image: {test_path}")
test_load_image(test_path)
print("\n" + "=" * 50)
print("Test complete!")
print("=" * 50)
|