使用PIL在图像上添加文本 [英] Add Text on Image using PIL

查看:576
本文介绍了使用PIL在图像上添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加载图像的应用程序,用户点击它时,会出现一个文本区域(使用 jquery ),用户可以在其中写一些文本图片。应该在Image上添加。



在对其进行了一些研究之后,我认为 PIL (Python Imaging Library )可以帮助我做到这一点。所以我尝试了几个例子来看看它是如何工作的,我设法在图像上写文本。但是,当我尝试使用 Python Shell 和在Web环境中时,我认为有一些区别。我的意思是textarea上的文字是非常大的px。当使用PIL作为textarea的文本时,如何实现相同大小的文本?



文本是Multiline。如何使用 PIL



有没有比使用PIL更好的方法?我不完全确定,如果这是最好的实现。



html:

 < img src =images / test.jpg/> 

其正在编辑的图像

  var count = 0; 
$('textarea')。autogrow();
$('img')。click(function(){
count = count + 1;
if(count> 1){
$(this) '< textarea />');
$('textarea')。focus();
}
});

添加textarea的jquery。
文本区域也是位置:绝对和固定大小。



我应该将其放在表单中,以便我可以在图像上获取textarea的坐标吗?
当用户单击并将其保存在图像上时,我想在图像上写入文本。

解决方案

我认为 ImageFont 模块可用于 PIL 应该有助于解决文字字体大小问题。只需检查您的字体类型和大小是否适合您,并使用以下功能更改字体值。

 #font = ImageFont.truetype (< font-file>,< font-size>)
#font-file应该存在于提供的路径中。
>>> font = ImageFont.truetype(sans-serif.ttf,16)

所以你的代码将会类似于

 >>>来自PIL import Image 
>>>从PIL导入ImageFont
>>>从PIL导入ImageDraw
>>> img = Image.open(sample_in.jpg)
>>> draw = ImageDraw.Draw(img)
#font = ImageFont.truetype(< font-file>,< font-size>)
>>> font = ImageFont.truetype(sans-serif.ttf,16)
#draw.text((x,y),Sample Text,(r,g,b))
> >> draw.text((0,0),Sample Text,(255,255,255),font = font)
>>> img.save('sample-out.jpg')

您可能需要额外付出努力计算字体大小。如果您想根据用户在 TextArea 中提供的文本数量进行更改。



添加文本包装(多重事物)只是粗略地想出一行中可以有多少个字符,然后你可以为你的文本编写一个预处理函数,它基本上是找到每行最后一个字符,并将空格转换成这个字符到新行。


I have an application that loads an Image and when the user clicks it, a text area appears for this Image (using jquery), where user can write some text on the Image. Which should be added on Image.

After doing some research on it, I figured that PIL (Python Imaging Library ) can help me do this. So I tried couple of examples to see how it works and I managed to write text on an image. But I think there is some difference when I try it using Python Shell and in web environment. I mean the text on the textarea is very big in px. How can I achieve the same size of text when using PIL as the one on the textarea?

The text is Multiline. How can i make it multiline in image also, using PIL?

Is there a better way than using PIL? I am not entirely sure, If this is the best implementation.

html:

<img src="images/test.jpg"/>

its the image being edited

var count = 0;
$('textarea').autogrow();
$('img').click(function(){
    count = count + 1;
    if (count > 1){
        $(this).after('<textarea />');
        $('textarea').focus();
    }   
});

the jquery to add the textarea. Also the text area is position:absolute and fixed size.

Should i place it inside a form so i can get coordinates of textarea on image? I want to write text on image when user clicks and save it on the image.

解决方案

I think ImageFont module available in PIL should be helpful in solving text font size problem. Just check what font type and size is appropriate for you and use following function to change font values.

# font = ImageFont.truetype(<font-file>, <font-size>)
# font-file should be present in provided path.
>>> font = ImageFont.truetype("sans-serif.ttf", 16)

So your code will look something similar to:

>>> from PIL import Image
>>> from PIL import ImageFont
>>> from PIL import ImageDraw 
>>> img = Image.open("sample_in.jpg")
>>> draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(<font-file>, <font-size>)
>>> font = ImageFont.truetype("sans-serif.ttf", 16)
# draw.text((x, y),"Sample Text",(r,g,b))
>>> draw.text((0, 0),"Sample Text",(255,255,255),font=font)
>>> img.save('sample-out.jpg')

You might need to put some extra effort to calculate font size. In case you want to change it based on amount of text user has provided in TextArea.

To add text wrapping (Multiline thing) just take a rough idea of how many characters can come in one line, Then you can probably write a pre-pprocessing function for your Text, Which basically finds the character which will be last in each line and converts white space before this character to new-line.

这篇关于使用PIL在图像上添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