javascript - 如何用python和js处理图像数据

标签 javascript python image

我需要一些帮助来使 python 和 JS 协同工作。互联网上已经有关于它的答案,但我无法让它发挥作用。让我解释一下问题:

我有一个图像要分析,实际上是获取某种颜色的所有像素的坐标,并将这些坐标存储在一个数组中。这个脚本非常简单并且运行良好。

但我的问题是,我该怎么做才能拥有一个登陆网页,访问者可以在该网页上上传他的图片,网站存储它,启动我的 python 脚本来获取我想要的这些坐标,然后将其检索回 JS 并存储它在一个数组中?

我已经使用 JSON 模块将这些坐标导出到一个 JSON 文件中,但这实际上是一个过程,在我不知道如何做的脚本之间变来变去。

这里是 python 脚本,如果它有任何用处的话:

from PIL import Image
import json
from random import *

im = Image.open("foo.png") 
pixels = im.load()  

width, height = im.size
coord_line = [] 

for x in range(0,width, 1):
    for y in range(height):
        r,g,b = im.getpixel((x,y))
        if r == 0 and g==0 and b==0:
            coord_line.append([x,y])

with open('data.json', 'w') as outfile:
    json.dump(coord_path, outfile)

谢谢!

最佳答案

But my question is, how do I do to have a landing web page where the visitor uploads his image, the site stores it, launches my python script to get these coordinates I want and then retrieves it back in the JS and store it in an array?

这是您想要更深入地学习 python 后端服务器的部分。 Javascript 在前端运行(大部分),如果您要使用它,python 在后端运行。理想情况下,您希望在后端有一个服务器来处理用户上传的图像,然后以 RESTful 方式将其返回给用户。

我推荐学习Flask ,它是一个非常容易学习的 Python 轻量级 Web 服务器。

前端 html/js 处理主页和用户上传 >>> FLASK 服务器处理图像处理请求>>> FLASK 服务器通过某种请求将数据写入或返回给用户

关于javascript - 如何用python和js处理图像数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59072386/

相关文章:

javascript - redux-api-middleware 如何向 store 添加数据?

javascript - 根据当前 ID 更改 CSS 类

python - pytest-django 将固定装置添加到 live_server 固定装置

algorithm - 什么是图像哈希?

ios - 如何在 Xcode 中操作 CGImageRef 中的像素值

javascript - 使用 ./make_bootstrap.sh 部署 Web 应用程序时存在 Sencha 编译问题; ./make_compile.sh

javascript - HTML5视频播放器防寻

python - 如何从 web.py 服务器应用程序中的 Post 获取 JSON 值?

python - 网状不适用于 Python 中的 R-Data 框架和 fit() 函数(TypeError : 'float' object cannot be interpreted as an integer)

Python:如何使用pyautogui lib在屏幕上找到图像?