Exemple #1
0
 def OnButton1Button(self, event):
     self.ShowMoreToCome(0)
     try:
         self.Control.Log("self.SS.XferImageNatively()")
         self.Control.statusBar1.SetStatusText("7 - Transferring Data", 2)
         (handle, more_to_come) = self.SS.XferImageNatively()
         self.Control.Log(">> (0x%lx, %d)" % (handle, more_to_come))
         if more_to_come:
             self.Control.statusBar1.SetStatusText("6 - Data Available", 2)
         else:
             self.Control.statusBar1.SetStatusText(
                 "5 - Acquisition Requested", 2)
         frm = frmViewBmp.create(self)
         frm.SetImageFile(handle, self.Control)
         self.Control.Log("twain.GlobalHandleFree(0x%lx)" % handle)
         twain.GlobalHandleFree(handle)
         frm.Show(1)
         frm.Raise()
         if more_to_come:
             self.ShowMoreToCome(1)
         else:
             # Provide from variable, so parent can re-raise it
             self.frm = frm
             self.Close(1)
     except:
         self.Control.DisplayException("self.SS.XferImageNatively")
    def multiScan(self):
        """ Scan and return an array of PIL objects 
            If no images, will return an empty array
        """
        if self.scanner == None:
            raise ScannerNotSet

        self.scanner.RequestAcquire(0, 1)  # RequestAcquire(ShowUI, ShowModal)
        info = self.scanner.GetImageInfo()
        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
Exemple #3
0
 def capture(self):
     try:
         print("capture()")
         (handle, more_to_come) = self.scanner.XferImageNatively()
     except twain.excDSTransferCancelled:
         return None
     self.scannedImages.append(
         Image.open(StringIO(twain.DIBToBMFile(handle))))
     twain.GlobalHandleFree(handle)
     return True
 def capture(self):
     try:
         print("capture()")
         (handle, more_to_come) = self.scanner.XferImageNatively()
     except twain.excDSTransferCancelled:
         self.close()
         return None
     image = twain.DIBToBMFile(handle)
     twain.GlobalHandleFree(handle)
     return image
def scan(fullfilename = 'dg_pic.bmp', **kwargs):
    sm, scanner = open_scanner()
    try:
        adjust_scanner_properties(scanner, **kwargs)
        scanner.RequestAcquire(0, 0) # 1,1 to show scanner user interface
        (handle, more_to_come) = scanner.XferImageNatively()
        twain.DIBToBMFile(handle, fullfilename)
        twain.GlobalHandleFree(handle)
    finally:
        scanner.destroy()
        sm.destroy()
Exemple #6
0
 def scan(self):
     """Scan and return PIL object if success else return False
     """
     self.scanner.RequestAcquire(0, 1)
     info = self.scanner.GetImageInfo()
     try:
         self.handle = self.scanner.XferImageNatively()[0]
         image = twain.DIBToBMFile(self.handle)
         twain.GlobalHandleFree(self.handle)
         return Image.open(StringIO(image))
     except:
         return False
Exemple #7
0
    def scan(self, dpi=200):
        self._open(dpi)
        self._scanner.RequestAcquire(0, 0)
        info = self._scanner.GetImageInfo()
        if info:
            (handle, more_to_come) = self._scanner.XferImageNatively()
            str_image = twain.DIBToBMFile(handle)
            twain.GlobalHandleFree(handle)
            self._close()
            return Image.open(StringIO(str_image))

        self._close()
        return None
 def capture(self):
     fileName = "tmp.tmp"
     try:
         (handle, more_to_come) = self.source.XferImageNatively()
     except:
         return None
     twain.DIBToBMFile(handle, fileName)
     twain.GlobalHandleFree(handle)
     image = QImage(fileName)
     res = float(self.resolution) * 1000 / 25.4
     image.setDotsPerMeterX(res)
     image.setDotsPerMeterY(res)
     return image
