From 0b029f15d24f8d06d2143616a937d19531c87f8e Mon Sep 17 00:00:00 2001 From: hchqin Date: Sat, 27 Jan 2024 11:04:44 -0800 Subject: [PATCH] modify create image function --- src/hexdropper/create_color_image.py | 17 ++++++++++------- tests/test_create_color_image.py | 6 +++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/hexdropper/create_color_image.py b/src/hexdropper/create_color_image.py index bcf92fa..ec4ec64 100644 --- a/src/hexdropper/create_color_image.py +++ b/src/hexdropper/create_color_image.py @@ -9,7 +9,7 @@ def create_color_image(hex_code, image_size=(200, 200), output_path=None): Parameters ---------- hex_code : str - A hexadecimal color code string (e.g., 'FF5733'), without the '#' symbol. + A hexadecimal color code string (e.g., '#FF5733'), without the '#' symbol. image_size : tuple of int, optional The size of the image as a (width, height) tuple. Default is (200, 200). output_path : str, optional @@ -24,15 +24,15 @@ def create_color_image(hex_code, image_size=(200, 200), output_path=None): Examples -------- - >>> create_color_image('FF5733') + >>> create_color_image('#FF5733') # This will create and save an image with a red background and 'FF5733' text in the center. - >>> create_color_image('00FF00', (100, 100), '/path/to/save/image.png') + >>> create_color_image('#00FF00', (100, 100), '/path/to/save/image.png') # This will create a 100x100 green background image with '00FF00' text in the center and save it to the specified path. """ # Validate the hex code - if not bool(re.match(r'^(?:[0-9a-fA-F]{3}){1,2}$', hex_code)): - raise ValueError("Invalid hex code provided, hexcode should be a string without the '#' symbol") + if not bool(re.match(r'^#(?:[0-9a-fA-F]{3}){1,2}$', hex_code)): + raise ValueError("Invalid hex code provided, hexcode should be a string with the '#' symbol") # Check if the hex_code is a string if not isinstance(hex_code, str): @@ -45,9 +45,12 @@ def create_color_image(hex_code, image_size=(200, 200), output_path=None): if not (isinstance(image_size[0], int) and isinstance(image_size[1], int)): raise ValueError("image_size should be a tuple of int") + # Process the hex code for filename + hex_code_for_filename = hex_code.lstrip('#') + # If no output path is provided, use the hex code as the file name if output_path is None: - output_path = hex_code + '.png' + output_path = hex_code_for_filename + '.png' else: # Ensure the directory of the output path exists, create it if not parent_dir = os.path.dirname(output_path) @@ -55,7 +58,7 @@ def create_color_image(hex_code, image_size=(200, 200), output_path=None): os.makedirs(parent_dir) # Create a new image with the specified color - image = Image.new("RGB", image_size, f"#{hex_code}") + image = Image.new("RGB", image_size, hex_code) # Save the image to the specified path image.save(output_path) \ No newline at end of file diff --git a/tests/test_create_color_image.py b/tests/test_create_color_image.py index 66869a5..e4029f8 100644 --- a/tests/test_create_color_image.py +++ b/tests/test_create_color_image.py @@ -4,8 +4,8 @@ from hexdropper.create_color_image import create_color_image # Test data -valid_hex_code = 'FF5733' -invalid_hex_code = 'ZZZ999' +valid_hex_code = '#FF5733' +invalid_hex_code = '#ZZZ999' valid_image_size = (200, 200) invalid_image_size = 'invalid_size' output_path = os.path.join('tests', 'output', 'test_image.png') @@ -29,6 +29,6 @@ def test_create_color_image_invalid_size(): # Test function without specifying output path def test_create_color_image_no_output_path(): create_color_image(valid_hex_code, valid_image_size) - default_output_path = valid_hex_code + '.png' + default_output_path = valid_hex_code.lstrip('#') + '.png' assert os.path.isfile(default_output_path) os.remove(default_output_path)