工具采用PIL:Python Imaging Library,图像处理标准库。PIL功能非常强大,但API却非常简单易用。

安装PIL

在Debian/Ubuntu Linux下直接通过apt安装

$ sudo apt-get install python-imaging

Windows平台直接通过pip安装

pip install pillow

批量工具脚本

默认执行方式为:     
        执行脚本命令 python drawline.py  
        1.获取当前路径下的'png','jpg'文件
        2.绘制宽高占比为0.5,0.5的矩形框
        3.保存图片至当前路径下的line文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw
import os, sys
 
 
def 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 draw
 
 
def endWith(s, *endstring):
    '''
    过滤文件扩展名
    :param s: 文件名
    :param endstring: 所需过滤的扩展名
    :return:
    '''
    array = map(s.endswith, endstring)
    if True in array:
        return True
    else:
        return False
 
 
if __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 批量绘制图片矩形框工具的更多相关文章

  1. 【Python】批量给图片增加水印工具

    背景 最近有一些图片需要增加水印,找了一圈也没看见比较好的工具,又不想用破解的PS,干脆自己做了一个GUI工具,有需要的同学自取 功能 支持水印预览 自定义水印文字内容 支持行楷和微软雅黑两种字体 支 ...

  2. 基于Python PIL实现简单图片格式转化器

    基于Python PIL实现简单图片格式转化器 目录 基于Python PIL实现简单图片格式转化器 1.简介 2.前期资料准备 2.1.1如何实现图片格式转换? 2.1.2如何保存需要大小的图片? ...

  3. < python PIL - 批量图像处理 - 生成自定义大小图像 >

    < python PIL - 批量图像处理 - 生成自定义大小图像 > 直接用python自带的PIL图像库,对一个文件夹下所有jpg/png的图像进行自定义像素变换 from PIL i ...

  4. c#在pictureBox控件上绘制多个矩形框及删除绘制的矩形框

    在pictureBox上每次只绘制一个矩形框,绘制下一个矩形框时上次绘制的矩形框取消,代码如链接:https://www.cnblogs.com/luxiao/p/5625196.html 在绘制矩形 ...

  5. < python PIL - 批量图像处理 - RGB图像生成灰度图像 >

    < python PIL - 批量图像处理 - RGB图像生成灰度图像 > 直接用python自带的PIL图像库,将一个文件夹下所有jpg/png的RGB图像转换成灰度/黑白图像 from ...

  6. Python,PIL压缩裁剪图片

    自己写了用来压缩 DC 照片的,批量处理整目录文件,非常方便.需要安装 PIL #!/usr/bin/env python import Image import os import os.path ...

  7. 使用Python轻松批量压缩图片

    在互联网,图片的大小对一个网站的响应速度有着明显的影响,因此在提供用户预览的时候,图片往往是使用压缩后的.如果一个网站图片较多,一张张压缩显然很浪费时间.那么接下来,我就跟大家分享一个批量压缩图片的方 ...

  8. OpenCV—Python 轮廓检测 绘出矩形框(findContours\ boundingRect\rectangle

    千万注意opencv的轮廓检测和边缘检测是两码事 本文链接:https://blog.csdn.net/wsp_1138886114/article/details/82945328 1 获取轮廓 O ...

  9. Python PIL创建文字图片

    PIL库中包含了很多模块,恰当地利用这些模块可以做许多图像处理方面的工作. 下面是我用来生成字母或字符串测试图片而写的类及测试代码. 主要用到的模块: PIL.Image,PIL.ImageDraw, ...

随机推荐

  1. php 错误堆栈

    ob_start();  debug_print_backtrace();   $errpr = ob_get_clean();

  2. Oracle函数over(),rank()over()作用及用法--分区(分组)求和& 不连续/连续排名

    (1)   函数:  over()的作用及用法:    -- 分区(分组)求和. RANK ( ) OVER ( [query_partition_clause] order_by_clause )D ...

  3. c# 第一个实例 通哥

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. 在gridControl的单元格中的多行文本

    我们知道,gridcontrol里面的单元格默认是不能换行的,但是有时候我们需要显示要换行的文本,应该怎么处理呢?这里提供一个方案: 假设我有一个列”合同文本“(colContractText),我要 ...

  5. Android Fragment (二) 实例2

    由于看客的要求,我就把读者所要的写出来. 由于上一篇是每一个Fragment 实例了同一个layout.xml ,造成了读者的困惑,这篇我就让每一个Fragment 加载一个不同的layout.xml ...

  6. php判断手机还是pc

    <?php function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AG ...

  7. Github注册过程

      一 github注册过程: 1.首先百度github官网,进入官网页面 2.在该页面分别输入昵称,昵称需要不和别人的重复,而后输入邮箱地址,该邮箱不能注册过这个网站,最后输入密码,密码至少要有七个 ...

  8. android基础(六)android的消息处理机制

    Android中的消息处理机制由四个部分组成:Message.Handler.MessageQueue和Looper,并且MessageQueue封装在Looper中,我们一般不直接与MQ打交道. 一 ...

  9. std::vector<bool>中的坑

    http://www.cplusplus.com/reference/vector/vector/?kw=vector C++中,vector<bool>为了达到节省内存的目的,专门做了特 ...

  10. delphi里动态创建AlphaControls实现换肤

    AlphaControls是一套Delphi下的优秀的皮肤vcl控件.几年前,一般用得比较多的是vclskin,使用很方便,可惜这套2010年已经停止维护了.后来就看到更多的人开始推崇AlphaCon ...