目前,当我需要创建一些用于科学可视化的图像(通常是从 CSV 文件或其他数据容器)时,我使用 Python 作为脚本语言,并使用 Cairo 作为绘图 API。它允许我运行脚本,并拥有一个充满 .png 文件的目录。
虽然 Cairo 是一个非常好的绘图 API,但我认为 Canvas 除了更主流之外,还有一些操作(如合成和 Swadowing),这使得创建更复杂的图形成为可能,以及其他潜在的优势。
我看到的主要问题是基于浏览器的,所以我认为脚本必须调用可编写脚本的浏览器,它具有实际的画布渲染引擎。我什至不知道“可编写脚本的浏览器”(通过命令行)的概念是否存在。
其他不太大的问题是需要运行 HTTP 服务器,但我认为 Python 提供的 SimpleHTTPServer 应该可以正常工作。
有什么想法吗?
感谢您的阅读,希望能帮助到更多的人。
Currently, when I need to create some images for scientific visualization (generally, from CSV files or other data-container), I use Python as scripting language, and Cairo as drawing API. It allows me to run a script, and have a directory full of .png files.
Although Cairo is a very nice drawing API, I see Canvas, besides being more mainstream, has some operations (like compositing and swadowing), which would make possible to create more sophisticated graphics, among other potential advantages.
The main problem I see is being browser-based, so I think the script would have to call a scriptable browser, which has the actual canvas rendering engine. I don't even know if the concept of "scriptable browser" (via command line) exists at all.
Other not-so-big problem would be the need for a running HTTP server, but I think Python provides SimpleHTTPServer
which should work fine.
Any idea?
Thanks for reading, hope it helps more people in the future.
发布评论
评论(2)
PhantomJS 怎么样?它有一个
render
方法来输出图像;请参阅接口文档。How about PhantomJS? It has a
render
method that will output images; see the Interface docs.伟大的雄心勃勃的想法!
可以编写打开浏览器的脚本并创建图像;拯救他们可能有点困难。
您可以编写一个包含脚本的 HTML 文件,然后使用 Python 的 webbrowser 模块打开该文件在浏览器窗口中 - 您可以设置一个简单的网络服务器或使用
file://
URI 来完成此操作。设置一个简单的 Web 服务器可能是值得的,因为即使在“同一域”上,也不能保证对
file://
URI 的 XMLHttpRequest 支持。您可以编写 Javascript 来获取任意(本地)URI,使用 XHR 获取其内容并进行处理。传递本地 URI 的一种简单方法是从片段字符串中解析它——如果有的话,这是一种笨拙的方法,但它在 Twitter 中得到了使用,所以它不会太糟糕,对吧?
然后将每一张图像渲染到画布上并查看 Eli Grey 的 FileSaver 并且...您可能会必须点击按钮无数次才能保存所有图像。
祝你好运——我很想知道事情进展如何。
Great ambitious idea!
It is possible to script the opening of a browser and create images; saving them might be a little difficult.
You could write an HTML file including your scripts, then use Python's webbrowser module to open that file in a browser window-- you can set up a trivial webserver or use a
file://
URI to do it.Setting up a trivial webserver might be worth it as support for XMLHttpRequests to
file://
URIs is not guaranteed, even on the "same domain".You could write your Javascript to take an arbitrary (local) URI, use XHR to fetch its contents, and process it. An easy way to pass that local URI would be to parse it out of the fragment string-- a janky approach if ever there were one, but it sees use in Twitter so it can't be too bad, right?
Then render each one of your images to canvas and check out Eli Grey's FileSaver and... you might have to click a button a bazillion times to save all your images.
Best of luck-- I'm curious to hear how it goes.