I am participating in the "Early Summer Creative Contest" for details, please see: Early Summer Creative Contest
Hello, everyone, I'm a gray, and I introduced an article about using her name to draw her beautiful pictures, which is mainly implemented in Java. Today, let's use Python to implement it.
Also the final code size will not exceed thirty lines
Link to the previous blog post: Programmer's Romance: Thirty Lines of Code to Make a Picture in Her Name - Java Edition
1. Environment related
Here we choose python as our main drawing weapon. As for the installation of the python environment, we will not introduce it here. Interested partners can explore by themselves.
In the python world, PIL is a powerful tool for manipulating pictures. I believe everyone knows it. Next, we will use it to achieve our goals.
Install dependencies
pip install Pillow
复制代码
2. Basic knowledge points
Before the official start, for those who are not familiar with the operation of PIL, I will briefly introduce its basic usage, and of course, I will also highlight several methods we will use.
2.1 Loading pictures
from PIL import Image
img = Image.open("图片地址")
复制代码
Yes, it's that simple, just use Image.open()
it to read the picture
2.2 Obtaining basic information of pictures
After obtaining the image, there are usually several parameters that need to be paid attention to, such as width and height, channel
width, height = img.size
# RGBA 表示包含透明度,如png
# RGB 不包含透明度,如jpg
mode = img.mode
复制代码
2.3 Create artboards for editing
If we want to draw information on this image, or want to create an empty artboard, then we first need to get an ImageDraw object
from PIL import ImageDraw
# 获取图片对应的画板
draw = ImageDraw.Draw(img)
复制代码
After the draw object is obtained above, we can draw various geometric figures, text, pictures, etc. according to the various methods it provides; if we want to obtain an empty drawing board, what can we do?
# 创建一个待透明度的图,第二个元组参数为图的宽高,第三个表示背景全透明
new_img = Image.new("RGBA", (width, height), (255, 255, 255, 0))
复制代码
ImageDraw provides many drawing methods. The following are some commonly used cases, which will not be described in detail.
new_img = Image.new("RGBA", (480, 640), (255, 255, 255, 0))
# 创建绘制对象
draw = ImageDraw.Draw(new_img)
# 从 (10, 10) -> (100, 100) 画一条黄色直线
draw.line((10, 10, 100, 100), 'red')
# 绘制矩形 (100, 110) -> (200, 200),黑色填充,黄色填充
draw.rectangle((100, 110, 200, 200), 'black', 'red')
# 绘制椭圆
draw.ellipse((300, 300, 500, 400), 'yellowgreen', 'red')
# 园
draw.ellipse((250, 250, 350, 350), 'seagreen', 'red')
# 绘制文本,选择宋体,字体大小为28,uniquecode编码
font = ImageFont.truetype("simsun.ttc", 28, encoding="unic")
draw.text((300, 200), u'一灰灰Blog', 'red', font)
复制代码
Focus on drawing the text here, draw.text((x,y), "文字", "文字颜色", 字体)
, this is the method to use next
2.4 Get pixels
If we want to get the RGB value of the specified coordinate, we can do the following
pixel = img.getpixel((x, y))
复制代码
Seeing the little friend here, combined with the previous blog post, if you want to realize the python version of drawing with her name, I believe it is very simple.
2.5 Preview and save pictures
# 展示图片
new_img.show()
# 保存图片
new_img.save("save.png")
复制代码
3. Python version of text drawing
Next, enter the formal implementation
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
def render(path: str, name: str, save: str):
img = Image.open(path)
width, height = img.size
# 创建画板,放大24倍
new_img = Image.new("RGBA", (width * 24, height * 24), (255, 255, 255, 0))
draw = ImageDraw.Draw(new_img)
# 加载自定义字体,字体文件来自 https://www.diyiziti.com/Builder/446
font = ImageFont.truetype(u"D://MobileFile/潇洒手写体.ttf", 20, encoding="unic") # 设置字体
render_index = 0
for x in range(width):
for y in range(height):
pixel = img.getpixel((x, y))
draw.text((x * 24 + 2, y * 24 + 2), name[render_index], pixel, font) # 再指定的地方使用文字替代之前的纯色
render_index = (render_index + 1) % len(name)
new_img.save(save)
复制代码
Still take the little yellow people as an example, let's see how the generated effect is
Judging from the above picture, the output of the previous java version is similar to that of the previous version. If you are interested, let’s start it;
A grey contact
It is better to have no books than no books. The above content is purely from one family. Due to limited personal ability, there are inevitably omissions and mistakes. If you find bugs or have better suggestions, you are welcome to criticize and correct them. Thank you
- Personal site: blog.hhui.top
- QQ: A gray gray / 3302797840
- WeChat public account: a gray blog