Exemple #9
0
    def ProcessXFer(self):
        """An image is ready at the scanner - fetch and display it"""
        more_to_come = False
        try:
            if self.XferMethod == XferNatively:
                XferFileName = tmpfilename
                (handle, more_to_come) = self.SD.XferImageNatively()
                twain.DIBToBMFile(handle, XferFileName)
                twain.GlobalHandleFree(handle)
                self.LogMessage(self.ProductName + ':' +
                                'Image acquired natively')
            else:
                try:
                    XferFileName = 'TWAIN.TMP'  # Default
                    rv = self.SD.GetXferFileName()
                    if rv:
                        (XferFileName, type) = rv

                    # Verify that the transfer file can be produced. Security
                    # configurations on windows can prevent it working.
                    try:
                        self.VerifyCanWrite(XferFileName)
                    except CannotWriteTransferFile:
                        self.SD.SetXferFileName(OverrideXferFileName)
                        XferFileName = OverrideXferFileName

                except:
                    # Functionality to influence file name is not implemented.
                    # The default is 'TWAIN.TMP'
                    pass

                self.VerifyCanWrite(XferFileName)
                self.SD.XferImageByFile()
                self.LogMessage(self.ProductName + ':' +
                                "Image acquired by file (%s)" % XferFileName)

            self.DisplayImage(XferFileName)
            if more_to_come: self.AcquirePending = True
            else: self.SD = None
        except:
            # Display information about the exception
            import sys, traceback
            ei = sys.exc_info()
            traceback.print_exception(ei[0], ei[1], ei[2])
Exemple #10
0
    def scan(self, output_name):
        """
        scan and return PIL object if sucess else return False.
        The input is the complete name of the file 
        to scan (directory + filename).
        For the moment only tesed JPEF format.
        TODO: test other formats
        
        """
        print("enter in the scan method..")

        self.scanner.RequestAcquire(0, 1)
        info = self.scanner.GetImageInfo()

        self.handle = self.scanner.XferImageNatively()[0]
        img = twain.DIBToBMFile(self.handle, output_name)
        twain.GlobalHandleFree(self.handle)
        img_scanned = Image.open(output_name)
        img_scanned.save(output_name,
                         self.config.scan_format,
                         dpi=(self.config.dpi, self.config.dpi))
        return img_scanned
Exemple #11
0
    async def scan(self, callback, device=None, dpi=None):
        if not self.sourceManager:
            self.start()
        devices = self.getScanners()
        if device:
            self.setScanner(device)
        else:
            self.setScanner(devices[0])
        print(self.scanner)

        if dpi:
            self.setDPI(dpi)

        self.setPixelType("color")
        self.setScanArea()

        self.scanner.RequestAcquire(0, 1)
        info = self.scanner.GetImageInfo()
        try:
            self.handle = self.scanner.XferImageNatively()[0]
            image = twain.DIBToBMFile(self.handle)
            twain.GlobalHandleFree(self.handle)
            self.close()

            #check the folder exists
            if not os.path.exists("temp"):
                os.mkdir("temp", 0o777)

            img = Image.open(BytesIO(image))
            filename = str(time.time()) + '.jpg'
            img.save('temp/' + filename)
            await callback(filename)
            return True
        except Exception as e:
            print(e)
            return False
