python pyinsane应用
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应用的更多相关文章
- PYTHON PIP 快速安装
清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https: ...
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- Python 小而美的函数
python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况 any any(iterable) ...
- JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
- 使用Python保存屏幕截图(不使用PIL)
起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...
- Python编码记录
字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...
- Apache执行Python脚本
由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...
随机推荐
- 第三方数据格式库protobuf
protobuf初识 protobuf是一种高效的数据格式,平台无关.语言无关.可扩展,可用于 RPC 系统和持续数据存储系统. protobuf protobuf介绍 Protobuf是Protoc ...
- TVM优化GPU机器翻译
TVM优化GPU机器翻译 背景 神经机器翻译(NMT)是一种自动化的端到端方法,具有克服传统基于短语的翻译系统中的弱点的潜力.最近,阿里巴巴集团正在为全球电子商务部署NMT服务. 将Transform ...
- Kubeedge Edged概述
Kubeedge Edged概述 Overview EdgeD是管理节点生命周期的边缘节点模块.它可以帮助用户在边缘节点上部署容器化的工作负载或应用程序.这些工作负载可以执行任何操作,从简单的远程遥测 ...
- gst-crypto GStreamer插件
gst-crypto GStreamer插件 内容 1. gst-crypto概述 1.1gst-crypto GStreamer插件功能 1.2用例范例 2. GStreamer插件支持 3. 在本 ...
- LLVM编译器架构
LLVM编译器架构 LLVM概述 LLVM项目是模块化和可重用的编译器及工具链技术的集合.尽管名称如此,LLVM与传统虚拟机关系不大.名称" LLVM"本身不是缩写.它是项目的全名 ...
- 如何在TVM上集成Codegen(下)
如何在TVM上集成Codegen(下) Bring DNNL to TVM: JSON Codegen/Runtime 现在实现将中继图序列化为JSON表示的DNNL codegen,然后实现DNNL ...
- CVPR2020:训练多视图三维点云配准
CVPR2020:训练多视图三维点云配准 Learning Multiview 3D Point Cloud Registration 源代码和预训练模型:https://github.com/zgo ...
- 深入理解java虚拟机笔记Chapter3-内存分配策略
内存分配策略 新生代和老年代的 GC 操作 新生代 GC 操作:Minor GC 发生的非常频繁,速度较块. 老年代 GC 操作:Full GC / Major GC 经常伴随着至少一次的 Minor ...
- 03:进程Queue --- 生产者消费者模型
1 进程Queue介绍 1 进程间数据隔离,两个进程进行通信,借助于Queue2 进程间通信:IPC -借助于Queue实现进程间通信 -借助于文件 -借助于数据库 -借助 ...
- 6.17考试总结(NOIP模拟8)[星际旅行·砍树·超级树·求和]
6.17考试总结(NOIP模拟8) 背景 考得不咋样,有一个非常遗憾的地方:最后一题少取膜了,\(100pts->40pts\),改了这么多年的错还是头一回看见以下的情景... T1星际旅行 前 ...