English 中文(简体)
使用PIL将文本绘制到图像中心
原标题:draw text to center of the image using PIL
  • 时间:2010-02-10 06:29:40
  •  标签:
  • python
  • text

I have a few stings I would like to draw to imagea. the thing is that I want each string to be displayed in the middle of each image. how do I do it? or how can I know the length in pixels of a string (giving ofcourse the font)? thanks

问题回答

PIL font s do have a getsize method which you can use to get width and height of font, did you try that? e.g.

from PIL import Image, ImageDraw, ImageFont

fontFile = "/usr/share/fonts/truetype/freefont/FreeSansBold.ttf"
font = ImageFont.truetype(fontFile, 10)
text="anurag"
print font.getsize(text)
assert font.getsize(text)[0]*2 == font.getsize(text*2)[0]

您可以在http://tools.jedutils.com/tools/center-text-image找到其实现。

你可以使用那个页面直接创建图像,而不必自己实现例程,但页面上也包含了代码。

遵循Nicole的建议

from PIL import Image, ImageDraw, ImageFont, ImageFilter
import StringIO

filter_dict = {
     BLUR  : ImageFilter.BLUR,
     CONTOUR  : ImageFilter.CONTOUR,
     DETAIL  : ImageFilter.DETAIL,
     EDGE_ENHANCE  : ImageFilter.EDGE_ENHANCE,
     EDGE_ENHANCE_MORE  : ImageFilter.EDGE_ENHANCE_MORE,
     EMBOSS  : ImageFilter.EMBOSS,
     FIND_EDGES  : ImageFilter.FIND_EDGES,
     SMOOTH  : ImageFilter.SMOOTH,
     SMOOTH_MORE  : ImageFilter.SMOOTH_MORE,
     SHARPEN  : ImageFilter.SHARPEN
}

def get_font_full_path(font_path,font_name):
    ext =  .TTF  if font_name.upper() == font_name else ".ttf"
    return font_path + font_name + ext

def create_image(font_name, font_size, font_color, width, height, back_ground_color, text, img_type="JPEG", image_filter=None):
    font_full_path = get_font_full_path(font_path,font_name)
    font  =  ImageFont.truetype ( font_full_path, font_size )

    im  =  Image.new ( "RGB", (width,height), back_ground_color )
    draw  =  ImageDraw.Draw ( im )
    text_x, text_y = font.getsize(text)
    x = (width - text_x)/2
    y = (height - text_y)/2
    draw.text ( (x,y), text, font=font, fill=font_color )

    if image_filter:
        real_filter = filter_dict[image_filter]
        im = im.filter(real_filter)
    im.save ( "sample.jpg", format=img_type )

Sorry, I cannot translate without context or the original phrase. Please provide more information.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签