Spaces:
Build error
Build error
Upload 2 files
Browse files- src/utils/image.py +17 -0
- src/utils/utils.py +31 -0
src/utils/image.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import os
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
def extract_url_after_filename(url):
|
| 7 |
+
"""Extract the filename from the URL."""
|
| 8 |
+
match = re.search(r'\?filename=(.*)', url)
|
| 9 |
+
return match.group(1) if match else None
|
| 10 |
+
|
| 11 |
+
def convert_jp2_to_image(content):
|
| 12 |
+
"""Convert JP2 image content to PIL Image."""
|
| 13 |
+
try:
|
| 14 |
+
return Image.open(BytesIO(content))
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error opening image: {e}")
|
| 17 |
+
return None
|
src/utils/utils.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from urllib.parse import urlparse
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def extract_s3_path_from_url(url):
|
| 5 |
+
"""
|
| 6 |
+
Extracts the S3 object path from an S3 URL or URI.
|
| 7 |
+
|
| 8 |
+
This function parses S3 URLs/URIs and returns just the object path portion,
|
| 9 |
+
removing the protocol (s3://), bucket name, and any leading slashes.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
url (str): The full S3 URI (e.g., 's3://eodata/path/to/file.jp2')
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
str: The S3 object path (without protocol, bucket name and leading slashes)
|
| 16 |
+
"""
|
| 17 |
+
# If it's not an S3 URI, return it unchanged
|
| 18 |
+
if not url.startswith('s3://'):
|
| 19 |
+
return url
|
| 20 |
+
|
| 21 |
+
# Parse the S3 URI
|
| 22 |
+
parsed_url = urlparse(url)
|
| 23 |
+
|
| 24 |
+
# Ensure this is an S3 URL
|
| 25 |
+
if parsed_url.scheme != 's3':
|
| 26 |
+
raise ValueError(f"URL {url} is not an S3 URL")
|
| 27 |
+
|
| 28 |
+
# Extract the path without leading slashes
|
| 29 |
+
object_path = parsed_url.path.lstrip('/')
|
| 30 |
+
|
| 31 |
+
return object_path
|