Python 练习册,每天一个小程序(0000)

在学习python的查找资料过程中,找到了一个他人整理的python题目集合。其中0000题目要求在一个图片右上角添加一个数字,具体内容可以点击https://github.com/Yixiaohan/show-me-the-code来查看。
#功能:输入图片完整地址,再输入新生成图片名字可以产生新的添加了数字的图片,其中字体文件我放置在代码同一文件夹下
# -*- coding: utf-8 -*-

from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

#输入图片的完整路径,打开该图片
location = raw_input("Please input the location of the image:")
target = raw_input("Please input the new name of target, you can enter to get the default name:")
image = Image.open(location)

#获得当前图片的大小,根据此参数设置字体大小
xSize, ySize = image.size
font = ImageFont.truetype("arial.ttf",int(ySize / 8))

#在图片上添加数字3
draw = ImageDraw.Draw(image)
draw.text((0.9*xSize, 0.1*ySize),"3",(255,0,0),font)

#根据输入的命名来设置新图片名字,默认设置为“原名_new”,格式与原图相同
form = location.split('.')[1]
if target == "":
    new_location = location.split('.')[0]
    image.save(new_location+'_new.'+form)
else:
    image.save(target+'.'+form)

关于ImageDraw模块的详细内容可以点击http://effbot.org/imagingbook/imagedraw.htm查看
而PIL模块的安装可以点击http://pillow-cn.readthedocs.org/zh_CN/latest/installation.html查看

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值