Exemple #12
0
    def processTransfer(self, event):
        directory = "\\\\files.dcarf\\teleactive\\PDFs\\To be processed\\"
        #directory = "H:\\Desktop\\"
        #temp_directory = "\\\\files.dcarf\\tmp\\MailTracking\\"
        #from time import strftime,localtime
        #filename = strftime("%Y-%m-%d-%H-%M-%S",localtime()) + ".pdf"

        #if os.access(directory, os.W_OK):
        #	c = canvas.Canvas(directory + filename)
        #elif os.access(temp_directory, os.W_OK):
        #	print "Unable to write to teleactive directory. Writing output " + \
        #			"to " + temp_directory
        #	c = canvas.Canvas(temp_directory + filename)
        #else:
        #	print "Unable to write output file. Terminating scan"
        #	return

        #c.setPageCompression(1)

        remaining = 1

        if self.control.GetValue() == "":
            from time import strftime, localtime
            self.control.SetValue(strftime("%Y-%m-%d-%H-%M-%S", localtime()))

        val = self.control3.GetValue() + self.control.GetValue()
        count = 0
        c = 0
        filename = ""

        #print self.calendar.GetDate()

        while remaining > 0:
            #print "Remaining gt 0 : %s" % remaining
            val = self.control3.GetValue() + self.control.GetValue()
            count = 0
            c = 0
            self.numPages = int(self.control2.GetValue())

            while self.numPages > 0:
                count = count + 1

                #print "COUNT: %s PAGES: %s REMAINING: %s" % (count,self.numPages,remaining)
                #fn = temp_directory + "tmp" + str(count) + ".gif"
                #tmp_string = StringIO.StringIO()
                (handle, remaining) = self.scanner.XferImageNatively()

                #print "handle %s" % handle
                #print "remaining %s" % remaining

                #twain.DIBToBMFile(handle, fn)
                #Image.open(StringIO.StringIO(twain.DIBToBMFile(handle))).save( \
                #		fn, "GIF")
                temp_img = Image.open(
                    StringIO.StringIO(twain.DIBToBMFile(handle)))
                (width, height) = temp_img.size

                if self.radio1.GetSelection() == 0:  #A3 - so split page
                    a4a = temp_img.crop((0, 0, int(width) / 2, height))
                    a4a.load()

                    a4b = temp_img.crop((int(width) / 2, 0, width, height))
                    a4b.load()

                    img = temp_img.resize((int(width) / 2, int(height) / 2), \
                      Image.ANTIALIAS)

                    (width, height) = a4a.size
                    imga = a4a.resize((int(width) / 2, int(height) / 2), \
                      Image.ANTIALIAS)
                    (width, height) = a4b.size
                    imgb = a4b.resize((int(width) / 2, int(height) / 2), \
                      Image.ANTIALIAS)
                else:  # A4 don't split
                    img = temp_img.resize((int(width) / 2, int(height) / 2), \
                      Image.ANTIALIAS)

                twain.GlobalHandleFree(handle)

                if c == 0:
                    filename = val + ".pdf"
                    if os.access(directory, os.W_OK):
                        c = canvas.Canvas(directory + filename)
                    c.setPageCompression(1)

                if self.radio1.GetSelection() == 0:  #A3 - so split page
                    c.drawInlineImage(imga, 0, 0, 600, 849)
                    c.showPage()

                    c.drawInlineImage(imgb, 0, 0, 600, 849)
                    c.showPage()
                else:  # A4 don't split
                    c.drawInlineImage(img, 0, 0, 600, 849)
                    c.showPage()

                self.numPages = self.numPages - 1

            c.save()
            print "SAVED to %s.pdf" % val
            if self.radio3.GetSelection() != 0:
                upload(directory + filename, self.control.GetValue()).start()
            self.control.SetValue("")
Exemple #13
0
    sm = twain.SourceManager(0)
    ss = sm.OpenSource()
    ss.RequestAcquire(0, 0)

    # set capability
    try:
        ss.SetCapability(twain.ICAP_YRESOLUTION, twain.TWTY_FIX32, 200.0)
        ss.SetCapability(twain.ICAP_XRESOLUTION, twain.TWTY_FIX32, 200.0)
    except Exception as e:
        print str(e.message)

    rv = ss.XferImageNatively()
    if rv:
        (handle, count) = rv
        str_image = twain.DIBToBMFile(handle)
        twain.GlobalHandleFree(handle)
        # tampilkan object file di command prompt
        print Image.open(StringIO(str_image))
elif (feeder == True):
    index = 0

    def next(ss):
        try:
            # cetak image info
            # tidak ditampilkan hanya sebagai checking image saja
            ss.GetImageInfo()
            return True
        except:
            return False

    def capture(ss):