This script calculates the dominant colors of an image by using k-means clustering, and returns the obtained color palette. It can be used in batch to generate the grid of the changes in color palettes in a movie (provided that we have the frames which can be obtained with ffmpeg).
For now, I'm running my scripts from Atom with Hydrogen, so the easiest way to modify the output is to edit the inputs directly in the first lines of the code (clearly commented), and run the whole script. I hope to have some time in the future to do a proper terminal wrapper.
- Place your images in the
./in/
folder. - Run
mainBatch.py
. - All the processed files will be exported to the
./out/
folder.
The main files are:
- mainBatch.py: This script should be used to get all the images within a folder, process them, and export them into another folder.
- mainSingle.py: This is meant to be used for a single image to export the frame, swatch, and color codes (hex and rgb) into the output folder.
To change style parameters:
- CLST_NUM: Defines the number of dominant colors to detect.
- MAX_ITER: Sets the maximum number of iterations for the k-means algorithm.
- BAR_HEIGHT: Defines the height of the dominant colors bar as a proportion of the height of the input image.
- BUF_HEIGHT: Sets the buffer zone as a proportion of the height of the input image.
- BUF_COLOR: Sets the color of the buffer zone around the image.
The script needs the following dependencies to be installed either in the base installation or a virtual environment: cv2, Pillow, numpy, sklearn, and ffmpeg-python. These dependencies can be installed through the provided conda environment:
conda env create -f cpExtract.yml
the REQUIREMENTS.txt file:
conda create -n new cpExtract --file REQUIREMENTS.txt
or individually:
pip install opencv-python
pip install numpy
pip install Pillow
pip install scikit-learn
pip install ffmpeg-python
- Overlay the hex code to the swatch.
- Translate the images grid generator from Mathematica to Python.
- Link to the repo with the ffmpeg frames exporter.