工具采用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. Html标签第一课

    <p>段落标签</p> <h1>字体标签,1到6,越来越小</h1>.....<h6></h6><h>标签自动换行 ...

  2. java基础之 多态

    在面向对象编程(Object-Oriented Programming, OOP)中,多态机制无疑是其最具特色的功能,甚至可以说,不运用多态的编程不能称之为OOP.这也是为什么有人说,使用面向对象语言 ...

  3. druid.properties的配置

    driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://NoOne:3306/eyes<!--需修改--> username=root ...

  4. ie11浏览器和chrome浏览器对于bgsound和background的一些区别

    今天在编写一个非常简单的网页的时候,按照书上写的,使用了一个jpg图片作为背景图片,用background属性放在<body>标签内,同时使用<bgsound>标签插入背景音乐 ...

  5. android.os.NetworkOnMainThreadException异常

    在android4.0以前,访问网络的代码可以写在UI主线程,但是在android4.0以上就不能在ui主线程中访问网络了,会出现android.os.NetworkOnMainThreadExcep ...

  6. 关于用Java实现二维码的生成

    昨天晚上看了一个视频讲的是web端把名片搞成二维码的形式,觉得挺有意思的,不过我还是初学,所以就没在网页端实现,写了命令行程序. 虽然看着程序很短,不过写的过程中还是出了问题, 最致命的就是 Grap ...

  7. Mysql封装

    <?php header("content-type:text/html;charset=utf-8"); class db{    //私有的静态属性    private ...

  8. CxImage图像库的使用 .

    http://blog.csdn.net/wangjie0377/article/details/7830405 CxImage图像库 CxImage下载地址:http://www.codeproje ...

  9. Myeclipse+AJAX+Servlet

    最近刚开始学AJAX的知识,这里介绍一个简单的Myeclipse+AJAX+Servlet项目. 此项目包含3个文件:index.jsp.check.java.还有一个需要配置的文件是:web.xml ...

  10. jsRender绑定数据

    首先,引入jquery(很重要),其次引入jsRender.js <script type="text/javascript" src="js/jquery-1.7 ...