将一张图像叠加在另一张图像上
Overlay one image over another
我正在尝试将 1 张图像覆盖在另一张图像之上,但似乎无法解决 - 我的代码没有抛出任何错误,但没有输出请求的图像修改。有人能指出我正确的方向吗?
这是我的代码
const user = message.mentions.users.first();
if (args[0] === undefined) {
message.channel.send("You can't jail yourself, dummy!")
} else {
var images = [user.avatarURL({ format: 'png', dynamic: true, size: 256 }), 'https://i.pinimg.com/originals/7b/51/9a/7b519a3422f940011d34d1f9aa75f683.png']
var jimps = []
//turns the images into readable variables for jimp, then pushes them into a new array
for (var i = 0; i < images.length; i++){
jimps.push(jimp.read(images[i]))
}
//creates a promise to handle the jimps
await Promise.all(jimps).then(function(data) {
return Promise.all(jimps)
}).then(async function(data){
// --- THIS IS WHERE YOU MODIFY THE IMAGES --- \
data[0].composite(data[1], 0, 0) //adds the second specified image (the jail bars) on top of the first specified image (the avatar). "0, 0" define where the second image is placed, originating from the top left corner
//you CAN resize the second image to fit the first one like this, if necessary. The "100, 100" is the new size in pixels.
data[1].resize(100,100)
//this saves our modified image
data[0].write(`\Users\jmoor\Pictures\JIMP Test\test.png`)
})
message.channel.send(`${user.username} has been jailed!`, {file: `\Users\jmoor\Pictures\JIMP Test\test.png`})
}
我已经在上面定义了 jimp,并且还在使用我制作的命令处理程序。
使用这个:
const user = message.mentions.users.first() //get The first user mentioned
if (!user) return message.reply("Who do you wanna send to jail?")//return if no user was mentioned
var bars = "https://i.pinimg.com/originals/7b/51/9a/7b519a3422f940011d34d1f9aa75f683.png"
var pfp = user.avatarURL({ format: 'png', dynamic: true, size: 128 }) //get link of profile picture
var image = await Jimp.read(pfp)//read the profile picture, returns a Jimp object
//Composite resized bars on profile picture
image.composite((await Jimp.read(bars)).resize(128, 128), 0, 0)
//create and attachment using buffer from edited picture and sending it
var image = new Discord.MessageAttachment(await image.getBufferAsync(Jimp.MIME_PNG))
message.reply(image)
我正在尝试将 1 张图像覆盖在另一张图像之上,但似乎无法解决 - 我的代码没有抛出任何错误,但没有输出请求的图像修改。有人能指出我正确的方向吗?
这是我的代码
const user = message.mentions.users.first();
if (args[0] === undefined) {
message.channel.send("You can't jail yourself, dummy!")
} else {
var images = [user.avatarURL({ format: 'png', dynamic: true, size: 256 }), 'https://i.pinimg.com/originals/7b/51/9a/7b519a3422f940011d34d1f9aa75f683.png']
var jimps = []
//turns the images into readable variables for jimp, then pushes them into a new array
for (var i = 0; i < images.length; i++){
jimps.push(jimp.read(images[i]))
}
//creates a promise to handle the jimps
await Promise.all(jimps).then(function(data) {
return Promise.all(jimps)
}).then(async function(data){
// --- THIS IS WHERE YOU MODIFY THE IMAGES --- \
data[0].composite(data[1], 0, 0) //adds the second specified image (the jail bars) on top of the first specified image (the avatar). "0, 0" define where the second image is placed, originating from the top left corner
//you CAN resize the second image to fit the first one like this, if necessary. The "100, 100" is the new size in pixels.
data[1].resize(100,100)
//this saves our modified image
data[0].write(`\Users\jmoor\Pictures\JIMP Test\test.png`)
})
message.channel.send(`${user.username} has been jailed!`, {file: `\Users\jmoor\Pictures\JIMP Test\test.png`})
}
我已经在上面定义了 jimp,并且还在使用我制作的命令处理程序。
使用这个:
const user = message.mentions.users.first() //get The first user mentioned
if (!user) return message.reply("Who do you wanna send to jail?")//return if no user was mentioned
var bars = "https://i.pinimg.com/originals/7b/51/9a/7b519a3422f940011d34d1f9aa75f683.png"
var pfp = user.avatarURL({ format: 'png', dynamic: true, size: 128 }) //get link of profile picture
var image = await Jimp.read(pfp)//read the profile picture, returns a Jimp object
//Composite resized bars on profile picture
image.composite((await Jimp.read(bars)).resize(128, 128), 0, 0)
//create and attachment using buffer from edited picture and sending it
var image = new Discord.MessageAttachment(await image.getBufferAsync(Jimp.MIME_PNG))
message.reply(image)