Posterize Image
Reduce the number of colors in an image to create a poster-like effect with flat areas of color.
/api/image/posterize
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
file | File | Yes | The image file to posterize. Supported formats: JPEG, PNG, WebP, BMP, TIFF. |
levels | Integer | No | The number of color levels per channel. Range: 2-16. Default: 4 . |
Response
Returns the posterized image in the same format as the input file.
Example Request
curl -X POST "https://oyyi.xyz/api/image/posterize" -H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@image.jpg" \
-F "levels=4" \
--output posterized.jpg
Example with Python
import requests
url = "https://oyyi.xyz/api/image/posterize"
files = {
'file': open('image.jpg', 'rb')
}
data = {
'levels': 4
}
response = requests.post(url, files=files, data=data)
# Save the posterized image
with open('posterized.jpg', 'wb') as f:
f.write(response.content)
Example with JavaScript
// Using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('levels', '4');
fetch('https://oyyi.xyz/api/image/posterize', {
method: 'POST',
body: formData
})
.then(response => response.blob())
.then(blob => {
// Create a URL for the blob
const url = URL.createObjectURL(blob);
// Create a link to download the image
const a = document.createElement('a');
a.href = url;
a.download = 'posterized.jpg';
a.click();
// Clean up by revoking the URL
URL.revokeObjectURL(url);
})
.catch(error => console.error('Error:', error));
Posterization Methods
Uniform
Divides the color range evenly into the specified number of levels. This creates a more stylized, graphic look with evenly distributed color bands.
K-means
Uses k-means clustering to find the most representative colors in the image. This often produces more natural-looking results that better preserve the image's original appearance.
How It Works
Posterization reduces the continuous gradation of tones in an image to a smaller number of distinct tones:
- Uniform method: Each color channel (R, G, B) is divided into the specified number of evenly spaced levels. For example, with 4 levels, the possible values would be 0, 85, 170, and 255.
- K-means method: The algorithm analyzes the image to find the most common or representative colors, then maps each pixel to the closest of these colors.
The result is an image with fewer colors and more distinct color boundaries, creating a stylized, poster-like effect.
Level Examples
2 Levels
Very stark, high-contrast effect (essentially black and white for each channel)
4-8 Levels
Classic posterization effect, good balance between stylization and recognizability
10-16 Levels
Subtle effect, retains more detail while still creating visible color banding
Error Responses
Status Code | Description |
---|---|
400 | Bad request. Missing required parameters, invalid levels value, invalid method, or invalid file format. |
413 | Payload too large. The file size exceeds the maximum allowed limit. |
500 | Internal server error. Something went wrong on the server. |
Notes
- Posterization is applied to all color channels (RGB) but does not affect the alpha channel (transparency) if present.
- Lower level values create more dramatic, stylized effects.
- The k-means method is more computationally intensive but often produces more visually pleasing results.
- The maximum file size allowed is 10MB.
- Posterization can significantly reduce file size when saving to formats like PNG, as there are fewer unique colors to encode.
Use Cases
- Creating retro or vintage poster-style artwork
- Simplifying complex images for graphic design purposes
- Preparing images for screen printing or other printing techniques with limited color capabilities
- Creating stylized visual effects for social media or digital art
- Reducing file size by limiting the color palette
- Creating base images for further digital painting or illustration