Apply Mask
Apply a mask to an image to selectively show or hide parts of the image.
POST
/api/image/apply-mask
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
base_file | File | Yes | The base image file to apply the mask to. Supported formats: JPEG, PNG, WebP, BMP, TIFF. |
mask_file | File | Yes | The mask image file (grayscale). Should be a grayscale or binary image where white areas (255) are fully visible, black areas (0) are fully transparent, and gray areas are partially transparent. Supported formats: JPEG, PNG, WebP, BMP, TIFF. |
invert_mask | Boolean | No | Whether to invert the mask (black becomes visible, white becomes transparent). Default: false . |
background_color | String | No | The background color to show in transparent areas. Can be a color name (e.g., white , transparent ) or a hex code (e.g., #FFFFFF ). Default: transparent . |
output_format | String | No | The format of the output image. Options: png , webp . Default: png . |
resize_mask | Boolean | No | Whether to automatically resize the mask to match the dimensions of the input image. Default: true . |
Response
Returns the image with the mask applied, in the specified output format.
Example Request
// example.sh
curl -X POST "https://oyyi.xyz/api/image/apply-mask" -H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "base_file=@image.jpg" \
-F "mask_file=@mask.png" \
--output masked.png
Example with Python
// apply_mask.py
import requests
url = "https://oyyi.xyz/api/image/apply-mask"
files = {
'base_file': open('image.jpg', 'rb'),
'mask_file': open('mask.png', 'rb')
}
data = {}
response = requests.post(url, files=files, data=data)
# Save the masked image
with open('masked.png', 'wb') as f:
f.write(response.content)
Example with JavaScript
// apply_mask.js
// Using fetch API
const formData = new FormData();
formData.append('image', imageInput.files[0]);
formData.append('mask', maskInput.files[0]);
formData.append('invert_mask', 'false');
fetch('https://oyyi.xyz/api/image/apply-mask', {
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 = 'masked.png';
a.click();
// Clean up by revoking the URL
URL.revokeObjectURL(url);
})
.catch(error => console.error('Error:', error));
Mask Types
Binary Mask
Contains only black and white pixels. Creates a sharp edge between visible and invisible areas. Good for precise cutouts.
Grayscale Mask
Contains varying shades of gray. Creates smooth transitions between visible and invisible areas. Good for soft edges and gradual fades.
How It Works
The apply-mask endpoint uses an alpha mask to control the visibility of different parts of an image:
- Image Loading: Both the input image and mask are loaded.
- Mask Preparation: If resize_mask is true, the mask is resized to match the dimensions of the input image.
- Mask Inversion (Optional): If invert_mask is true, the mask is inverted (255 becomes 0, 0 becomes 255).
- Alpha Channel Creation: The mask is used to create an alpha channel for the input image, where:
- White areas in the mask (255) correspond to fully visible areas in the output.
- Black areas in the mask (0) correspond to fully transparent areas in the output.
- Gray areas in the mask correspond to partially transparent areas in the output.
- Background Application: The specified background color is applied to transparent areas.
- Output Generation: The final image is generated in the specified output format.
The result is an image where certain parts are visible or transparent based on the mask.
Mask Creation Tips
Creating effective masks:
- Use image editing software like Photoshop, GIMP, or online tools to create masks.
- For binary masks, use the selection tools and fill with black and white.
- For grayscale masks, use gradient tools or blur effects on binary masks.
- Save masks as PNG files to preserve the exact black and white values.
- For complex subjects, consider using AI-powered background removal tools to generate masks.
Error Responses
Status Code | Description |
---|---|
400 | Bad request. Missing required parameters, invalid invert_mask value, invalid background_color, invalid output_format, invalid resize_mask value, 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
- The mask should ideally be a grayscale image where the intensity of each pixel determines the transparency level.
- If the mask has color channels, it will be converted to grayscale before being applied.
- For transparent backgrounds, use PNG or WebP as the output format. JPEG does not support transparency.
- If the mask and image have different dimensions and resize_mask is false, the mask will be applied from the top-left corner, and any areas outside the mask will be fully visible.
- The maximum file size allowed for each image is 10MB.
- For the best results, create masks with smooth edges to avoid jagged or pixelated transitions.
Use Cases
- Removing backgrounds from product images
- Creating cutouts of subjects for compositing
- Applying vignette effects with custom shapes
- Creating spotlight or focus effects
- Generating transparent PNG images for web use
- Creating fade effects between images
- Masking out unwanted elements in photos
- Creating shaped frames for images