import sys

from PIL import Image

try:
import src.abstract as pyinsane
except ImportError:
import pyinsane.abstract as pyinsane def set_scanner_opt(scanner, opt, value):
print("Setting %s to %s" % (opt, str(value)))
try:
scanner.options[opt].value = value
except (KeyError, pyinsane.SaneException) as exc:
print("Failed to set %s to %s: %s" % (opt, str(value), str(exc))) if __name__ == "__main__":
steps = False args = sys.argv[1:]
if len(args) <= 0 or args[0] == "-h" or args[0] == "--help":
print("Syntax:")
print(" %s [-s] <output file (JPG)>" % sys.argv[0])
print("")
print("Options:")
print(" -s : Generate intermediate images (may generate a lot of"
" images !)")
sys.exit(1) for arg in args[:]:
if arg == "-s":
steps = True
args.remove(arg) output_file = args[0]
print("Output file: %s" % output_file) print("Looking for scanners ...")
devices = pyinsane.get_devices()
if (len(devices) <= 0):
print("No scanner detected !")
sys.exit(1)
print("Devices detected:")
print("- " + "\n- ".join([str(d) for d in devices])) print("") device = devices[0]
print("Will use: %s" % str(device)) print("") source = 'Auto' if (device.options['source'].constraint_type
== pyinsane.SaneConstraintType.STRING_LIST):
if 'Auto' in device.options['source'].constraint:
source = 'Auto'
elif 'FlatBed' in device.options['source'].constraint:
source = 'FlatBed'
else:
print("Warning: Unknown constraint type on the source: %d"
% device.options['source'].constraint_type) set_scanner_opt(device, 'resolution', 300)
set_scanner_opt(device, 'source', source)
set_scanner_opt(device, 'mode', 'Color') print("") print("Scanning ... ")
scan_session = device.scan(multiple=False) if steps and scan_session.scan.expected_size[1] < 0:
print("Warning: requested step by step scan images, but"
" scanner didn't report the expected number of lines"
" in the final image --> can't do")
print("Step by step scan images won't be recorded")
steps = False if steps:
last_line = 0
expected_size = scan_session.scan.expected_size
img = Image.new("RGB", expected_size, "#ff00ff")
sp = output_file.split(".")
steps_filename = (".".join(sp[:-1]), sp[-1]) try:
PROGRESSION_INDICATOR = ['|', '/', '-', '\\']
i = -1
while True:
i += 1
i %= len(PROGRESSION_INDICATOR)
sys.stdout.write("\b%s" % PROGRESSION_INDICATOR[i])
sys.stdout.flush() scan_session.scan.read() if steps:
next_line = scan_session.scan.available_lines[1]
if (next_line > last_line):
subimg = scan_session.scan.get_image(last_line, next_line)
img.paste(subimg, (0, last_line))
img.save("%s-%05d.%s" % (steps_filename[0], last_line,
steps_filename[1]), "JPEG")
last_line = next_line
except EOFError:
pass print("\b ")
print("Writing output file ...")
img = scan_session.images[0]
img.save(output_file, "JPEG")
print("Done")

python pyinsane应用的更多相关文章

  1. PYTHON PIP 快速安装

    清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https: ...

  2. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  3. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  4. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  5. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  6. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  7. 使用Python保存屏幕截图(不使用PIL)

    起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...

  8. Python编码记录

    字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...

  9. Apache执行Python脚本

    由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...

随机推荐

  1. TheSuperego 实验六 团队作业3:项目需求分析与原型设计

    项目 内容 课程班级博客链接 班级博客 这个作业要求链接 作业要求 团队名称 TheSuperego 团队成员分工描述 杨丽霞:组织QQ会议,合理明确组内分工,推进任务,实施关于我们原型设计陈来弟:负 ...

  2. Tensor Core技术解析(上)

    Tensor Core技术解析(上) NVIDIA在SIGGRAPH 2018上正式发布了新一代GPU架构--Turing(图灵),黄仁勋称Turing架构是自2006年CUDA GPU发明以来最大的 ...

  3. C# & JS 判断字符串是否为日期格式

    在C#中,对格式的判断有一类专门函数,那就是TryParse.TryParse在各个不同的类型类(如int,string,DateTime)中,都是存在的.在TryParse中一般有两个参数,一个是待 ...

  4. jemeter压测, 高级应用: 发1万个请求,每个请求参数都不同, 使用CSV数据文件配置

    今天接到一个压测任务, 数据源需要自己从测试环境库中取, 并且使用jemeter 请求, 每个请求参数都不相同 这里使用jemeter的 CSV数据文件来配置: 这样配置好后, 开始发送请求: csv ...

  5. QT环境变量设置

    首先找到自己对应的目录 我的如图 还有一个路径 将这两个路径添加到系统变量的Path中

  6. 【NX二次开发】Block UI 选择小平面区域

    属性说明 属性   类型   描述   常规           BlockID    String    控件ID    Enable    Logical    是否可操作    Group    ...

  7. ipconfig提示不是内部或外部命令

    昨天因为公司断网,重新连上之后ip地址变了,于是就想看看现在的ip是什么 输入ipconfig,回车 提示不是外部和内部命令,是因为系统在本路径下未找到ipconfig.exe系统,所以无法识别ipc ...

  8. 快速了解ARP

    目录 前言 一.MAC 1.MAC地址三种帧 二.ARP 1.五种ARP 三.ARP老化 四.什么时候会发送免费ARP 五.代理ARP 六.ARP欺骗 总结 前言 分别介绍MAC地址和五种ARP报文 ...

  9. 低代码Paas开发平台可以本地实施吗

    低代码Paas开发平台可以本地实施吗?答案是肯定的.虽然低代码开发通常是以云端形式面向用户,也就是我们经常看到到aPaaS,而它也更加倾向于SaaS.但实际上,低代码开发平台是可以支持本地部署的,例如 ...

  10. SQL反模式读书笔记思维导图

    在写SQL过程以及设计数据表的过程中,我们经常会走一些弯路,会做一些错误的设计.<SQL反模式>这本书针对这些经常容易出错的设计模式进行分析,解释了错误的理由.允许错误的场景,并给出更好的 ...