コード例 #1
0
ファイル: bpgviewer.py プロジェクト: simscolony/pybpgviewer
 def bpgdecode(self,filename):
     msg=None
     cmd=self.bpgpath
     self.frames_index=0
     if len(self.frames): self.frames=[]
     if self.img:
         del self.img
         self.img=None
     if len(filename)>4 and filename[-4:].lower()=='.bpg':
         try:
             if not(isfile(filename) and access(filename,R_OK)):
                 msg=_('Unable to open')+'\"%s\"!'%filename
         except: return False
         if not(msg):
             err=0
             try:
                 imbuffer=''
                 if osflag:
                     fifo=osopen(self.fifo,O_RDONLY|O_NONBLOCK)
                     cmd+=' "'+realpath(filename)+'" '+self.fifo+\
                         ' >/dev/null 2>&1'
                     f=Popen(cmd,shell=True,stdin=None,stdout=None,\
                         stderr=None)
                     if fifo:
                         while True:
                             if f.poll()!=None: break;
                             try: data=osread(fifo,16777216)
                             except OSError as e:
                                 if e.errno==errno.EAGAIN or\
                                     e.errno==errno.EWOULDBLOCK: data=''
                                 else: raise
                             if len(data): imbuffer+=data
                         osclose(fifo)
                 else:
                     si=STARTUPINFO()
                     si.dwFlags|=1
                     si.wShowWindow=0
                     pname='\\\\.\\pipe\\'+basename(self.fifo)
                     tpipe=win32pipe.CreateNamedPipe(
                         pname,
                         win32pipe.PIPE_ACCESS_DUPLEX,
                         win32pipe.PIPE_TYPE_BYTE|win32pipe.PIPE_WAIT,
                         1,16777216,16777216,2000,None)
                     cmd+=' "'+realpath(filename)+'" '+pname
                     f=Popen(cmd,shell=False,stdin=None,stdout=None,\
                         stderr=None,bufsize=0,startupinfo=si)
                     win32pipe.ConnectNamedPipe(tpipe,None)
                     imbuffer=''
                     if version_info[0]<3: imbuffer=''
                     else: imbuffer=b''
                     if tpipe:
                         while True:
                             data=None
                             try: data=win32file.ReadFile(tpipe,16777216)
                             except: data=None
                             if not(data): break
                             if data[0]!=0: break
                             if len(data[1]): imbuffer+=data[1]
                     win32pipe.DisconnectNamedPipe(tpipe)
                     f.wait()
                 if len(imbuffer):
                     x,=unpack("i",imbuffer[0:4])
                     y,=unpack("i",imbuffer[4:8])
                     n,=unpack("i",imbuffer[8:12])
                     d,=unpack("i",imbuffer[12:16])
                     if n==0 and d==1:
                         try:
                             self.img=Image.frombytes('RGBA',(x,y),
                                 imbuffer[16:])
                         except: err=1
                     else:
                         self.scale=100.0
                         self.autoscale=self.scale
                         self.bitmap_text=str(x)+'x'+str(y)
                         self.imginfo='%.2f'%self.scale+'%@'+self.bitmap_text
                         ishift=8
                         dr=n*1000/d
                         while True:
                             try:
                                 n,=unpack("i",imbuffer[ishift:ishift+4])
                                 ishift+=4
                                 d,=unpack("i",imbuffer[ishift:ishift+4])
                                 ishift+=4
                             except: break
                             try:
                                 img=Image.frombytes('RGBA',(x,y),
                                     imbuffer[ishift:])
                             except: break
                             ishift+=(x*y*4)
                             self.frames.append([self.bitmapfrompil(img),n*1000/d])
                         else: err=1
                     del imbuffer
                 else: err=1
             except: err=1
             if err: msg=_('BPG decoding error!\n')
     else: msg=_('File')+' \"%s\" '%filename+_('is not a BPG-File!')
     if msg:
         wx.PostEvent(self,ShowEvent(SE_EVT_TYPE,-1,value=msg))
         if self.img:
             del self.img
             self.img=None
     else: return True
     return False
コード例 #2
0
ファイル: bpgviewer.py プロジェクト: asimba/pybpgviewer
 def bpgdecode(self,filename):
     msg=None
     cmd='"'+self.bpgpath+'"'
     self.frames_index=0
     if len(self.frames): self.frames=[]
     if self.img:
         del self.img
         self.img=None
     if len(filename)>4 and filename[-4:].lower()=='.bpg':
         try:
             if not(isfile(filename) and access(filename,R_OK)):
                 msg=_('Unable to open')+'\"%s\"!'%filename
         except: return False
         if not(msg):
             err=0
             try:
                 if version_info[0]<3: imbuffer=''
                 else: imbuffer=b''
                 t,tmp=mkstemp(suffix='.rgb',prefix='')
                 close(t)
                 cmd+=' "'+realpath(filename)+'" '+tmp+' >/dev/null 2>&1'
                 f=Popen(cmd,shell=True,stdin=None,stdout=None,stderr=None)
                 f.wait()
                 fifo=osopen(tmp,O_RDONLY|O_NONBLOCK)
                 if fifo:
                     while True:
                         try: data=osread(fifo,16777216)
                         except OSError as e:
                             if e.errno==errno.EAGAIN or\
                                 e.errno==errno.EWOULDBLOCK:
                                     data=''
                                     continue
                             else: raise
                         if len(data): imbuffer+=data
                         else: break
                     osclose(fifo)
                 if exists(tmp):
                     try: remove(tmp)
                     except: pass
                 if len(imbuffer):
                     x,=unpack("i",imbuffer[0:4])
                     y,=unpack("i",imbuffer[4:8])
                     n,=unpack("i",imbuffer[8:12])
                     d,=unpack("i",imbuffer[12:16])
                     if n==0 and d==1:
                         try:
                             self.img=Image.frombytes('RGBA',(x,y),
                                 imbuffer[16:])
                         except: err=1
                     else:
                         self.scale=100.0
                         self.autoscale=self.scale
                         self.bitmap_text=str(x)+'x'+str(y)
                         self.imginfo='%.2f'%self.scale+'%@'+self.bitmap_text
                         ishift=8
                         dr=n*1000/d
                         while True:
                             try:
                                 n,=unpack("i",imbuffer[ishift:ishift+4])
                                 ishift+=4
                                 d,=unpack("i",imbuffer[ishift:ishift+4])
                                 ishift+=4
                             except: break
                             try:
                                 img=Image.frombytes('RGBA',(x,y),
                                     imbuffer[ishift:])
                             except: break
                             ishift+=(x*y*4)
                             self.frames.append([self.bitmapfrompil(img),n*1000/d])
                         else: err=1
                     del imbuffer
                 else: err=1
             except: err=1
             if err: msg=_('BPG decoding error!\n')
     else: msg=_('File')+' \"%s\" '%filename+_('is not a BPG-File!')
     if msg:
         wx.PostEvent(self,ShowEvent(SE_EVT_TYPE,-1,value=msg))
         if self.img:
             del self.img
             self.img=None
     else: return True
     return False
コード例 #3
0
 def bpgdecode(self, filename):
     msg = None
     cmd = '"' + self.bpgpath + '"'
     self.frames_index = 0
     if len(self.frames): self.frames = []
     if self.img:
         del self.img
         self.img = None
     if len(filename) > 4 and filename[-4:].lower() == '.bpg':
         try:
             if not (isfile(filename) and access(filename, R_OK)):
                 msg = _('Unable to open') + '\"%s\"!' % filename
         except:
             return False
         if not (msg):
             err = 0
             try:
                 if version_info[0] < 3: imbuffer = ''
                 else: imbuffer = b''
                 t, tmp = mkstemp(suffix='.rgb', prefix='')
                 close(t)
                 cmd += ' "' + realpath(
                     filename) + '" ' + tmp + ' >/dev/null 2>&1'
                 f = Popen(cmd,
                           shell=True,
                           stdin=None,
                           stdout=None,
                           stderr=None)
                 f.wait()
                 fifo = osopen(tmp, O_RDONLY | O_NONBLOCK)
                 if fifo:
                     while True:
                         try:
                             data = osread(fifo, 16777216)
                         except OSError as e:
                             if e.errno==errno.EAGAIN or\
                                 e.errno==errno.EWOULDBLOCK:
                                 data = ''
                                 continue
                             else:
                                 raise
                         if len(data): imbuffer += data
                         else: break
                     osclose(fifo)
                 if exists(tmp):
                     try:
                         remove(tmp)
                     except:
                         pass
                 if len(imbuffer):
                     x, = unpack("i", imbuffer[0:4])
                     y, = unpack("i", imbuffer[4:8])
                     n, = unpack("i", imbuffer[8:12])
                     d, = unpack("i", imbuffer[12:16])
                     if n == 0 and d == 1:
                         try:
                             self.img = Image.frombytes(
                                 'RGBA', (x, y), imbuffer[16:])
                         except:
                             err = 1
                     else:
                         self.scale = 100.0
                         self.autoscale = self.scale
                         self.bitmap_text = str(x) + 'x' + str(y)
                         self.imginfo = '%.2f' % self.scale + '%@' + self.bitmap_text
                         ishift = 8
                         dr = n * 1000 / d
                         while True:
                             try:
                                 n, = unpack("i",
                                             imbuffer[ishift:ishift + 4])
                                 ishift += 4
                                 d, = unpack("i",
                                             imbuffer[ishift:ishift + 4])
                                 ishift += 4
                             except:
                                 break
                             try:
                                 img = Image.frombytes(
                                     'RGBA', (x, y), imbuffer[ishift:])
                             except:
                                 break
                             ishift += (x * y * 4)
                             self.frames.append(
                                 [self.bitmapfrompil(img), n * 1000 / d])
                         else:
                             err = 1
                     del imbuffer
                 else:
                     err = 1
             except:
                 err = 1
             if err: msg = _('BPG decoding error!\n')
     else: msg = _('File') + ' \"%s\" ' % filename + _('is not a BPG-File!')
     if msg:
         wx.PostEvent(self, ShowEvent(SE_EVT_TYPE, -1, value=msg))
         if self.img:
             del self.img
             self.img = None
     else:
         return True
     return False
コード例 #4
0
ファイル: log.py プロジェクト: charlesw1234/learning
 def readall(self, rfd):
     body, body0 = '', osread(rfd, 1)
     while body0:
         body += body0
         body0 = osread(rfd, 1)
     return body
コード例 #5
0
 def bpgdecode(self,filename):
     msg=None
     cmd=self.bpgpath+' -o '
     if self.img:
         del self.img
         self.img=None
     if len(filename)>4 and filename[-4:].lower()=='.bpg':
         try:
             if not(isfile(filename) and access(filename,R_OK)):
                 msg=_('Unable to open')+'\"%s\"!'%filename
         except: return False
         if not(msg):
             err=0
             try:
                 imbuffer=''
                 if osflag:
                     fifo=osopen(self.fifo,O_RDONLY|O_NONBLOCK)
                     cmd+=self.fifo+' "'+realpath(filename)+'"'+\
                         ' >/dev/null 2>&1'
                     f=Popen(cmd,shell=True,stdin=None,stdout=None,\
                         stderr=None)
                     if fifo:
                         while True:
                             if f.poll()!=None: break;
                             try: data=osread(fifo,16777216)
                             except OSError as e:
                                 if e.errno==errno.EAGAIN or\
                                     e.errno==errno.EWOULDBLOCK: data=''
                                 else: raise
                             if len(data): imbuffer+=data
                         osclose(fifo)
                 else:
                     si=STARTUPINFO()
                     si.dwFlags|=1
                     si.wShowWindow=0
                     pname='\\\\.\\pipe\\'+basename(self.fifo)
                     tpipe=win32pipe.CreateNamedPipe(
                         pname,
                         win32pipe.PIPE_ACCESS_DUPLEX,
                         win32pipe.PIPE_TYPE_BYTE|win32pipe.PIPE_WAIT,
                         1,16777216,16777216,2000,None)
                     cmd+=pname+' "'+realpath(filename)+'"'
                     f=Popen(cmd,shell=False,stdin=None,stdout=None,\
                         stderr=None,bufsize=0,startupinfo=si)
                     win32pipe.ConnectNamedPipe(tpipe,None)
                     imbuffer=''
                     if tpipe:
                         while True:
                             data=None
                             try: data=win32file.ReadFile(tpipe,16777216)
                             except: data=None
                             if not(data): break
                             if data[0]!=0: break
                             if len(data[1]): imbuffer+=data[1]
                     win32pipe.DisconnectNamedPipe(tpipe)
                     f.wait()
                 if len(imbuffer):
                     if imbuffer[0]=='a': mode='RGBA'
                     else: mode='RGB'
                     x,=unpack("i",imbuffer[1:5])
                     y,=unpack("i",imbuffer[5:9])
                     try: self.img=Image.fromstring(mode,(x,y),imbuffer[9:])
                     except: err=1
                     del imbuffer
                 else: err=1
             except: err=1
             if err: msg=_('BPG decoding error!\n')
     else: msg=_('File')+' \"%s\" '%filename+_('is not a BPG-File!')
     if msg:
         print msg
         errmsgbox(msg)
         if self.img:
             del self.img
             self.img=None
     else: return True
     return False
コード例 #6
0
ファイル: log.py プロジェクト: charlesw1234/learning
 def readall(self, rfd):
     body, body0 = '', osread(rfd, 1)
     while body0:
         body += body0
         body0 = osread(rfd, 1)
     return body