Convert Base64 to Image
Convert a Base64-encoded string back to an image file in various formats.
/api/image/from-base64
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
base64_str | String | Yes | The Base64-encoded string to convert to an image. Can be a plain Base64 string or a complete data URI (e.g., "data:image/jpeg;base64,..."). |
output_format | String | No | The desired output format for the image. Options: jpeg , png , webp , bmp , tiff , gif . Default: Determined from the input if possible, otherwise png . |
quality | Integer | No | The quality level for JPEG or WebP compression. Range: 1-100. Default: 90 . |
Response
Returns the binary image data in the specified format. The Content-Type header will be set to the appropriate MIME type for the image format.
Example Request
// example.sh
curl -X POST "https://oyyi.xyz/api/image/from-base64" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d '{
"base64_str": "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/...",
"output_format": "png"
}' \
--output converted_image.png
Example with Python
// from_base64.py
import requests
import base64
url = "https://oyyi.xyz/api/image/from-base64"
# Example: Read a Base64 string from a text file
with open('base64_image.txt', 'r') as f:
base64_string = f.read().strip()
# Or use a Base64 string directly
# base64_string = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/..."
data = {
"base64_str": base64_string,
"output_format": "png"
}
response = requests.post(url, json=data)
# Save the converted image
with open('converted_image.png', 'wb') as f:
f.write(response.content)
Example with JavaScript
// from_base64.js
// Using fetch API
const base64String = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/...";
fetch('https://oyyi.xyz/api/image/from-base64', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
base64_str: base64String,
output_format: 'png'
})
})
.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_image.png';
a.click();
// Clean up by revoking the URL
URL.revokeObjectURL(url);
})
.catch(error => console.error('Error:', error));
Supported Output Formats
JPEG
Lossy compression, good for photographs, no transparency support. Smaller file size.
PNG
Lossless compression, good for graphics with sharp edges and transparency. Larger file size.
WebP
Modern format with both lossy and lossless compression, supports transparency. Smaller file size than JPEG or PNG.
BMP
Uncompressed format, simple structure. Very large file size.
TIFF
Flexible format with various compression options, good for high-quality images and print.
GIF
Limited to 256 colors, supports animation and transparency. Good for simple animations and icons.
How It Works
The from-base64 endpoint converts a Base64-encoded string back to an image file:
- Input Parsing: The Base64 string is parsed, removing any data URI prefix if present.
- MIME Type Detection: If the input is a data URI, the MIME type is extracted to determine the original format.
- Base64 Decoding: The Base64 string is decoded back to binary data.
- Image Loading: The binary data is loaded as an image.
- Format Conversion: If an output_format is specified, the image is converted to that format.
- Quality Adjustment: For JPEG or WebP output, the quality parameter is applied.
- Response Generation: The image is returned as binary data with the appropriate Content-Type header.
The result is an image file that can be saved, displayed, or further processed.
Data URI Format
The endpoint accepts both plain Base64 strings and data URIs:
- Plain Base64:
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/...
- Data URI:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/...
If a data URI is provided, the MIME type will be used to determine the original format if no output_format is specified.
Error Responses
Status Code | Description |
---|---|
400 | Bad request. Missing required parameters, invalid base64 string, invalid output_format, or invalid quality value. |
413 | Payload too large. The decoded image size exceeds the maximum allowed limit. |
415 | Unsupported media type. The Base64 string does not represent a valid image. |
500 | Internal server error. Something went wrong on the server. |
Notes
- The endpoint automatically handles both plain Base64 strings and complete data URIs.
- If the Base64 string contains padding characters (=), they can be included or omitted.
- When converting between formats, some information may be lost. For example, converting from PNG to JPEG will lose transparency.
- The quality parameter only affects JPEG and WebP output formats. It is ignored for other formats.
- The maximum decoded image size allowed is 50MB.
- For animated GIFs or multi-frame images, only the first frame will be processed.
- If the Base64 string is invalid or does not represent an image, a 400 or 415 error will be returned.
Use Cases
- Converting Base64-encoded images from APIs back to image files
- Saving images embedded in HTML or CSS as separate files
- Converting between image formats (e.g., from JPEG to PNG)
- Extracting images from data URIs
- Processing Base64-encoded images from form submissions
- Saving images from canvas elements in web applications
- Converting Base64-encoded images from databases to file storage
- Implementing "Save As" functionality for Base64-embedded images