Centroid Calculator
A Max Westerman Project
What does it do?
This Python program will find the center of an object. It will consider the RGB values the user gives as not part of the object and find the center of the rest. This can be useful for finding the center of very complex shapes, such as states, congressional districts, or parts with complex geometry. This can also be used to find the center of mass for physics and engineering.
The calculator will automatically size the centroid dot, text positioning, and text sizing based on the size of the image. If you want to change the default settings, when you run your centroid calculation there's an option to customize these parameters, seen in the F22 custom centroid below.
F22 Raptor Custom CentroidÂ
The program allows for the following custom settings: circle sizing, text sizing, text offset from the centroid, and text color. This centroid omitted white pixels.
Transparent Example
When you insert a transparent image it will convert all transparent pixels to white, then find the centroid relating to the white pixels given 255,255,255 as inputs.
Centroid of the United States
Calculated the geographic center of mass for the contiguous United States. This matches the center of mass shown on Wikipedia. In this image, the pure black pixels were removed from the calculation.
How to set up
Download the required libraries:
pip: - used to simplify downloading the other libraries
DUdraw: pip install dudraw - Utilized for drawing text and circles on the image
Numpy: pip install numpy - used with PIL to make the array of pixel values
PIL (Pillow): pip install Pillow - used with numpy to make the array of pixel values
How to use
Run the program and insert the pathname of the image you want the centroid to be calculated from as well as the place you want the file to be saved to. Enter the RGB of the pixels you don't want to be counted towards the centroid as well as optional custom formatting. Once finished, the image will show on your screen and be saved.
Future Improvements
Convert to a completely online experience with no downloads necessary, all hosted on a webpage.
Instead of converting transparent pixels to white and removing white pixels, just ignore the transparent pixels. Also has the added benefit of exporting the resulting image with it's transparent pixels intact.
Add a youtube video going through an example usage.
Add presets for the custom formatting in the case of calculating the centroid of many objects