Your task is to develop a Python program that allows a user to upload an image, analyze the image, and detect the major colors present in it. The program should then return the major colors in RGB or any valid web format, ensuring that the color extraction process is efficient. The program will return the image name and a list of major colors in a structured JSON format.
- An image file uploaded by the user. Supported formats include JPEG, PNG, GIF, etc.
- A JSON response containing:
- Image Name: The name of the uploaded image.
- Major Colors: A list of the major colors present in the image.
- The colors should be represented in either RGB or HEX format.
- The number of major colors should vary between 4 and 10.
-
Major Color Extraction:
- The program should detect between 4 and 10 major colors in the image using an efficient approach.
- Noise around the image (e.g., subtle color variations or shadows) should be ignored.
- Use KMeans clustering for color extraction.
-
Output Format:
- The output should be returned as a JSON object containing the image name and a list of detected major colors.
- Example output format:
{ "image_name": "sample_image.jpg", "colors": [ "rgb(123, 45, 67)", "rgb(255, 220, 180)", "rgb(100, 120, 130)", "rgb(90, 60, 40)" ] }
- Python: Core language for this assignment.
- Libraries: (just suggested not necessary to use)
Pillowfor image processing.scikit-learnfor KMeans clustering.numpyfor numerical operations.jsonfor returning the output in JSON format.
- Load and Preprocess Image:
- Use
Pillowto load the uploaded image and convert it to RGB format. - Optionally, resize the image to speed up processing for larger images.
- Use
- Extract Pixel Data:
- Convert the image into a numpy array containing RGB pixel values.
- Apply KMeans Clustering:
- Use KMeans clustering to group the pixels into clusters that represent the major colors.
- Set the number of clusters between 4 and 10.
- Convert RGB to HEX:
- Convert the RGB values of the cluster centers into HEX or RGB format to represent the major colors.
- After clustering, convert the cluster centers to a valid color format (either RGB or Hex).
- Return the color list along with the image name in a JSON format.
-
The program should return a JSON response like the following:
{ "image_name": "sample_image.jpg", "colors": [ "rgb(123, 45, 67)", "rgb(255, 220, 180)", "rgb(100, 120, 130)", "rgb(90, 60, 40)" ] }
- Correctness
Ensure the program correctly identifies and returns the major colors in the range of 4-10 colors.
- Performance
The program should process images efficiently, particularly for larger images.
- Code Quality
The code should be clean, modular, well-commented, and easy to understand.
- Error Handling
Handle cases such as invalid image formats or very small images gracefully.
Create a GitHub repository with the following:
- Python script (color_extractor.py or similar) containing the code.
- Sample images for testing the functionality.
- A README.md file explaining how to run and test the code.
- Ensure the code runs efficiently and produces correct results within the given constraints.
Note: Interns are encouraged to be creative and innovative in their approach to designing the portal. The goal is to create a user-friendly and visually appealing platform for accessing brand information efficiently.
© 2024 RecursiveZero Private Limited | All rights reserved.