canvas教程

Python flask wtforms从画布dataurl而不是form.input.data读取图

字号+ 作者:H5之家 来源:H5之家 2017-12-19 17:00 我要评论( )

Python flask wtforms从画布dataurl而不是form.input.data读取图像(Python flask wtforms read image from canvas dataurl instead of form.input.data) - IT屋-

问 题

I have currently a working JS code to resize images clientside. Now I need to send this resized image to my flask app. My flask app will then upload it to aws3.

This is the JS code which I use to resize the image, it also generates a dataurl:

$( "input[type='file']" ).change(function() { // from an input element var filesToUpload = this.files; console.log(filesToUpload); var img = document.createElement("img"); img.src = window.URL.createObjectURL(this.files[0]); $( img ).load(function() { var MAX_WIDTH = 600; var MAX_HEIGHT = 450; var width = img.width; var height = img.height; if (width > height) { if (width > MAX_WIDTH) { height *= MAX_WIDTH / width; width = MAX_WIDTH; } } else { if (height > MAX_HEIGHT) { width *= MAX_HEIGHT / height; height = MAX_HEIGHT; } } canvas.width = width; canvas.height = height; var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, width, height); var dataurl = canvas.toDataURL("image/png"); //var file = canvas.mozGetAsFile("foo.png"); }); });

In my flask app I used form.company_logo_image_path.data.read() to get the filecontents, but now even if I resize the resized image is ignored. That is because I am still getting the old image from the input. I need to get the canvas image.

How do I use the dataurl to get the image in my flask app? Or is there an other way?

That is my AJAX code which I use right after I get the dataurl:

$.ajax({ type: "POST", url: "/profil/unternehmen-bearbeiten", data:{ imageBase64: dataurl } }).done(function() { console.log('sent'); });

In my python view I try to access the dataurl:

data_url = request.values['imageBase64'] #data_url = request.args.get('image') # here parse the data_url out ?image={dataURL} print data_url content = data_url.split(';')[1] image_encoded = content.split(',')[1] body = base64.decodebytes(image_encoded.encode('utf-8')) # Then do whatever you want print body, type(body)

解决方案

per https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL, you get something like:

var canvas = document.getElementById('canvas'); var dataURL = canvas.toDataURL(); console.log(dataURL); // "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNby // blAAAADElEQVQImWNgoBMAAABpAAFEI8ARAAAAAElFTkSuQmCC"

Then just use base64 decode the file out.

import base64 from flask import request def your_view(): data_url = request.args.get('image') # here parse the data_url out ?image={dataURL} content = data_url.split(';')[1] image_encoded = content.split(',')[1] body = base64.decodebytes(image_encoded.encode('utf-8')) # Then do whatever you want

body is what you need.

本文地址:IT屋 » Python flask wtforms read image from canvas dataurl instead of form.input.data

问 题

我目前有一个工作的JS代码来调整客户端的图像大小。现在我需要发送这个调整大小的图像到我的烧瓶应用程序。我的烧瓶应用程序然后将它上传到aws3。

这是我用来调整图像大小的JS代码,它也生成一个dataurl:



$(“input [type ='file']”).change(function(){

// from输入元素
var filesToUpload = this.files;
console.log(filesToUpload);

var img = document.createElement(“img”);
img。 src = window.URL.createObjectURL(this.files [0]);

$(img).load(function(){

var MAX_WIDTH = 600;
var MAX_HEIGHT = 450;
var width = img.width;
var height = img.height;

if(width> height){
if (width> MAX_WIDTH){
height * = MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if(height> MAX_HEIGHT){
width * = MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}

canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext(“2d”);
ctx.drawImage(img,0,0,width,height);

var dataurl = canvas.toDataURL(“image / png”);
// var file = canvas.mozGetAsFile(“foo.png”);

});

});


在我的应用程序中,我使用了 form.company_logo_image_path.data.read() 来获取文件内容,但现在即使我调整大小调整后的图像被忽略。那是因为我仍然从输入中得到旧图像。我需要获取画布图像。

如何使用dataurl在我的烧瓶应用程序中获取图像?
或者还有其他的方法吗?

这是我在获取dataurl后使用的AJAX代码:

$。ajax({
type:“POST”,
url:“/ profil / unternehmen-bearbeiten”,
data :{
imageBase64:dataurl
}
})。done(function(){
console.log('sent');
});


在我的python视图中,我尝试访问dataurl:

data_url = request.values ['imageBase64']
#data_url = request.args.get('image')#here解析data_url out http:/ / xxxxx /?image = {dataURL}
print data_url
content = data_url.split(';')[1]
image_encoded = content.split(',')[1]
ody = base64.decodebytes(image_encoded.encode('utf-8'))
#然后做任何你想要的
打印正文,类型(正文)

解决方案

per https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL ,你会得到如下所示:



var canvas = document.getElementById('canvas');
var dataURL = canvas.toDataURL();
console.log(dataURL);
//“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNby
// blAAAADElEQVQImWNgoBMAAABpAAFEI8ARAAAAAElFTkSuQmCC”




导入base64 $ b $从烧瓶导入请求

def your_view():
data_url = request.args.get('image')#这里解析data_url出http:// xxxxx /?image = {dataURL}
content = data_url .split(';')[1]
image_encoded = content.split(',')[1]
body = base64.decodebytes(image_encoded.encode('utf-8'))
#然后做任何你想要的


body是你需要的。 b

本文地址:IT屋 » Python flask wtforms从画布dataurl而不是form.input.data读取图像

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 微信小程序,小程序论坛,weixin,微信小程序canvas绘制坐标图

    微信小程序,小程序论坛,weixin,微信小程序canvas绘制坐标图

    2017-12-19 12:02

  • 关于canvas处理图片的问题

    关于canvas处理图片的问题

    2017-12-19 10:22

  • 怎么样使用Canvas绘制七巧板?

    怎么样使用Canvas绘制七巧板?

    2017-12-19 10:22

  • HTML 5 Canvas (HTML5 参考手册)

    HTML 5 Canvas (HTML5 参考手册)

    2017-12-19 08:05

网友点评