Crop Image
Extract a specific rectangular region from an image by specifying coordinates and dimensions.
/api/image/crop
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
file | File | Yes | The image file to crop. Supported formats: JPEG, PNG, WebP, BMP, TIFF, GIF. |
x | Integer | Yes | The x-coordinate of the top-left corner of the crop region. Range: 0 to image width-1. |
y | Integer | Yes | The y-coordinate of the top-left corner of the crop region. Range: 0 to image height-1. |
width | Integer | Yes | The width of the crop region in pixels. Range: 1 to image width. |
height | Integer | Yes | The height of the crop region in pixels. Range: 1 to image height. |
quality | Integer | No | The quality of the output image for lossy formats (JPEG, WebP). Range: 1-100. Default: 90 . |
smart_crop | Boolean | No | Whether to use content-aware cropping to automatically determine the best crop region. If true, x, y, width, and height parameters are used as constraints rather than exact coordinates. Default: false . |
Response
Returns the cropped image in the specified or original format.
Example Request
// example.sh
curl -X POST "https://oyyi.xyz/api/image/crop" -H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@image.jpg" \
-F "x=100" \
-F "y=100" \
-F "width=500" \
-F "height=300" \
--output cropped.jpg
Example with Python
// crop.py
import requests
url = "https://oyyi.xyz/api/image/crop"
files = {
'file': open('image.jpg', 'rb')
}
data = {
'x': 100,
'y': 100,
'width': 500,
'height': 300
}
response = requests.post(url, files=files, data=data)
# Save the cropped image
with open('cropped.jpg', 'wb') as f:
f.write(response.content)
Example with JavaScript
// crop.js
// Using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('x', '100');
formData.append('y', '100');
formData.append('width', '500');
formData.append('height', '300');
fetch('https://oyyi.xyz/api/image/crop', {
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 = 'cropped.jpg';
a.click();
// Clean up by revoking the URL
URL.revokeObjectURL(url);
})
.catch(error => console.error('Error:', error));
Coordinate System
The image coordinate system starts at the top-left corner (0,0) and extends to the bottom-right corner (width-1, height-1):
(0,0) (x,y) +----------+----------+ | | | | | Crop | | | Region | +----------+----------+ | | | | | | | | | +----------+----------+ (width-1, height-1)
The crop region is defined by its top-left corner (x,y) and its dimensions (width, height).
Smart Cropping
Standard Cropping
Uses the exact coordinates and dimensions provided. The crop region is precisely defined by the x, y, width, and height parameters.
Smart Cropping
Uses content-aware algorithms to identify the most important parts of the image. The provided coordinates and dimensions are used as constraints, but the final crop region may be adjusted to include important content.
How It Works
The crop endpoint extracts a rectangular region from an image:
- Image Loading: The uploaded image is loaded into memory.
- Coordinate Validation: The provided coordinates and dimensions are validated to ensure they fall within the image boundaries.
- Smart Crop (Optional): If smart_crop is enabled, content-aware algorithms analyze the image to identify important features (faces, objects, etc.) and adjust the crop region accordingly.
- Region Extraction: The specified rectangular region is extracted from the image.
- Format Conversion: If an output_format is specified, the cropped image is converted to that format.
- Quality Setting: For lossy formats (JPEG, WebP), the specified quality level is applied.
The result is a new image containing only the specified portion of the original image.
Boundary Handling
If the crop region extends beyond the image boundaries:
- If x or y is negative, they will be set to 0.
- If x + width exceeds the image width, width will be reduced to fit within the image.
- If y + height exceeds the image height, height will be reduced to fit within the image.
- If the resulting crop region has zero width or height, an error will be returned.
Error Responses
Status Code | Description |
---|---|
400 | Bad request. Missing required parameters, invalid coordinate values, invalid dimensions, invalid output_format, invalid quality value, invalid smart_crop 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 coordinate system starts at (0,0) in the top-left corner of the image.
- For precise cropping, it's helpful to know the dimensions of the original image beforehand. You can use the metadata endpoint to get this information.
- Smart cropping is useful for automatically focusing on the important parts of an image, such as faces in portraits or subjects in photographs.
- When cropping transparent images (PNG, WebP), the transparency is preserved in the output if the output format supports it.
- For animated GIFs, only the first frame will be cropped unless the output format is also GIF.
- The maximum file size allowed is 50MB.
- This operation preserves the alpha channel (transparency) if present in the original image and the output format supports it (PNG, WebP, GIF).
Use Cases
- Removing unwanted elements from the edges of an image
- Extracting a specific subject or region of interest
- Creating thumbnails focused on the most important part of an image
- Standardizing image aspect ratios for consistent layouts
- Preparing images for social media platforms with specific dimension requirements
- Removing metadata-containing borders from scanned documents
- Creating detail views of larger images
- Implementing image editing functionality in applications