pypillow模块生成小图片

在app测试过程,经常需要构造一些小图片,用于app界面展示测试,这里结合pypillow模块给出一个示例:

import os
from PIL import Image, ImageDraw, ImageFont


# 创建一个灰度图像
# newL = Image.new("L", (600, 400), 255)
# newL.show()
# newrgba = Image.new("RGBA", (28, 28), (20, 200, 45, 255))
# newrgba.show()
# print("The frist image:", newL.size, newL.mode)
# print("The second image:", newrgb.size, newrgb.mode)
# print("The third image:", newrgba.size, newrgba.mode)
# 创建一个RGb图像
def draw_image_rgb(imagesize,text):
    newrgb = Image.new(mode="RGB", size=imagesize, color=(0, 255, 127))
    set_font = ImageFont.truetype(r'C:\Windows\Fonts\simsun.ttc', 20)
    draw = ImageDraw.Draw(newrgb)
    draw.text(xy=(10, imagesize[1] - 60), text=u'{}'.format(text), font=set_font,
              fill="black")
    if not os.path.exists("./test_pictures/test"):
        os.mkdir("./test_pictures/test")
    newrgb.save("./test_pictures/test/size-{0}-{1}{2}.jpg".format(imagesize[0], imagesize[1], text))
    newrgb.show()
    newrgb.close()

def image_resize(filname, imagesize):
    img = Image.open(filname)
    img = img.resize(imagesize, Image.ANTIALIAS)
    img.save("./test_pictures/size-{0}-{1}.png".format(imagesize[0], imagesize[1]))
    # r, g, b = img.split()
    # r.show()
    # g.show()
    # b.show()

if __name__ == '__main__':
    draw_image_rgb(imagesize=(200, 100),text="用数据的心去超越")
    # image_resize(u"1521444731724_KNYHYD2.jpg", (576, 172))

你可能感兴趣的:(python)