Skip to content

Commit

Permalink
Merge pull request #46 from UBC-MDS/feat-print-color
Browse files Browse the repository at this point in the history
modify create image function
  • Loading branch information
joeywwwu authored Jan 27, 2024
2 parents 076c31d + 0b029f1 commit 94f67f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
17 changes: 10 additions & 7 deletions src/hexdropper/create_color_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -45,17 +45,20 @@ 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)
if parent_dir and not os.path.exists(parent_dir):
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)
6 changes: 3 additions & 3 deletions tests/test_create_color_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)

0 comments on commit 94f67f8

Please sign in to comment.