def onTimer(self): index = self.value() target = os.path.join(self.bookPath, '%05d.png' % index) source = unicode(self.book.images[index]) if index == 0: try: if not os.path.isdir(self.bookPath): os.makedirs(self.bookPath) except OSError: QtGui.QMessageBox.critical( self, 'Mangle', 'Cannot create directory %s' % self.bookPath) self.close() return try: base = os.path.join(self.bookPath, unicode(self.book.title)) mangaName = base + '.manga' if self.book.overwrite or not os.path.isfile(mangaName): manga = open(mangaName, 'w') manga.write('\x00') manga.close() mangaSaveName = base + '.manga_save' if self.book.overwrite or not os.path.isfile(mangaSaveName): mangaSave = open(base + '.manga_save', 'w') saveData = u'LAST=/mnt/us/pictures/%s/%s' % ( self.book.title, os.path.split(target)[1]) mangaSave.write(saveData.encode('utf-8')) mangaSave.close() except IOError: QtGui.QMessageBox.critical( self, 'Mangle', 'Cannot write manga file(s) to directory %s' % self.bookPath) self.close() return False self.setLabelText('Processing %s...' % os.path.split(source)[1]) try: if self.book.overwrite or not os.path.isfile(target): image.convertImage(source, target, str(self.book.device), self.book.imageFlags) if self.archive is not None: self.archive.addFile(target) except RuntimeError, error: result = QtGui.QMessageBox.critical( self, 'Mangle', str(error), QtGui.QMessageBox.Abort | QtGui.QMessageBox.Ignore, QtGui.QMessageBox.Ignore) if result == QtGui.QMessageBox.Abort: self.close() return
def onTimer(self): index = self.value() target = os.path.join(self.bookPath, '%05d.png' % index) source = unicode(self.book.images[index]) if index == 0: try: if not os.path.isdir(self.bookPath): os.makedirs(self.bookPath) except OSError: QtGui.QMessageBox.critical(self, 'Mangle', 'Cannot create directory %s' % self.bookPath) self.close() return try: base = os.path.join(self.bookPath, unicode(self.book.title)) mangaName = base + '.manga' if self.book.overwrite or not os.path.isfile(mangaName): manga = open(mangaName, 'w') manga.write('\x00') manga.close() mangaSaveName = base + '.manga_save' if self.book.overwrite or not os.path.isfile(mangaSaveName): mangaSave = open(base + '.manga_save', 'w') saveData = u'LAST=/mnt/us/pictures/%s/%s' % (self.book.title, os.path.split(target)[1]) mangaSave.write(saveData.encode('utf-8')) mangaSave.close() except IOError: QtGui.QMessageBox.critical(self, 'Mangle', 'Cannot write manga file(s) to directory %s' % self.bookPath) self.close() return False self.setLabelText('Processing %s...' % os.path.split(source)[1]) try: if self.book.overwrite or not os.path.isfile(target): image.convertImage(source, target, str(self.book.device), self.book.imageFlags) if self.archive is not None: self.archive.addFile(target) except RuntimeError, error: result = QtGui.QMessageBox.critical( self, 'Mangle', str(error), QtGui.QMessageBox.Abort | QtGui.QMessageBox.Ignore, QtGui.QMessageBox.Ignore ) if result == QtGui.QMessageBox.Abort: self.close() return
def onTimer(self): index = self.value() directory = os.path.join(unicode(self.directory), unicode(self.book.title)) target = os.path.join(directory, "%05d.png" % index) source = unicode(self.book.images[index]) if index == 0: try: if not os.path.isdir(directory): os.makedirs(directory) except OSError: QtGui.QMessageBox.critical(self, "Mangle", "Cannot create directory %s" % directory) self.close() return try: base = os.path.join(directory, unicode(self.book.title)) mangaName = base + ".manga" if self.book.overwrite or not os.path.isfile(mangaName): manga = open(mangaName, "w") manga.write("\x00") manga.close() mangaSaveName = base + ".manga_save" if self.book.overwrite or not os.path.isfile(mangaSaveName): mangaSave = open(base + ".manga_save", "w") saveData = u"LAST=/mnt/us/pictures/%s/%s" % (self.book.title, os.path.split(target)[1]) mangaSave.write(saveData.encode("utf-8")) mangaSave.close() except IOError: QtGui.QMessageBox.critical(self, "Mangle", "Cannot write manga file(s) to directory %s" % directory) self.close() return False self.setLabelText("Processing %s..." % os.path.split(source)[1]) try: if self.book.overwrite or not os.path.isfile(target): image.convertImage(source, target, str(self.book.device), self.book.imageFlags) except RuntimeError, error: result = QtGui.QMessageBox.critical( self, "Mangle", str(error), QtGui.QMessageBox.Abort | QtGui.QMessageBox.Ignore, QtGui.QMessageBox.Ignore ) if result == QtGui.QMessageBox.Abort: self.close() return
def convertPage(book, index): directory = os.path.join(unicode(book.directory), unicode(book.title)) target = os.path.join(directory, "%05d.png" % index) source = unicode(book.images[index]) if index == 0: try: if not os.path.isdir(directory): os.makedirs(directory) except OSError: print("Cannot create directory %s" % directory) sys.exit(1) return try: base = os.path.join(directory, unicode(book.title)) mangaName = base + ".manga" if book.overwrite or not os.path.isfile(mangaName): manga = open(mangaName, "w") manga.write("\x00") manga.close() mangaSaveName = base + ".manga_save" if book.overwrite or not os.path.isfile(mangaSaveName): mangaSave = open(base + ".manga_save", "w") saveData = u"LAST=/mnt/us/pictures/%s/%s" % (book.title, os.path.split(target)[1]) mangaSave.write(saveData.encode("utf-8")) mangaSave.close() except IOError: print("Cannot write mange file(s) to directory %s" % directory) sys.exit(1) return False print("Converting %d of %d: %s" % (index + 1, len(book.images), source)) try: if book.overwrite or not os.path.isfile(target): image.convertImage(source, target, str(book.device), book.imageFlags) except RuntimeError, error: print("Encountered an error: %s" % error) sys.exit(1) return
sourceFolder = os.path.join(root, folder) if not os.path.isdir(sourceFolder): continue targetFolder = os.path.join(target, folder) if not os.path.exists(targetFolder): os.mkdir(targetFolder) print targetFolder for (idx, f) in enumerate(sorted(os.listdir(sourceFolder))): sf = os.path.join(sourceFolder, f) tfName = "%02d-%s.png" % (idx, os.path.splitext(f)[0]) if idx == 0: mangaName = os.path.join(targetFolder, folder + ".manga") manga = open(mangaName, "w") manga.write("\x00") manga.close() mangaSaveName = os.path.join(targetFolder, folder + ".manga_save") mangaSave = open(mangaSaveName, "w") saveData = u"LAST=/mnt/us/pictures/%s/%s" % (folder, tfName) mangaSave.write(saveData.encode("utf-8")) mangaSave.close() tf = os.path.join(targetFolder, tfName) print tf if not os.path.isfile(tf): image.convertImage( sf, tf, "Kindle 3", image.ImageFlags.Orient | image.ImageFlags.Resize | image.ImageFlags.Frame | image.ImageFlags.Quantize, )
def convertAndSave(self, source, target, device, flags, archive, pdf): image.convertImage(source, target, device, flags) if archive is not None: archive.addFile(target) if pdf is not None: pdf.addImage(target)
def run(self): try: targets = image.convertImage(self.source, self.target, self.index, self.device, self.imageFlags, self.threshold) except RuntimeError, error: self.emitter.emit(QtCore.SIGNAL("threadError(QString)"),QtCore.QString(unicode(error))) return
def onTimer(self): index = self.value() out_dir = "" cbz = "" name_template = "%05d.png" if self.book.cbz: out_dir = os.path.split(self.target)[0] cbz = self.target else: out_dir = os.path.join(unicode(self.target), unicode(self.book.title)) source = unicode(self.book.images[index]) if index == 0: try: if not os.path.isdir(out_dir): os.makedirs(out_dir) except OSError: QtGui.QMessageBox.critical(self, 'Mangle', 'Cannot create directory %s' % out_dir) self.close() return try: base = os.path.join(out_dir, unicode(self.book.title)) # What, exactly, is this for? I never could figure it out. saveData = u'LAST=/mnt/us/pictures/%s/%s' % (self.book.title, name_template % self.counter) if not self.book.cbz: mangaName = '%s.manga' % base mangaSaveName = '%s.manga_save' % base if self.book.overwrite or not os.path.isfile(mangaName): manga = open(mangaName, 'w') manga.write('\x00') manga.close() if self.book.overwrite or not os.path.isfile(mangaSaveName): mangaSave = open(mangaSaveName, 'w') mangaSave.write(saveData.encode('utf-8')) mangaSave.close() elif self.book.cbz: mangaName = '%s.manga' % self.book.title mangaSaveName = '%s.manga_save' % self.book.title if os.path.isfile(cbz): # If the file already exists, get rid of it before we start writing to it, # since we open the zip file in append mode. os.remove(cbz) cbz_out = ZipFile(cbz, 'a', ZIP_DEFLATED, allowZip64 = True) cbz_out.writestr(mangaName, '\x00') cbz_out.writestr(mangaSaveName, saveData.encode('utf-8')) cbz_out.close() except IOError: QtGui.QMessageBox.critical(self, 'Mangle', 'Cannot write manga file(s) to directory %s' % out_dir) self.close() return False self.setLabelText('Processing %s...' % os.path.split(source)[1]) try: # Since splitting is an option, we can get multiple files back from # the convert operation, and it'll always be stored in a list. images = image.convertImage(source, str(self.book.device), self.book.imageFlags) # lol cheating #conv_img = images[0] for conv_img in images: # If we're exporting to file, we need the full path. Otherwise, we just need the filename. out_file = ( os.path.join(out_dir, name_template % self.counter) if not self.book.cbz else (name_template % self.counter) ) if (self.book.overwrite or not os.path.isfile(out_file)) and not self.book.cbz: try: conv_img.save(out_file) except IOError: raise RuntimeError('Cannot write image file %s' % out_file) if self.book.cbz: cbz_out = ZipFile(cbz, 'a', ZIP_DEFLATED, allowZip64 = True) # Write the image in PNG format to an object pretending to be a file, so we can # dump the string data into the CBZ file without saving to disk first. out_str = StringIO.StringIO() conv_img.save(out_str, format='PNG') cbz_out.writestr(out_file, out_str.getvalue()) out_str.close() cbz_out.close() # We're done with this image, so up the counter. self.counter = self.counter + 1 except RuntimeError, error: result = QtGui.QMessageBox.critical( self, 'Mangle', str(error), QtGui.QMessageBox.Abort | QtGui.QMessageBox.Ignore, QtGui.QMessageBox.Ignore ) if result == QtGui.QMessageBox.Abort: self.close() return
def onTimer(self): index = self.value() out_dir = "" cbz = "" epub = "" name_template = "%05d.png" if self.book.cbz: out_dir = os.path.split(self.target)[0] cbz = self.target else: out_dir = os.path.join(unicode(self.target), unicode(self.book.title)) source = unicode(self.book.images[index]) if index == 0: try: if not os.path.isdir(out_dir): os.makedirs(out_dir) except OSError: QtGui.QMessageBox.critical( self, 'Mangle', 'Cannot create directory %s' % out_dir) self.close() return try: base = os.path.join(out_dir, unicode(self.book.title)) # What, exactly, is this for? I never could figure it out. saveData = u'LAST=/mnt/us/pictures/%s/%s' % ( self.book.title, name_template % self.counter) if not self.book.cbz | self.book.epub: mangaName = '%s.manga' % base mangaSaveName = '%s.manga_save' % base if self.book.overwrite or not os.path.isfile(mangaName): manga = open(mangaName, 'w') manga.write('\x00') manga.close() if self.book.overwrite or not os.path.isfile( mangaSaveName): mangaSave = open(mangaSaveName, 'w') mangaSave.write(saveData.encode('utf-8')) mangaSave.close() elif self.book.cbz: mangaName = '%s.manga' % self.book.title mangaSaveName = '%s.manga_save' % self.book.title if os.path.isfile(cbz): # If the file already exists, get rid of it before we start writing to it, # since we open the zip file in append mode. os.remove(cbz) cbz_out = ZipFile(cbz, 'a', ZIP_DEFLATED, allowZip64=True) cbz_out.writestr(mangaName, '\x00') cbz_out.writestr(mangaSaveName, saveData.encode('utf-8')) cbz_out.close() except IOError: QtGui.QMessageBox.critical( self, 'Mangle', 'Cannot write manga file(s) to directory %s' % out_dir) self.close() return False self.setLabelText('Processing %s...' % os.path.split(source)[1]) try: # Since splitting is an option, we can get multiple files back from # the convert operation, and it'll always be stored in a list. images = image.convertImage(source, str(self.book.device), self.book.imageFlags) # lol cheating #conv_img = images[0] for conv_img in images: # If we're exporting to file, we need the full path. Otherwise, we just need the filename. out_file = (os.path.join(out_dir, name_template % self.counter) if not self.book.cbz else (name_template % self.counter)) if (self.book.overwrite or not os.path.isfile(out_file)) and not self.book.cbz: try: conv_img.save(out_file) if self.book.epub: epub_out = self.epub_out epub_out.setTitle(unicode(self.book.title)) item = epub_out.addImage( out_file, name_template % self.counter) page = epub_out.addHtmlForImage(item) epub_out.addSpineItem(page) if self.counter == 0: epub_out.addTocMapNode( page.destPath, "Page " + str(self.counter + 1)) except IOError: raise RuntimeError('Cannot write image file %s' % out_file) if self.book.cbz: cbz_out = ZipFile(cbz, 'a', ZIP_DEFLATED, allowZip64=True) # Write the image in PNG format to an object pretending to be a file, so we can # dump the string data into the CBZ file without saving to disk first. out_str = StringIO.StringIO() conv_img.save(out_str, format='PNG') cbz_out.writestr(out_file, out_str.getvalue()) out_str.close() cbz_out.close() # We're done with this image, so up the counter. self.counter = self.counter + 1 except RuntimeError, error: result = QtGui.QMessageBox.critical( self, 'Mangle', str(error), QtGui.QMessageBox.Abort | QtGui.QMessageBox.Ignore, QtGui.QMessageBox.Ignore) if result == QtGui.QMessageBox.Abort: self.close() return
def Export(self): if not os.path.isdir(self.directory): os.makedirs(self.directory ) hasDisplayLock = False ProgressBarTag = 'Converting ' + self.book.title if (not self.verbose): # Function Tries to acquire the lock if it succeeds it initialize the progress bar hasDisplayLock = ThreadProgressBar.AcquireDisplayLock(ProgressBarTag, len(self.book.images), False ) for index in range(0,len(self.book.images)): directory = os.path.join(unicode(self.directory), unicode(self.book.title)) source = unicode(self.book.images[index]) newSource = os.path.join(self.book.images[index]+"."+ imghdr.what(str(source))) target = os.path.join(directory, '%05d.png' % index) if index == 0: try: if not os.path.isdir(directory ): os.makedirs(directory ) except OSError: return try: base = os.path.join(directory, unicode(self.book.title)) mangaName = base + '.manga' if (self.verbose): print mangaName if self.book.overwrite or not os.path.isfile(mangaName): manga = open(mangaName, 'w') manga.write('\x00') manga.close() mangaSaveName = base + '.manga_save' if self.book.overwrite or not os.path.isfile(mangaSaveName): mangaSave = open(base + '.manga_save', 'w') saveData = u'LAST=/mnt/us/pictures/%s/%s' % (self.book.title, os.path.split(target)[1]) mangaSave.write(saveData.encode('utf-8')) mangaSave.close() except IOError: return False os.renames(source, newSource) try: if self.book.overwrite or not os.path.isfile(target): image.convertImage(newSource, target, str(self.book.device), self.book.imageFlags) if (self.verbose): print source + " -> " + target else: if (not hasDisplayLock): hasDisplayLock = ThreadProgressBar.AcquireDisplayLock(ProgressBarTag, len(self.book.images), False ) if (hasDisplayLock): ThreadProgressBar.UpdateProgressBar(index + 1) except RuntimeError, error: print "ERROR" finally:
def Export(self): if not os.path.isdir(self.directory): os.makedirs(self.directory) if (not self.verbose): outputIdx = self.outputMgr.createOutputObj( "Converting " + self.book.title, len(self.book.images)) for index in range(0, len(self.book.images)): directory = os.path.join(unicode(self.directory), unicode(self.book.title)) source = unicode(self.book.images[index]) newSource = os.path.join(self.book.images[index] + "." + imghdr.what(str(source))) target = os.path.join(directory, '%05d.png' % index) if (self.verbose): print(str(index) + " Target = " + target) if index == 0: try: if not os.path.isdir(directory): os.makedirs(directory) except OSError: return try: base = os.path.join(directory, unicode(self.book.title)) mangaName = base + '.manga' if (self.verbose): print mangaName if self.book.overwrite or not os.path.isfile(mangaName): manga = open(mangaName, 'w') manga.write('\x00') manga.close() mangaSaveName = base + '.manga_save' if self.book.overwrite or not os.path.isfile( mangaSaveName): mangaSave = open(base + '.manga_save', 'w') saveData = u'LAST=/mnt/us/pictures/%s/%s' % ( self.book.title, os.path.split(target)[1]) if (self.verbose): print("SaveData = " + saveData) mangaSave.write(saveData.encode('utf-8')) mangaSave.close() except IOError: return False os.renames(source, newSource) try: if self.book.overwrite or not os.path.isfile(target): image.convertImage(newSource, target, str(self.book.device), self.book.imageFlags) if (self.verbose): print source + " -> " + target else: self.outputMgr.updateOutputObj(outputIdx) except RuntimeError, error: print "ERROR" finally:
def Export(self): if not os.path.isdir(self.directory): os.makedirs(self.directory ) if (not self.verbose): outputIdx = self.outputMgr.createOutputObj("Converting "+self.book.title, len(self.book.images)) for index in range(0,len(self.book.images)): directory = os.path.join(unicode(self.directory), unicode(self.book.title)) source = unicode(self.book.images[index]) newSource = os.path.join(self.book.images[index]+"."+ imghdr.what(str(source))) target = os.path.join(directory, '%05d.png' % index) if (self.verbose): print(str(index) +" Target = " + target) if index == 0: try: if not os.path.isdir(directory ): os.makedirs(directory ) except OSError: return try: base = os.path.join(directory, unicode(self.book.title)) mangaName = base + '.manga' if (self.verbose): print mangaName if self.book.overwrite or not os.path.isfile(mangaName): manga = open(mangaName, 'w') manga.write('\x00') manga.close() mangaSaveName = base + '.manga_save' if self.book.overwrite or not os.path.isfile(mangaSaveName): mangaSave = open(base + '.manga_save', 'w') saveData = u'LAST=/mnt/us/pictures/%s/%s' % (self.book.title, os.path.split(target)[1]) if (self.verbose): print("SaveData = " + saveData) mangaSave.write(saveData.encode('utf-8')) mangaSave.close() except IOError: return False os.renames(source, newSource) try: if self.book.overwrite or not os.path.isfile(target): image.convertImage(newSource, target, str(self.book.device), self.book.imageFlags) if (self.verbose): print source + " -> " + target else: self.outputMgr.updateOutputObj( outputIdx ) except RuntimeError, error: print "ERROR" finally: