博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python练习册第零题
阅读量:7060 次
发布时间:2019-06-28

本文共 2529 字,大约阅读时间需要 8 分钟。

说明

这个是网上一些大佬做的一套练习题,总共有25题,训练大家python在文件读取、文本处理、数据库、网页等方向的熟练度,十分有用。github地址在这:

上不了github的可以直接搜名字,应该能搜到。

我这个笔记集也是只记了五道题。。。我大概多做了一两题吧,唉。。。。。。


题目:

将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。类似于图中效果:

图片


学习内容

虽然python学的不久,但是用Pillow库解决图像方面的问题还是知道的。这个题目的解决思路就是:如何用Pillow库实现在一个图像的某一个像素位置插入一个text字段。


Load an Image

To load an image from a file, use the open() function in the Image module:

>>> from PIL import Image>>> im = Image.open("hopper.ppm")

If successful, this function returns an Image object. If the file cannot be opened, an IOError exception is raised.


ImageFont Module

The ImageFont module defines a class with the same name. Instances of this class store bitmap fonts, and are used with the PIL.ImageDraw.Draw.text() method.

font字体在Image模块中被当做bitmap位图处理,也就是一种图片格式。添加字体其实就是在一张图片上添加bitmap格式的font图片。

from PIL import ImageFont, ImageDrawdraw = ImageDraw.Draw(image)# use a bitmap fontfont = ImageFont.load("arial.pil")draw.text((10, 10), "hello", font=font)# use a truetype fontfont = ImageFont.truetype("arial.ttf", 15)draw.text((10, 25), "world", font=font)

ImageDraw Module

Example: Draw Partial Opacity Text
from PIL import Image, ImageDraw, ImageFont# get an imagebase = Image.open('Pillow/Tests/images/hopper.png').convert('RGBA')# make a blank image for the text, initialized to transparent text colortxt = Image.new('RGBA', base.size, (255,255,255,0))# get a fontfnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 40)# get a drawing contextd = ImageDraw.Draw(txt)# draw text, half opacityd.text((10,10), "Hello", font=fnt, fill=(255,255,255,128))# draw text, full opacityd.text((10,60), "World", font=fnt, fill=(255,255,255,255))out = Image.alpha_composite(base, txt)out.show()

PIL.ImageDraw.ImageDraw.text(xy, text, fill=None, font=None, anchor=None, spacing=0, align="left", direction=None, features=None)


解决代码

from PIL import Image, ImageFont, ImageDrawimage = Image.open(r'C:\Users\ChanWunsam\Desktop\0.png')draw = ImageDraw.Draw(image)font = ImageFont.truetype('arial.ttf', 25)draw.text((230,10), '10', fill='red', font=font)# del draw ## 释放ImageDraw.Draw缓存,不过不影响结果,感觉也非必要image.save(r'C:\Users\ChanWunsam\Desktop\0_1.png', format='PNG')image.close()

0_1.png

别人的解决方法

我的方法是看文档做出来的,确实没有看过别人的源码。不过看起来思路一样,就是他的在细节方面比我更好。

from PIL import Image, ImageFont, ImageDrawimage = Image.open('0.png')w, h = image.sizefont = ImageFont.truetype('arial.ttf', 50)draw = ImageDraw.Draw(image)draw.text((4*w/5, h/5), '5', fill=(255, 10, 10), font=font)image.save('0.0.png', 'png')

转载于:https://www.cnblogs.com/ChanWunsam/p/10018277.html

你可能感兴趣的文章
Maven报错:ArtifactdescriptorException: failed to read artifact for xxxxxx
查看>>
C# API 调用格式和参数类型
查看>>
无法删除MySql数据库,报错1010 error dropping
查看>>
Android application使用总结
查看>>
硬币问题
查看>>
鼠标悬停图片移动的效果
查看>>
YII2操作mongodb笔记(转)
查看>>
javaScript 比较数字大小
查看>>
从汇编来看c语言之指针
查看>>
sqlserver查询表索引
查看>>
JavaScript 基础知识系列:数据类型及slice(8,-1)
查看>>
String,StringBuffer,StringBuilder三者有什么异同?
查看>>
[LeetCode] Invert Binary Tree
查看>>
2018.3.31——(4)句子
查看>>
js call
查看>>
【原】Java学习笔记024 - 包装类
查看>>
如何写一手漂亮的 Vue
查看>>
2018.10.29-dtoj-3999-游戏(game)
查看>>
LNOI2019 游记
查看>>
php简单实现MVC
查看>>