o
    g                     @   s  d dl Z d dlmZ d dlZd dlZdddZdd Zedkre jd	d
Z	e	j
dedd e	j
ddeddd e	j
dddd e	 ZeejejZeryedee dej d ejrndd eD ZeD ]Zee qfnee ed  dS ed dS dS )    N)
ColorThief   c              
   C   s   t j| std|  dtjd dS t j| s&td|  dtjd dS zt| }|j|d}|W S  t	yS } ztd|  d	| tjd W Y d}~dS d}~ww )
al  
    Extracts a specified number of colors from an image using ColorThief.

    Args:
        image_path (str): The path to the image file.
        num_colors (int): The number of colors to extract in the palette.

    Returns:
        list: A list of RGB tuples representing the color palette,
              or None if the file is invalid or an error occurs.
    z Error: Image file not found at '')fileNzError: Path 'z' is not a file.)Zcolor_countzError processing image 'z': )
ospathexistsprintsysstderrisfiler   get_palette	Exception)
image_path
num_colorsZcolor_thiefpalettee r   3/Users/srajan/code/music-website/extract_palette.pyextract_colors   s   r   c                 C   s   d | d | d | d S )z,Converts an RGB tuple to a hex color string.z#{:02x}{:02x}{:02x}r         )format)Zrgbr   r   r   
rgb_to_hex"   s   r   __main__z0Extract a color palette from an album art image.)description
image_filez!Path to the album art image file.)typehelpz-nz--num_colors   z(Number of colors to extract (default: 6))r   defaultr   z--hex
store_truez2Output colors in hex format instead of RGB tuples.)actionr   z
Extracted z colors from 'z':c                 C   s   g | ]}t |qS r   )r   ).0colorr   r   r   
<listcomp>5   s    r%   r   )r   )argparseZ
colorthiefr   r
   r   r   r   __name__ArgumentParserparseradd_argumentstrint
parse_argsargsr   r   r   r	   lenhexhex_paletter$   exitr   r   r   r   <module>   s8    

