python画布加载图片的程序_用python keras加载的JPG与用javascript加载的JPG不同

在将画布呈现为张量之前,您正在画布上绘制图像。在画布上绘制可以改变初始图像的形状。例如,除非另有规定(代码就是这样),否则画布的创建宽度为300 px,高度为150 px。因此张量的结果形状或多或少类似于下面的[150,300,3]。

1 -使用画布

画布适合调整图像的大小,因为可以在画布上绘制初始图像的全部或部分。在这种情况下,需要调整画布的大小。

const canvas = document.create('canvas')

// canvas has initial width: 300px and height: 150px

canvas.width = image.width

canvas.height = image.heigth

// canvas is set to redraw the initial image

const ctx = canvas.getContext('2d')

ctx.drawImage(image, 0, 0) // to draw the entire image

不过,有一点要注意:以上所有操作都应该在使用事件处理程序加载完图像之后执行。

onload

如下所示

const im = new Image()

im.crossOrigin = 'anonymous'

im.src = 'url'

// document.body.appendChild(im) (optional if the image should be displayed)

im.onload = () => {

const canvas = document.create('canvas')

canvas.width = image.width

canvas.height = image.heigth

const ctx = canvas.getContext('2d')

ctx.drawImage(image, 0, 0)

}

或者使用异步/等待

function load(url){

return new Promise((resolve, reject) => {

const im = new Image()

im.crossOrigin = 'anonymous'

im.src = 'url'

im.onload = () => {

resolve(im)

}

})

}

// use the load function inside an async function

(async() => {

const image = await load(url)

const canvas = document.create('canvas')

canvas.width = image.width

canvas.height = image.heigth

const ctx = canvas.getContext('2d')

ctx.drawImage(image, 0, 0)

})()

2-直接在图像上使用frompixel

如果图像不需要调整大小,可以使用

fromPixel

在图像本身

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值