我是Python 的新手,我一直在尝试实现一个在网页上运行的文本到图像转换器。
-
我已经使用 PIL 模块成功地在 Python 中编写了将文本转换为图像的功能代码(即,用户在运行时输入输入文本,然后将其转换为图像并存储在硬盘中)。
-
现在我希望此代码段能够在网页上运行(类似于网站中的反馈或评论表单)。
a.它要求用户在文本字段中输入一个字符串,然后按下按钮,它会继续将其转换为图像并重新加载页面。
b.重新加载后,它会显示字符串转换后的图像,并且还会再次提供文本框,供任何新用户执行相同操作。
-
我从 Google Web Apps 框架开始,不幸的是我了解到 Google Web Apps 不支持 PIL 模块,尽管它提供了图像 API,它不支持基于用户输入的动态图像生成。
-
那么,有人可以指导我如何继续集成网页和我准备好的 Python 代码吗?那么它可以工作吗?
-
请指导我需要查看的内容以及您认为需要了解的任何信息才能继续
前进。
I'm new to learning Python and I've been trying to implement a text to image converter that runs on a web page.
-
I have succeeded in making the functional code that converts the text into image in Python, using the PIL module (i.e., user enters input text at run time and that gets converted into an image and is stored in the hard drive).
-
Now I want this code segment to work on a web page (something similar to feedback or comments form in websites).
a. That asks the user to enter a string in a text field and, on pressing a button, it goes ahead and converts it into an image and reloads the page.
b. once reloaded it displays the string-converted-image, and also provides the text box again for any new user to do the same.
-
I started with the Google Web Apps framework, and unfortunately I learnt that Google Web Apps can't support PIL module although it provides an image API, it can't support dynamic image generations based on user input.
-
So, can anyone guide me as to how I can go ahead and integrate a web page and the Python code that I have ready? So that it works?
-
Please guide me where I need to look and anything you consider is necessary to know in order to proceed
ahead.
发布评论
评论(2)
查看 webpy,它是最简单的 Python Web 框架。在 web.py 模块中,导入 PIL 图像转换器,从提交的表单中获取文本(通过
web.input()
),转换文本并渲染带有新图像的页面。您应该能够在十几行左右的时间内完成一个简单的页面。Check out webpy, just about the simplest Python web framework. In the web.py module, import your PIL image converter, grab the text from the submitted form (via
web.input()
), convert the text and render a page with the new image on it. You should be able to get a simple page up in a dozen or so lines.您可以使用任何现有的 Web 脚本语言来构建实际的网页本身。 Python(Django?)、PHP、ASP.NET、Perl CGI 都可以使用。然后,如果您使用基于 python 的 Web 框架,只需在代码中包含该函数后直接调用您的 python 代码并提供生成的图像即可。否则,您可以在 python 脚本内使用 system/exec/passthru 调用您的代码,并将文本作为输入发送,获取图像源作为输出。
要将这一切放入网页中,您需要以下内容:
You can use any existing web scripting language to build the actual web page itself. Python (Django?), PHP, ASP.NET, Perl CGI will all work. Then if you're using a python based web framework simply call your python code directly after including the function in your code and serve the resulting generated image. Otherwise you can use a system/exec/passthru call to your code inside of a python script and send the text as input getting the source of the image as output.
To put this all into a webpage you would need the following: