Using Algodoo and Paint I made these six 300×300 monochromatic images of four convenient shapes:
This class of images has the following properties:
- They are always 300×300 pixels, monochromatic (black and white only), and have exactly four white regions that correspond to a square, a circle, a triangle, and a gear.
- The shapes never overlap or touch each other, nor do they touch the image border or go out of bounds.
- The shapes always have the same size, but they may be rotated and positioned in any way.
(The shapes also have equal areas, though when rastered like this their pixel counts are unlikely to be exactly equivalent.)
Write the shortest program or function possible that takes in the filename of such an image and turns all the white pixels...
(255, 0, 0)if they are in the square.
(0, 0, 255)if they are in the circle.
(0, 255, 0)if they are in the triangle.
(255, 255, 0)if they are in the gear.
Your program should work for effectively all possible input images. (Only valid 300×300 monochromatic images will be input.) The six images I have provided are merely examples, you may not hardcode their output into your program.
You may not use computer vision libraries or functions, built-in or external. The point is to do this using your own pixel-level operations. You may use image libraries that simply let you open and modify images (e.g. PIL for Python).
You may use any common lossless image file formats for input and output as long as you stick to the color scheme.
You can take in the image filename as a function argument, from stdin, or from the command line. The output image can be saved to a new file, the same file, or simply displayed.
The submission with the fewest bytes wins. I may test submissions with additional images to determine their validity.