mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 21:37:11 +00:00
feat: support dalle image variation
This commit is contained in:
parent
38de2ce714
commit
5fceef37ec
5 changed files with 127 additions and 6 deletions
|
|
@ -1,6 +1,8 @@
|
|||
import { Configuration, OpenAIApi } from 'openai'
|
||||
import { Config } from './config.js'
|
||||
|
||||
import fs from 'fs'
|
||||
import { mkdirs } from './common.js'
|
||||
import sharp from 'sharp'
|
||||
export async function createImage (prompt, n = 1, size = '512x512') {
|
||||
const configuration = new Configuration({
|
||||
apiKey: Config.apiKey
|
||||
|
|
@ -17,3 +19,60 @@ export async function createImage (prompt, n = 1, size = '512x512') {
|
|||
})
|
||||
return response.data.data?.map(pic => pic.b64_json)
|
||||
}
|
||||
|
||||
export async function imageVariation (imageUrl, n = 1, size = '512x512') {
|
||||
const configuration = new Configuration({
|
||||
apiKey: Config.apiKey
|
||||
})
|
||||
const openai = new OpenAIApi(configuration)
|
||||
if (Config.debug) {
|
||||
logger.info({ imageUrl, n, size })
|
||||
}
|
||||
const imageResponse = await fetch(imageUrl)
|
||||
const fileType = imageResponse.headers.get('Content-Type').split('/')[1]
|
||||
let fileLoc = `data/chatgpt/imagesAccept/${Date.now()}.${fileType}`
|
||||
mkdirs('data/chatgpt/imagesAccept')
|
||||
const blob = await imageResponse.blob()
|
||||
const arrayBuffer = await blob.arrayBuffer()
|
||||
const buffer = Buffer.from(arrayBuffer)
|
||||
await fs.writeFileSync(fileLoc, buffer)
|
||||
|
||||
let croppedFileLoc = `data/chatgpt/imagesAccept/${Date.now()}_cropped.png`
|
||||
await resizeAndCropImage(fileLoc, croppedFileLoc, 512)
|
||||
|
||||
const response = await openai.createImageVariation(
|
||||
fs.createReadStream(croppedFileLoc),
|
||||
n,
|
||||
size,
|
||||
'b64_json'
|
||||
)
|
||||
if (response.status !== 200) {
|
||||
console.log(response.data.error)
|
||||
}
|
||||
await fs.unlinkSync(fileLoc)
|
||||
await fs.unlinkSync(croppedFileLoc)
|
||||
return response.data.data?.map(pic => pic.b64_json)
|
||||
}
|
||||
|
||||
async function resizeAndCropImage (inputFilePath, outputFilePath, size = 512) {
|
||||
// Determine the maximum dimension of the input image
|
||||
const metadata = await sharp(inputFilePath).metadata()
|
||||
const maxDimension = Math.max(metadata.width, metadata.height)
|
||||
logger.mark(`original picture size is ${metadata.width} x ${metadata.height}`)
|
||||
// Calculate the required dimensions for the output image
|
||||
const outputWidth = size * metadata.width / maxDimension
|
||||
const outputHeight = size * metadata.height / maxDimension
|
||||
|
||||
// Resize the image to the required dimensions
|
||||
await sharp(inputFilePath)
|
||||
.resize(outputWidth, outputHeight, {
|
||||
fit: 'contain',
|
||||
background: { r: 255, g: 255, b: 255, alpha: 1 }
|
||||
})
|
||||
.resize(size, size, { fit: 'cover', position: 'center' })
|
||||
.png()
|
||||
.toFile(outputFilePath)
|
||||
console.log('Image resized successfully!')
|
||||
|
||||
console.log('Image resized and cropped successfully!')
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue