"python" PIL Bulk Draw picture Rectangle box Tool

Source: Internet
Author: User

Tools using Pil:python Imaging library, image processing Standard Libraries. The PIL feature is very powerful, but the API is very easy to use.

Installing PIL

Install directly via apt under Debian/ubuntu Linux

$ sudoapt-get installpython-imaging

Windows platform installs directly from PIP

pip installpillow

Bulk Tool Scripts

default execution is:                 Execute script command python drawline.py            1. Gets the ' png ' ' jpg ' file          2. draw a rectangular box with a width-to-height ratio of 0.5,0.5          3. Save the picture to the line folder under the current path
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081 # -*- coding: utf-8 -*-from PIL import Image, ImageDrawimport os, sysdef drawLine(im, width, height):    ‘‘‘    在图片上绘制矩形图    :param im: 图片    :param width: 矩形宽占比    :param height: 矩形高占比    :return:    ‘‘‘    draw = ImageDraw.Draw(im)    image_width = im.size[0]    image_height = im.size[1]    line_width = im.size[0] * width    line_height = im.size[1] * height    draw.line(        ((image_width - line_width) / 2, (image_height - line_height) / 2,         (image_width + line_width) / 2, (image_height - line_height) / 2),        fill=128)    draw.line(        ((image_width - line_width) / 2, (image_height - line_height) / 2,         (image_width - line_width) / 2, (image_height + line_height) / 2),        fill=128)    draw.line(        ((image_width + line_width) / 2, (image_height - line_height) / 2,         (image_width + line_width) / 2, (image_height + line_height) / 2),        fill=128)    draw.line(        ((image_width - line_width) / 2, (image_height + line_height) / 2,         (image_width + line_width) / 2, (image_height + line_height) / 2),        fill=128)    del drawdef endWith(s, *endstring):    ‘‘‘    过滤文件扩展名    :param s: 文件名    :param endstring: 所需过滤的扩展名    :return:    ‘‘‘    array = map(s.endswith, endstring)    if True in array:        return True    else:        return Falseif __name__ == ‘__main__‘:    ‘‘‘    默认执行方式为:        1.获取当前路径下的‘png‘,‘jpg‘文件        2.绘制宽高占比为0.5,0.5的矩形框        3.保存图片至当前路径下的line文件夹    ‘‘‘    line_w = 0.5    line_h = 0.5    try:        if sys.argv[1]:            line_w = float(sys.argv[1])        if sys.argv[2]:            line_h = float(sys.argv[2])    except IndexError:        pass    current_path = os.getcwd()    save_path = os.path.join(current_path, ‘line‘)    file_list = os.listdir(current_path)    for file_one in file_list:        # endWith(file_one, ‘.png‘, ‘.jpg‘) 第二个参数后为过滤格式 以 , 分割        if endWith(file_one, ‘.png‘, ‘.jpg‘):            im = Image.open(file_one)            # drawLine(im,line_w, line_h) 后面两位参数为矩形图宽高占比            drawLine(im, line_w, line_h)            if not os.path.exists(save_path):                os.mkdir(save_path)            im.save(                os.path.join(save_path, str(file_one.split(‘.‘)[-2]) + ‘_line.‘ + str(file_one.split(‘.‘)[-1])))

"python" PIL Bulk Draw picture Rectangle box Tool

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.