PIL removes outlines from written fonts

Please indicate the source for reprint: http://blog.csdn.net/jinixin/article/details/79248842

Aliases: PIL removes the stroke of the written font, PIL removes the black border of the written font, Python makes a transparent background image and removes the outline of the written font



question


Through a previous blog , I briefly introduced the method of Python using the PIL library to make a transparent background image and write a line of text. But then it turns out that the resulting font has a dark outline (as in the image below), which would look very out of place if the image was attached to a light background.




Is there a way to remove these dark outlines? The answer is yes, the text in the picture is also composed of pixels, you can consider replacing those dark pixels with the font color.



Prepare


The following briefly introduces several PIL library methods used:


Image channel:

A complete image is composed of multiple channels, which work together to produce a complete image. In RGB mode, it refers to the three separate channels of red R, green G, and blue B.


1)Image.point(lut, mode=None)

Modify the Image object by passing in the lookup table or a function, returning the resulting copy of the image

lut parameter: lookup table sequence or use a function instead, the function must receive a parameter, the function iteratively processes each pixel in the image, and merges the processing results into a new image (personally recommended function, because I don't understand lookup watch, escape~)

The mode parameter: defines some attributes of pixels in the image, the default is consistent with the attributes of the input image


2)ImageColor.getrgb(color)

Converts a string or hexadecimal color to an RGB tuple


3)Image.split()

Divide the Image into a single channel image and return, such as an RGB image will be split into three new images, each new image contains only one channel color (red, green, blue). If you only need one channel, it will be faster to use the getchannel function


4)Image.merge(mode, bands)

Merge a series of channel images into a new multi-channel image and return

The mode parameter: defines the pixel attributes of the new image, see here for details

bands parameter: a sequence containing a series of channel images, all channel images must have the same size



ideas


1) First use the image.split method to divide the RGBA image into four single-channel images of R, G, B, and A;

2) Then use the image.point method to set each pixel of the R channel image to the R value of the font color, and do the same processing to the G channel image and B channel image, and leave the A channel image in charge of transparency. be transparent;

3) Finally, merge all the channel images generated in 2) into a new image through the image.merge method, then you will find that the dark contours have completely disappeared.

The result after processing:





code


#!/usr/bin/env python
# coding=utf-8

from PIL import Image, ImageDraw, ImageFont, ImageColor

font_color = '#E1FFFF'

image = Image.new(mode='RGBA', size=(400, 50)) # In RGBA mode, no color parameter is a transparent image
draw_table = ImageDraw.Draw(im=image)
draw_table.text(xy=(0, 0), text=u'Look up and smile like a full moon', fill=font_color, font=ImageFont.truetype('./SimHei.ttf', 50))

fcolor_channel = ImageColor.getrgb(font_color)
r, g, b, a = image.split() # split the image into three single channel images

r = r.point(lambda x: fcolor_channel[0]) # Iterate over all pixels of the R channel image and set them to the R value of the font color
g = g.point(lambda x: fcolor_channel[1])
b = b.point(lambda x: fcolor_channel[2])

image = Image.merge('RGBA', (r, g, b, a)) # Merge multiple channel images into a new image

image.show()
image.save('满月.png', 'PNG')
image.close()


If there is any inappropriateness in the text, I hope everyone will tolerate and point out, thank you



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325561934&siteId=291194637