简体   繁体   English

在网页上使用Python动态创建图像

[英]Dynamic image creation using Python over a web page

I'm new to learning Python and I've been trying to implement a text to image converter that runs on a web page. 我是学习Python的新手,并且一直在尝试实现在网页上运行的文本到图像转换器。

  1. I have succeeded in making the functional code that converts the text into image in Python, using the PIL module (ie, user enters input text at run time and that gets converted into an image and is stored in the hard drive). 我已经成功制作了使用PIL模块在Python中将文本转换为图像的功能代码(即,用户在运行时输入输入文本,然后将其转换为图像并存储在硬盘中)。

  2. 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. b。 once reloaded it displays the string-converted-image, and also provides the text box again for any new user to do the same. 重新加载后,它会显示字符串转换后的图像,并再次为所有新用户提供文本框。

  3. 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. 我从Google Web Apps框架开始,但不幸的是,我了解到Google Web Apps尽管提供了图像API,但它不支持PIL模块,它不支持基于用户输入的动态图像生成。

  4. 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? 因此,有人可以指导我如何继续进行工作,并集成网页和我已经准备好的Python代码吗? So that it works? 这样行得通吗?

  5. Please guide me where I need to look and anything you consider is necessary to know in order to proceed ahead. 请引导我到哪里去寻找您需要考虑的任何信息,以便继续前进。

Check out webpy , just about the simplest Python web framework. 查看webpy ,这是最简单的Python Web框架。 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. 在web.py模块中,导入您的PIL图像转换器,从提交的表单中获取文本(通过web.input() ),转换文本并在页面上呈现新图像。 You should be able to get a simple page up in a dozen or so lines. 您应该能够在十几行左右的页面上得到一个简单的页面。

You can use any existing web scripting language to build the actual web page itself. 您可以使用任何现有的Web脚本语言来构建实际的网页本身。 Python (Django?), PHP, ASP.NET, Perl CGI will all work. Python(Django?),PHP,ASP.NET,Perl CGI都可以使用。 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. 然后,如果您使用的是基于python的网络框架,则只需在将函数包含在代码中后直接调用python代码,然后提供生成的图像即可。 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. 否则,您可以在python脚本中使用对系统代码的system / exec / passthru调用,并将文本作为输入发送,将图像源作为输出。

To put this all into a webpage you would need the following: 要将所有内容放到网页中,您需要执行以下操作:

  • A webpage with form and text box 带有表单和文本框的网页
  • A web scripting language page which serves an image after taking text as input through GET or POST. Web脚本语言页面,该页面在通过GET或POST输入文本作为输入后提供图像。 Example: Django Serve Image 示例: Django服务图像
  • Once the user submits a form it should load the results of the form data being passed to the script in either a frame or div below the form. 用户提交表单后,应在表单下方的框架或div中加载传递到脚本的表单数据结果。 This can be done in many different ways with Javascript or Frames. 可以使用Javascript或框架以多种不同方式完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM