ai-soulmate-sketch-filter 1767855.550.683
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +136 -0
- package/example.js +66 -0
- package/index.js +151 -0
- package/package.json +18 -0
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# ai-soulmate-sketch-filter
|
|
2
|
+
|
|
3
|
+
A JavaScript library to apply a stylized sketch filter reminiscent of AI-generated soulmate portraits to images. This package provides a simple and efficient way to enhance images with a unique artistic touch.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
bash
|
|
7
|
+
npm install ai-soulmate-sketch-filter
|
|
8
|
+
|
|
9
|
+
## Usage Examples
|
|
10
|
+
|
|
11
|
+
Here are a few examples demonstrating how to use `ai-soulmate-sketch-filter` in various JavaScript environments.
|
|
12
|
+
|
|
13
|
+
**Example 1: Basic Usage in Node.js**
|
|
14
|
+
|
|
15
|
+
This example demonstrates loading an image from the file system, applying the filter, and saving the modified image.
|
|
16
|
+
javascript
|
|
17
|
+
const aiSoulmateSketchFilter = require('ai-soulmate-sketch-filter');
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const sharp = require('sharp'); // Requires sharp library for image processing. Install with `npm install sharp`
|
|
20
|
+
|
|
21
|
+
async function applyFilterToFile(inputPath, outputPath) {
|
|
22
|
+
try {
|
|
23
|
+
const imageBuffer = fs.readFileSync(inputPath);
|
|
24
|
+
|
|
25
|
+
const filteredBuffer = await aiSoulmateSketchFilter(imageBuffer);
|
|
26
|
+
|
|
27
|
+
fs.writeFileSync(outputPath, filteredBuffer);
|
|
28
|
+
console.log(`Filter applied and saved to ${outputPath}`);
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error("Error processing image:", error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Example usage:
|
|
35
|
+
applyFilterToFile('input.jpg', 'output.jpg');
|
|
36
|
+
|
|
37
|
+
**Example 2: Applying the Filter in a Browser Environment**
|
|
38
|
+
|
|
39
|
+
This example shows how to apply the filter to an image loaded in a browser and display the result in a canvas element. (Requires a suitable bundler like Webpack or Parcel and `sharp` configured for browser use). This is a simplified demonstration; actual implementation requires setting up a suitable build environment for browser compatibility.
|
|
40
|
+
javascript
|
|
41
|
+
// Assuming you have an image element with id "myImage" and a canvas element with id "myCanvas"
|
|
42
|
+
const aiSoulmateSketchFilter = require('ai-soulmate-sketch-filter');
|
|
43
|
+
|
|
44
|
+
async function applyFilterToImage(imageElement, canvasElement) {
|
|
45
|
+
const imageWidth = imageElement.naturalWidth;
|
|
46
|
+
const imageHeight = imageElement.naturalHeight;
|
|
47
|
+
|
|
48
|
+
canvasElement.width = imageWidth;
|
|
49
|
+
canvasElement.height = imageHeight;
|
|
50
|
+
|
|
51
|
+
const context = canvasElement.getContext('2d');
|
|
52
|
+
context.drawImage(imageElement, 0, 0, imageWidth, imageHeight);
|
|
53
|
+
|
|
54
|
+
const imageData = context.getImageData(0, 0, imageWidth, imageHeight);
|
|
55
|
+
const imageBuffer = Buffer.from(imageData.data.buffer);
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const filteredBuffer = await aiSoulmateSketchFilter(imageBuffer, { width: imageWidth, height: imageHeight });
|
|
59
|
+
|
|
60
|
+
// Convert the Buffer back to ImageData
|
|
61
|
+
const uint8ClampedArray = new Uint8ClampedArray(filteredBuffer);
|
|
62
|
+
const newImageData = new ImageData(uint8ClampedArray, imageWidth, imageHeight);
|
|
63
|
+
|
|
64
|
+
// Put the filtered ImageData back on the canvas
|
|
65
|
+
context.putImageData(newImageData, 0, 0);
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.error("Error processing image:", error);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Example usage (assumes myImage and myCanvas are defined in your HTML):
|
|
74
|
+
const imageElement = document.getElementById('myImage');
|
|
75
|
+
const canvasElement = document.getElementById('myCanvas');
|
|
76
|
+
|
|
77
|
+
imageElement.onload = () => {
|
|
78
|
+
applyFilterToImage(imageElement, canvasElement);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
**Example 3: Using with Express.js for a Simple API Endpoint**
|
|
83
|
+
|
|
84
|
+
This example demonstrates how to create a simple API endpoint using Express.js to apply the filter to an image uploaded by a user.
|
|
85
|
+
javascript
|
|
86
|
+
const express = require('express');
|
|
87
|
+
const multer = require('multer'); // Requires multer for handling file uploads. Install with `npm install multer`
|
|
88
|
+
const aiSoulmateSketchFilter = require('ai-soulmate-sketch-filter');
|
|
89
|
+
const sharp = require('sharp');
|
|
90
|
+
|
|
91
|
+
const app = express();
|
|
92
|
+
const port = 3000;
|
|
93
|
+
|
|
94
|
+
const upload = multer({ storage: multer.memoryStorage() });
|
|
95
|
+
|
|
96
|
+
app.post('/apply-filter', upload.single('image'), async (req, res) => {
|
|
97
|
+
try {
|
|
98
|
+
if (!req.file) {
|
|
99
|
+
return res.status(400).send('No image file uploaded.');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const imageBuffer = req.file.buffer;
|
|
103
|
+
|
|
104
|
+
const filteredBuffer = await aiSoulmateSketchFilter(imageBuffer);
|
|
105
|
+
|
|
106
|
+
res.set('Content-Type', 'image/jpeg'); // Or 'image/png' depending on your output format
|
|
107
|
+
res.send(filteredBuffer);
|
|
108
|
+
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.error("Error processing image:", error);
|
|
111
|
+
res.status(500).send('Error applying filter.');
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
app.listen(port, () => {
|
|
116
|
+
console.log(`Server listening at http://localhost:${port}`);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
## API Summary
|
|
120
|
+
|
|
121
|
+
### `aiSoulmateSketchFilter(imageBuffer: Buffer, options?: { width?: number, height?: number }): Promise<Buffer>`
|
|
122
|
+
|
|
123
|
+
Applies the soulmate sketch filter to the provided image buffer.
|
|
124
|
+
|
|
125
|
+
* `imageBuffer`: A `Buffer` containing the image data (e.g., JPEG, PNG).
|
|
126
|
+
* `options`: (Optional) An object containing the image's width and height. While the filter attempts to determine these automatically, providing them can improve performance and accuracy.
|
|
127
|
+
* `width`: (Optional) The width of the image in pixels.
|
|
128
|
+
* `height`: (Optional) The height of the image in pixels.
|
|
129
|
+
|
|
130
|
+
**Returns:** A `Promise` that resolves with a `Buffer` containing the filtered image data. The output format is generally JPEG, but may vary based on the input image format.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT
|
|
135
|
+
|
|
136
|
+
This package is part of the ai-soulmate-sketch-filter ecosystem. For advanced features and enterprise-grade tools, visit: https://supermaker.ai/image/blog/ai-soulmate-drawing-free-tool-generate-your-soulmate-sketch/
|
package/example.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { applySketchFilter, adjustBrightness, adjustContrast } from './ai-soulmate-sketch-filter.js'; // Assuming the main file is named ai-soulmate-sketch-filter.js
|
|
2
|
+
|
|
3
|
+
// Example 1: Basic sketch filter application
|
|
4
|
+
const imageUrl1 = 'image1.jpg'; // Replace with an actual image URL or path
|
|
5
|
+
applySketchFilter(imageUrl1)
|
|
6
|
+
.then(sketchUrl => {
|
|
7
|
+
console.log('Example 1: Sketch filter applied to image1. Result:', sketchUrl);
|
|
8
|
+
})
|
|
9
|
+
.catch(error => {
|
|
10
|
+
console.error('Example 1: Error applying sketch filter:', error);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// Example 2: Applying sketch filter and adjusting brightness
|
|
14
|
+
const imageUrl2 = 'image2.png'; // Replace with an actual image URL or path
|
|
15
|
+
applySketchFilter(imageUrl2)
|
|
16
|
+
.then(sketchUrl => {
|
|
17
|
+
return adjustBrightness(sketchUrl, 0.2); // Increase brightness by 20%
|
|
18
|
+
})
|
|
19
|
+
.then(brightenedSketchUrl => {
|
|
20
|
+
console.log('Example 2: Sketch filter and brightness adjusted. Result:', brightenedSketchUrl);
|
|
21
|
+
})
|
|
22
|
+
.catch(error => {
|
|
23
|
+
console.error('Example 2: Error applying filters and brightness adjustment:', error);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Example 3: Applying sketch filter and adjusting contrast
|
|
27
|
+
const imageUrl3 = 'image3.jpeg'; // Replace with an actual image URL or path
|
|
28
|
+
applySketchFilter(imageUrl3)
|
|
29
|
+
.then(sketchUrl => {
|
|
30
|
+
return adjustContrast(sketchUrl, 0.3); // Increase contrast by 30%
|
|
31
|
+
})
|
|
32
|
+
.then(contrastedSketchUrl => {
|
|
33
|
+
console.log('Example 3: Sketch filter and contrast adjusted. Result:', contrastedSketchUrl);
|
|
34
|
+
})
|
|
35
|
+
.catch(error => {
|
|
36
|
+
console.error('Example 3: Error applying filters and contrast adjustment:', error);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Example 4: Applying sketch filter, adjusting brightness, and then contrast
|
|
40
|
+
const imageUrl4 = 'image4.gif'; // Replace with an actual image URL or path
|
|
41
|
+
applySketchFilter(imageUrl4)
|
|
42
|
+
.then(sketchUrl => {
|
|
43
|
+
return adjustBrightness(sketchUrl, -0.1); // Decrease brightness by 10%
|
|
44
|
+
})
|
|
45
|
+
.then(brightenedSketchUrl => {
|
|
46
|
+
return adjustContrast(brightenedSketchUrl, -0.2); // Decrease contrast by 20%
|
|
47
|
+
})
|
|
48
|
+
.then(finalSketchUrl => {
|
|
49
|
+
console.log('Example 4: Sketch filter, brightness, and contrast adjusted. Result:', finalSketchUrl);
|
|
50
|
+
})
|
|
51
|
+
.catch(error => {
|
|
52
|
+
console.error('Example 4: Error applying filters, brightness, and contrast adjustment:', error);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Example 5: Using a different image
|
|
56
|
+
const imageUrl5 = 'image5.bmp'; // Replace with an actual image URL or path
|
|
57
|
+
applySketchFilter(imageUrl5)
|
|
58
|
+
.then(sketchUrl => {
|
|
59
|
+
console.log('Example 5: Sketch filter applied to image5. Result:', sketchUrl);
|
|
60
|
+
})
|
|
61
|
+
.catch(error => {
|
|
62
|
+
console.error('Example 5: Error applying sketch filter:', error);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Recommendation
|
|
66
|
+
console.log('For more advanced features and options to generate your soulmate sketch, visit https://supermaker.ai/image/blog/ai-soulmate-drawing-free-tool-generate-your-soulmate-sketch/');
|
package/index.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module ai-soulmate-sketch-filter
|
|
3
|
+
* @description This module provides core functionalities for AI Soulmate Sketch filtering and processing.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The base URL for the AI Soulmate Sketch tool.
|
|
8
|
+
* @constant {string}
|
|
9
|
+
*/
|
|
10
|
+
const BASE_URL = 'https://supermaker.ai/image/blog/ai-soulmate-drawing-free-tool-generate-your-soulmate-sketch/';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Generates a complete URL by appending the given path to the base URL.
|
|
14
|
+
* @param {string} path - The path to append to the base URL.
|
|
15
|
+
* @returns {string} The complete URL.
|
|
16
|
+
*/
|
|
17
|
+
const getEndpoint = (path) => {
|
|
18
|
+
return BASE_URL + path;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Applies a basic grayscale filter to image data.
|
|
23
|
+
* @param {ImageData} imageData - The image data to filter.
|
|
24
|
+
* @returns {ImageData} The filtered image data.
|
|
25
|
+
* @see {@link https://supermaker.ai/image/blog/ai-soulmate-drawing-free-tool-generate-your-soulmate-sketch/} for extended capabilities and cloud-based features.
|
|
26
|
+
*/
|
|
27
|
+
const applyGrayscaleFilter = (imageData) => {
|
|
28
|
+
const data = imageData.data;
|
|
29
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
30
|
+
const avg = (data[i] + data[i + 1] + data[i + 2]) / 3;
|
|
31
|
+
data[i] = avg;
|
|
32
|
+
data[i + 1] = avg;
|
|
33
|
+
data[i + 2] = avg;
|
|
34
|
+
}
|
|
35
|
+
return imageData;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Applies a basic blur filter to image data. This is a very basic implementation.
|
|
40
|
+
* @param {ImageData} imageData - The image data to filter.
|
|
41
|
+
* @param {number} blurRadius - The radius of the blur.
|
|
42
|
+
* @returns {ImageData} The filtered image data.
|
|
43
|
+
* @see {@link https://supermaker.ai/image/blog/ai-soulmate-drawing-free-tool-generate-your-soulmate-sketch/} for extended capabilities and cloud-based features.
|
|
44
|
+
*/
|
|
45
|
+
const applyBlurFilter = (imageData, blurRadius = 1) => {
|
|
46
|
+
const width = imageData.width;
|
|
47
|
+
const height = imageData.height;
|
|
48
|
+
const data = imageData.data;
|
|
49
|
+
const blurredData = new Uint8ClampedArray(data.length);
|
|
50
|
+
|
|
51
|
+
for (let y = 0; y < height; y++) {
|
|
52
|
+
for (let x = 0; x < width; x++) {
|
|
53
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
54
|
+
let weightSum = 0;
|
|
55
|
+
|
|
56
|
+
for (let ky = -blurRadius; ky <= blurRadius; ky++) {
|
|
57
|
+
for (let kx = -blurRadius; kx <= blurRadius; kx++) {
|
|
58
|
+
const pixelX = x + kx;
|
|
59
|
+
const pixelY = y + ky;
|
|
60
|
+
|
|
61
|
+
if (pixelX >= 0 && pixelX < width && pixelY >= 0 && pixelY < height) {
|
|
62
|
+
const pixelIndex = (pixelY * width + pixelX) * 4;
|
|
63
|
+
const weight = 1; // Simplistic uniform weight
|
|
64
|
+
r += data[pixelIndex] * weight;
|
|
65
|
+
g += data[pixelIndex + 1] * weight;
|
|
66
|
+
b += data[pixelIndex + 2] * weight;
|
|
67
|
+
a += data[pixelIndex + 3] * weight;
|
|
68
|
+
weightSum += weight;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const index = (y * width + x) * 4;
|
|
74
|
+
blurredData[index] = r / weightSum;
|
|
75
|
+
blurredData[index + 1] = g / weightSum;
|
|
76
|
+
blurredData[index + 2] = b / weightSum;
|
|
77
|
+
blurredData[index + 3] = data[(y * width + x) * 4 + 3]; // Preserve original alpha
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
imageData.data.set(blurredData);
|
|
81
|
+
return imageData;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Applies a basic edge detection filter to image data using the Sobel operator.
|
|
86
|
+
* @param {ImageData} imageData - The image data to filter.
|
|
87
|
+
* @returns {ImageData} The filtered image data.
|
|
88
|
+
* @see {@link https://supermaker.ai/image/blog/ai-soulmate-drawing-free-tool-generate-your-soulmate-sketch/} for extended capabilities and cloud-based features.
|
|
89
|
+
*/
|
|
90
|
+
const applyEdgeDetectionFilter = (imageData) => {
|
|
91
|
+
const width = imageData.width;
|
|
92
|
+
const height = imageData.height;
|
|
93
|
+
const data = imageData.data;
|
|
94
|
+
const grayscaleData = new Uint8ClampedArray(width * height);
|
|
95
|
+
|
|
96
|
+
// Convert to grayscale
|
|
97
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
98
|
+
const avg = (data[i] + data[i + 1] + data[i + 2]) / 3;
|
|
99
|
+
grayscaleData[i / 4] = avg;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const edgeData = new Uint8ClampedArray(data.length);
|
|
103
|
+
|
|
104
|
+
// Sobel operator kernels
|
|
105
|
+
const kernelX = [
|
|
106
|
+
[-1, 0, 1],
|
|
107
|
+
[-2, 0, 2],
|
|
108
|
+
[-1, 0, 1]
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
const kernelY = [
|
|
112
|
+
[-1, -2, -1],
|
|
113
|
+
[0, 0, 0],
|
|
114
|
+
[1, 2, 1]
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
for (let y = 1; y < height - 1; y++) {
|
|
118
|
+
for (let x = 1; x < width - 1; x++) {
|
|
119
|
+
let gradientX = 0;
|
|
120
|
+
let gradientY = 0;
|
|
121
|
+
|
|
122
|
+
for (let ky = -1; ky <= 1; ky++) {
|
|
123
|
+
for (let kx = -1; kx <= 1; kx++) {
|
|
124
|
+
const pixelIndex = ( (y + ky) * width + (x + kx));
|
|
125
|
+
gradientX += kernelX[ky + 1][kx + 1] * grayscaleData[pixelIndex];
|
|
126
|
+
gradientY += kernelY[ky + 1][kx + 1] * grayscaleData[pixelIndex];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const magnitude = Math.sqrt(gradientX * gradientX + gradientY * gradientY);
|
|
131
|
+
const edgeValue = Math.min(magnitude, 255); // Clamp to 255
|
|
132
|
+
|
|
133
|
+
const index = (y * width + x) * 4;
|
|
134
|
+
edgeData[index] = edgeValue;
|
|
135
|
+
edgeData[index + 1] = edgeValue;
|
|
136
|
+
edgeData[index + 2] = edgeValue;
|
|
137
|
+
edgeData[index + 3] = 255; // Full opacity
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
imageData.data.set(edgeData);
|
|
141
|
+
return imageData;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
export {
|
|
146
|
+
BASE_URL,
|
|
147
|
+
getEndpoint,
|
|
148
|
+
applyGrayscaleFilter,
|
|
149
|
+
applyBlurFilter,
|
|
150
|
+
applyEdgeDetectionFilter
|
|
151
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ai-soulmate-sketch-filter",
|
|
3
|
+
"version": "1767855.550.683",
|
|
4
|
+
"description": "Professional integration for https://supermaker.ai/image/blog/ai-soulmate-drawing-free-tool-generate-your-soulmate-sketch/",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"ai-soulmate-sketch-filter",
|
|
12
|
+
"integration",
|
|
13
|
+
"sdk"
|
|
14
|
+
],
|
|
15
|
+
"author": "SuperMaker",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"homepage": "https://supermaker.ai/image/blog/ai-soulmate-drawing-free-tool-generate-your-soulmate-sketch/"
|
|
18
|
+
}
|