Skip to content

Commit

Permalink
Support png images
Browse files Browse the repository at this point in the history
Signed-off-by: Luigi311 <[email protected]>
  • Loading branch information
luigi311 committed Oct 10, 2023
1 parent df95264 commit efe97ae
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
55 changes: 43 additions & 12 deletions images/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,44 @@ def process_raw(dng_file, half_size=False, auto_white_balance=False):

return image

def process_image(image_path, half_size=False, auto_white_balance=False):
if image_path.endswith("dng"):
image = process_raw(image_path, half_size, auto_white_balance)
else:
image = cv2.imread(image_path)

return image


def resize_images(images):
# Get the minimum width and height of all the images
min_x = min([image.shape[1] for image in images])
min_y = min([image.shape[0] for image in images])

# Resize all the images to the minimum width and height if they are larger and aspect ratio is compatible
resized_images = []
for image in images:
if image.shape[1] > min_x or image.shape[0] > min_y:
aspect_ratio = image.shape[1] / image.shape[0]
if aspect_ratio > 1.0: # Landscape orientation
new_width = min_x
new_height = int(min_x / aspect_ratio)
else: # Portrait or square orientation
new_height = min_y
new_width = int(min_y * aspect_ratio)

# Check if resizing is possible based on new dimensions
if new_width <= min_x and new_height <= min_y:
resized_image = cv2.resize(image, (new_width, new_height))
resized_images.append(resized_image)
else:
# Image can't be resized while maintaining aspect ratio, so skip it
print(f"Skipping image due to aspect ratio: {image.shape}")
else:
resized_images.append(image)

return resized_images


def loadImages(path, threads=None, half_size=False, auto_white_balance=False):
"""
Expand All @@ -44,26 +82,19 @@ def loadImages(path, threads=None, half_size=False, auto_white_balance=False):
"""

dng_files, tiff_files = files(path)
file_list = files(path)

images = []

# Default to half the number of cpu cores due to rawpy using multiple threads
workers = threads if threads else max(floor(os.cpu_count() / 2), 1)
# Instead of appending, concatenate the result images to numpy_array
with ProcessPoolExecutor(max_workers=workers) as executor:
if dng_files:
for result_image in executor.map(
process_raw,
dng_files,
[half_size] * len(dng_files),
[auto_white_balance] * len(dng_files),
):
images.append(result_image)

if tiff_files:
for result_image in executor.map(cv2.imread, tiff_files):
if file_list:
for result_image in executor.map(process_image, file_list, [half_size] * len(file_list), [auto_white_balance] * len(file_list)):
images.append(result_image)

images = resize_images(images)

return np.array(images)

Expand Down
24 changes: 14 additions & 10 deletions utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from concurrent.futures import ThreadPoolExecutor


def files(path):
def files(path: str) -> list:
# Check if the path exists and is a directory
if not os.path.exists(path) or not os.path.isdir(path):
raise ValueError(f"ERROR {path} is not a valid directory.")
Expand All @@ -17,15 +17,19 @@ def files(path):
file_list = os.listdir(path)

# Filter the list to only include dng, tiff, and hdf5 files
process_file_list = [
os.path.join(path, x) for x in file_list if x.endswith(("dng", "tiff"))
]

# Check if there are any dng or tiff files in the directory
dng_files = [x for x in process_file_list if x.endswith("dng")]
tiff_files = [x for x in process_file_list if x.endswith("tiff")]

return dng_files, tiff_files
process_file_list = []

for file in file_list:
if file.endswith("dng") or file.endswith("tiff"):
process_file_list.append(os.path.join(path, file))
# Allow other unncompressed formats to be processed
elif file.endswith("png"):
# exclude all that include main in the name to
# avoid processing processed images
if "main" not in file:
process_file_list.append(os.path.join(path, file))

return process_file_list


def downloader(url, file_name, directory):
Expand Down

0 comments on commit efe97ae

Please sign in to comment.