首页 > 代码库 > 扫描仪扫描文件处理之imagemagick_resize.py

扫描仪扫描文件处理之imagemagick_resize.py

高级扫描书籍系列参数配置文章:http://www.cnblogs.com/whycnblogs/category/1036599.html

作用:

  1. 批量调整扫描图片的宽度高度到指定值(像素不够增加、多余减去,自动居中,不改变原始图片的宽高比例不变形)
  2. 删除图片的exif信息(包含分辨率、DPI、等等)防止后续ABBYY识别出现问题

需要环境:

  1. python3
  2. python3 pip模块Pillow或PIL
  3. imagemagick

 

# -*- coding: utf-8 -*-
# version: python 3
# ==========
# brew reinstall imagemagick
# pip3 install Pillow
# ==========
import sys, os
from PIL import Image

path = ‘/Users/osx/Desktop/test‘  # 处理目录【修改】
suffix = ‘jpg‘  # "处理目录"中的指定图片后缀【修改】

out_path = os.path.join(path, ‘out‘)  # 输出目录
# see: http://www.a4papersize.org/a4-paper-size-in-pixels.php (A4 Dimensions @ 600 DPI)
width = 4961  # 输出宽度(像素)
height = 7016  # 输出高度(像素)
brightness = -10  # 亮度(0表示不设置)
contrast = 20  # 对比度(0表示不设置)

if os.path.exists(out_path):
    print(‘输出目录已存在,请移走后再运行程序!‘)
    sys.exit()

if not os.path.exists(out_path):
    os.makedirs(out_path)


def get_file_list():
    exclude = ([‘.DS_Store‘, ‘.localized‘, ‘Thumbs.db‘, ‘desktop.ini‘])
    result_list = []
    if os.path.isfile(path):
        result_list.append(path)
    else:
        for dir_path, dir_names, file_names in os.walk(path):
            if os.path.abspath(dir_path) != os.path.abspath(path):  # 只允许 1 层目录
                continue
            for name in file_names:
                if not os.path.basename(name) in exclude:
                    result_list.append(os.path.join(dir_path, name))
    return result_list


def parse_image(in_image_file, out_image_file):
    # -----删除exif信息-----
    image_file = open(in_image_file, ‘rb‘)
    image = Image.open(image_file)
    data = http://www.mamicode.com/list(image.getdata())>

  

扫描仪扫描文件处理之imagemagick_resize.py