python twain 扫描

复制代码
def multiScan(self):
    """ Scan and return an array of PIL objects 
        If no images, will return an empty array
    """

    self.scanner.RequestAcquire(0, 1)
    images = []
    handles = []
    try:
        handle, more = self.scanner.XferImageNatively()
        handles.append(handle)
    except twain.excDSTransferCancelled:
        return []
    while more != 0:
        try:
            handle, more = self.scanner.XferImageNatively()
            handles.append(handle)
        except twain.excDSTransferCancelled:
            more = 0

    for handle in handles:
        images.append(Image.open(StringIO(twain.DIBToBMFile(handle))))
        twain.GlobalHandleFree(handle)

    return images
复制代码

 

复制代码
import twain

sm = twain.SourceManager(0)
ss = sm.OpenSource()

for i in range(3):  //for ex. 3 documents in the scanner device
   ss.RequestAcquire(0,0)
   rv = ss.XferImageNatively()
   if rv:
       (handle, count) = rv
       twain.DIBToBMFile(handle, '{i}.bmp'.format(i))
复制代码

 

复制代码
import twain

sm = twain.SourceManager(0)
sm.SetCallback(onTwainEvent)
ss = sm.OpenSource()
index = 0

for i in range(3):  //for ex. 3 documents in the scanner device
   ss.RequestAcquire(0,0)

def onTwainEvent(event):
    if event == twain.MSG_XFERREADY:
        saveImage()

def saveImage():
    rv = ss.XferImageNatively()
    if rv:
        (handle, count) = rv
        twain.DIBToBMFile(handle, '{index}.bmp'.format(index))
        index += 1
复制代码

 

import twain 
sm = twain.SourceManager(0) 
ss = sm.OpenSource() 
ss.RequestAcquire(0,0) 
rv = ss.XferImageNatively() 
if rv: 
(handle, count) = rv 
twain.DIBToBMFile(handle, 'image.bmp')

 1、降低扫描分辨率、能用灰度就不用彩色、能用24位彩色就不用32位彩色

2、使用自动进纸器
3、采用高压缩比的图像格式,能用jpg就不用tiff,能用tiff就不用bmp
Source.GetCapabilityDefault(CapabilityCode)
Source.GetCapability(CapabilityCode)
Source.GetCapabilityCurrent(CapabilityCode)
Source.SetCapability(CapabilityCode)
Source.ResetCapability(CapabilityCode)
Source.GetImageInfo():返回XResolution, YResolution, ImageWidth, ImageLength, SamplesPerPixel, BitsPerSample, BitsPerPixel, Planar, PixelType, Compression
Source.GetImageLayout():返回((left, top, right, bottom) document_number, page_number, frame_number)

One of the CAP_* (Generic Capabilities), ICAP_ (Image Capabilities)

 

posted @   myrj  阅读(1395)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示