Convert Image Format
Convert an image from one format to another with control over quality, compression, and other format-specific options.
POST
/api/image/convert
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
file | File | Yes | The image file to convert. Supported input formats: JPEG, PNG, WebP, BMP, TIFF, GIF, HEIC, AVIF. |
target_format | String | Yes | Target format. Allowed values: "PNG", "JPEG", "WEBP", "BMP", "TIFF". |
quality | Integer | No | The quality level for lossy formats (JPEG, WebP). Range: 1-100. Default: 85 . |
optimize | Boolean | No | Whether to apply additional optimization to reduce file size. Default: true . |
lossless | Boolean | No | For WebP format only: whether to use lossless compression. Default: false . |
compression_level | Integer | No | For PNG format only: the compression level. Range: 0-9. Default: 6 . |
progressive | Boolean | No | For JPEG format only: whether to create a progressive JPEG. Default: false . |
strip_metadata | Boolean | No | Whether to remove EXIF and other metadata from the image. Default: false . |
Response
Returns the image converted to the specified format.
Example Request
// example.sh
curl -X POST "https://oyyi.xyz/api/image/convert" -H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@image.jpg" \
-F "target_format=WEBP" \
--output converted.webp
Example with Python
// convert.py
import requests
url = "https://oyyi.xyz/api/image/convert"
files = {
'file': open('image.jpg', 'rb')
}
data = {
'target_format': 'WEBP'
}
response = requests.post(url, files=files, data=data)
# Save the converted image
with open('converted.webp', 'wb') as f:
f.write(response.content)
Example with JavaScript
// convert.js
// Using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('format', 'webp');
formData.append('quality', '85');
fetch('https://oyyi.xyz/api/image/convert', {
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 = 'converted.webp';
a.click();
// Clean up by revoking the URL
URL.revokeObjectURL(url);
})
.catch(error => console.error('Error:', error));
Format Comparison
Format | Transparency | Compression | File Size | Best For |
---|---|---|---|---|
JPEG | No | Lossy | Small | Photographs, complex images with many colors |
PNG | Yes | Lossless | Medium to Large | Graphics with transparency, screenshots, images with text |
WebP | Yes | Lossy or Lossless | Very Small | Web images, best balance of quality and size |
BMP | Limited | None | Very Large | Simple graphics, compatibility with older systems |
TIFF | Yes | Various | Large | Print, publishing, archival, high-quality images |
GIF | Yes | Lossless | Medium | Animations, simple graphics with few colors |
How It Works
The convert endpoint changes the format of an image:
- Image Loading: The uploaded image is loaded into memory, automatically detecting its format.
- Format Conversion: The image is converted to the target format specified by the format parameter.
- Quality Setting: For lossy formats (JPEG, WebP), the specified quality level is applied.
- Format-Specific Options: Additional options are applied based on the target format:
- For WebP: lossless compression if specified
- For PNG: compression level for file size optimization
- For JPEG: progressive encoding if specified
- Optimization: If optimize is true, additional algorithms are applied to reduce file size without significant quality loss.
- Metadata Handling: If strip_metadata is true, EXIF and other metadata are removed from the image.
The result is an image in the requested format, optimized according to the specified parameters.
Format-Specific Options
JPEG Options
- quality: Controls compression level (1-100). Higher values preserve more detail but result in larger files.
- progressive: Creates a progressive JPEG that loads gradually, showing a low-resolution version first.
PNG Options
- compression_level: Controls the compression effort (0-9). Higher values result in smaller files but slower encoding.
- optimize: Applies additional optimization algorithms to reduce file size.
WebP Options
- quality: Controls compression level for lossy WebP (1-100).
- lossless: Uses lossless compression instead of lossy, preserving all details but resulting in larger files.
General Options
- strip_metadata: Removes EXIF data, color profiles, and other metadata to reduce file size.
- optimize: Applies format-specific optimizations to reduce file size.
Error Responses
Status Code | Description |
---|---|
400 | Bad request. Missing required parameters, invalid format value, invalid quality value, invalid compression_level, invalid boolean parameters, or unsupported input file format. |
413 | Payload too large. The file size exceeds the maximum allowed limit. |
415 | Unsupported media type. The input file is not a valid image or is in an unsupported format. |
500 | Internal server error. Something went wrong on the server. |
Notes
- When converting from a format with transparency (PNG, WebP) to a format without transparency (JPEG), transparent areas will be filled with a white background by default.
- Converting from a higher bit depth to a lower one (e.g., 16-bit TIFF to 8-bit PNG) may result in some loss of color information.
- GIF to GIF conversion preserves animation if the input is an animated GIF.
- Converting to WebP is recommended for web use as it typically provides the best balance of quality and file size.
- The strip_metadata option is useful for privacy (removing location data) and for reducing file size.
- Progressive JPEGs are useful for web pages as they allow the image to be displayed gradually as it loads.
- The maximum file size allowed is 50MB.
- For the best quality when converting from lossy formats (JPEG), consider using a high quality setting to minimize additional quality loss.
Use Cases
- Converting images to WebP for better web performance
- Converting transparent PNGs to JPEGs with a white background for printing
- Converting high-quality TIFF images to web-friendly formats
- Converting between formats for compatibility with specific applications
- Optimizing images for different use cases (web, print, storage)
- Removing metadata for privacy or file size reduction
- Converting HEIC photos from iOS devices to more widely supported formats
- Creating progressive JPEGs for better perceived loading performance on websites