简体   繁体   English

您将使用哪种脚本语言在生成的图像上放置文本?

[英]What scripting language would you use to put text over a generated image?

I got this question from my cousin: 我从堂兄那里得到了一个问题:

"What will be the best way to create a similar working website just like: http://www.plakletters.nl ". “创建一个类似的工作网站的最佳方法是什么,就像: http : //www.plakletters.nl ”。 I have looked into the website and think that to start of with i'm gonna help my cousin write a script that loads all fonts installed on the system into a dropdownlist. 我已经浏览过该网站,并认为从一开始,我将帮助我的堂兄编写一个脚本,将系统上安装的所有字体加载到下拉列表中。 This dropdownlist will post a font value back to the page and will create images with the input of a user that comes from a textbox. 该下拉列表会将字体值发布回页面,并使用来自文本框的用户输入来创建图像。 This images will have to be created server side, and I also want to give the users the ability (in the future) to lay their own text over an uploaded image, to see what the result will look like on their own image. 该图像必须在服务器端创建,我还希望使用户能够(将来)在上传的图像上放置他们自己的文本,以查看结果在他们自己的图像上是什么样子。 I found some information about how to create images using php, I don't know if php can output a list of installed fonts from the system it's running on. 我发现了一些有关如何使用php创建图像的信息,我不知道php是否可以从运行它的系统中输出已安装字体的列表。 What scripting language would you use to get this job done? 您将使用哪种脚本语言来完成这项工作? Keep in mind i would just like to start with outputting some images based on user input using a scripting language. 请记住,我只想从使用脚本语言根据用户输入输出一些图像开始。

ImageMagick. ImageMagick。 This can be called from the shell or with the Perl bindings or with bindings for a lot of other languages. 可以从shell或Perl绑定或许多其他语言的绑定中调用它。 See http://www.imagemagick.org/Usage/text/ 参见http://www.imagemagick.org/用法/ text /

If you use perl, another alternative is the Imager module. 如果使用perl,则另一种替代方法是Imager模块。 This tends to be a simpler install than ImageMagick/PerlMagick. 这往往比ImageMagick / PerlMagick的安装更为简单。

Adding text looks like: 添加文本如下所示:

use Imager;

my $img = Imager->new(file => "foo.jpg") || die Imager->errstr();

my $font = Imager::Font->new(file=>"Arial.ttf");

$img->string(
    x => 50, y => 70,
    string => "Hello, World!",
    font => $font, size => 30,
    aa => 1, color => 'white'
);
$img->write(file => "foo2.jpg");

More details here . 更多细节在这里

Well, my favorite language is Python, so that's what I'd use! 好吧,我最喜欢的语言是Python,这就是我要使用的语言! :) The PIL module gives you image manipulation capabilities. :) PIL模块为您提供图像处理功能。

Although I have never used PIL before, I put together the following with a few minutes of research and experimentation: 尽管我以前从未使用过PIL,但我将以下内容与几分钟的研究和实验放在一起:

import Image
import ImageDraw

im = Image.open("my_image.png")
draw = ImageDraw.Draw(im)
draw.text((0,0), "my text")
im.save("my_out.png")

As you might have guessed, this just opens a PNG file, writes "my text" into it in the upper left corner and saves the modified image with a new name. 您可能已经猜到了,这只是打开一个PNG文件,在左上角向其中写入“我的文本”,然后使用新名称保存修改后的图像。 I do not know how to get the font information you're looking for, but expect there's a module that can help with that also. 我不知道如何获取所需的字体信息,但是希望有一个模块可以提供帮助。

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

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