def convertFolder(input_folder, output_folder,quality=50,resize_type='width',
                   resize=700,valid_exts=['tif','jpg']):
    '''
    Resizes and compresses images in a folder to jpegs in another folder. Used
    to create thumbnails. 
    
    The method is used to create thumbnails for Goobi. As KBs Boobi is 
    configured to only look for images of the form NNNNNNN.jpg, images are named
    after this sequence.
    
    :param input_folder: Folder containing images to convert
    :param output_folder: Destination folder
    :param quality: % to compress output jpeg as
    :param resize_type: Resize by percentage or width. Default resize is the 
        desired width of the output image. Height will be set to keep aspect 
        ratio
    :param resize: the height or percentage to resize after.
    :param valid_exts: Only convert images with extensions in this list
    '''
    images = sorted([f for f in os.listdir(input_folder)
                     if os.path.splitext(f)[-1].lstrip('.') in valid_exts])

    for index, image in enumerate(images, start=1):
        # Handle spaces in filemames
        image = '"' + image + '"'
        input_path = os.path.join(input_folder,image)
        output_file_name = str(index).zfill(8)+'.jpg'
        output_path = os.path.join(output_folder,output_file_name)
        image_tools.compressFile(input_path,output_path,quality,resize,resize_type)
 def processFiles(self):
     '''
     Process all the files
     '''
     file_paths = sorted(self.img_proc_info['images'].keys())
     for file_path in file_paths:
         info = self.img_proc_info['images'][file_path]
         proc_time_stat = self.processFile(file_path,info)
         self.add_to_avg_time_stat(proc_time_stat)
         fs.clear_folder(self.temp_folder)
     if self.settings['has_binding'] and not self.settings['remove_binding']:
         for b in self.bindings:
             file_name,_ = os.path.splitext(os.path.basename(b.rstrip(os.sep)))
             b_pdf_dest = os.path.join(self.temp_pdf_folder,file_name+'.pdf')
             if self.settings['output_images']: shutil.copy2(b,self.output_image_location)
             if self.settings['output_pdf']: image_tools.compressFile(b,b_pdf_dest,resize=50,quality=33)
 def addBindingsToPdf(self):
     #=======================================================================
     # Get density for bw-pdf (i.e. DPI/PixelsPerInch)
     #=======================================================================
     density = pdf_tools.getDensity(src=self.pdf_bw_path,layer=0)
     #=======================================================================
     # Create temp folder for temp pdf-files
     #=======================================================================
     temp_folder = os.path.join(self.temp_root,self.process_title)
     tools.create_folder(temp_folder)
     #=======================================================================
     # Get path for first and last image
     #=======================================================================
     images = fs.getFilesInFolderWithExts(self.img_master_path,
                                          self.valid_exts,
                                          absolute=True)
     #=======================================================================
     # Create PDF of bindings (first and last image in master image folder)
     #=======================================================================
     front_image_path = images[0]
     end_image_path = images[-1]
     front_pdf_path = os.path.join(temp_folder,'front.pdf')
     end_pdf_path = os.path.join(temp_folder,'end.pdf') 
     image_tools.compressFile(input_file     = front_image_path, 
                              output_file    = front_pdf_path,
                              quality        = self.quality,
                              resize         = self.resize,
                              density        = density)
     image_tools.compressFile(input_file     = end_image_path, 
                              output_file    = end_pdf_path,
                              quality        = self.quality,
                              resize         = self.resize,
                              density        = density)
     #=======================================================================
     # Add front and back-binding to pdf
     #=======================================================================
     pdf_list = [front_pdf_path,self.pdf_bw_path,end_pdf_path]
     temp_dest = os.path.join(temp_folder,self.process_title+'.pdf')
     pdf_tools.joinPdfFiles(pdf_list, temp_dest)
     #=======================================================================
     # Move new pdf from temp to bw-pdf location (overwrite)
     #=======================================================================
     shutil.move(temp_dest, self.pdf_bw_path)
     #=======================================================================
     # Delete temp_folder
     #=======================================================================
     fs.clear_folder(temp_folder, also_folder=True)
 def processFile(self,file_path,info):
     time_stat = {}
     file_name,_ = os.path.splitext(os.path.basename(file_path.rstrip(os.sep)))
     if self.debug: self.logger.debug('File: {0}'.format(file_name))
     if info['crop']:
         #===================================================================
         # Crop image with coordinates, if cropping is turned on
         #===================================================================
         if self.debug: self.logger.debug('\tCrop image')
         t = time.time()
         file_path = image_tools.cropImage(file_path,self.temp_folder,info)
         time_stat['Crop image'] = time.time()-t
     
     if info['deskew']:
         #===================================================================
         # Deskew image, if deskew is turned on
         #===================================================================
         if self.debug: self.logger.debug('\tDeskew image')
         t = time.time()
         # Jeg har sat quality til 50% så de ikke fylder så meget når jeg skal gemme de resulterende jpgs til OCR
         file_path = image_tools.deskewImage(file_path,self.temp_folder,
                                             info['deskew_angle'],quality=50,
                                             resize=self.settings['output_resize'])
         time_stat['Deskew image'] = time.time()-t
     else:
         if self.debug: self.logger.debug('\tNo deskewing')
         t = time.time()
         file_name,_ = os.path.splitext(os.path.basename(file_path))
         dest = os.path.join(self.temp_folder,file_name+'compressed.jpg')
         image_tools.compressFile(file_path,dest,quality=50,
                                  resize=self.settings['output_resize'])
         file_path = dest
         time_stat['Compress/resize image'] = time.time()-t
     # 3: to pdf 
     if self.debug: self.logger.debug('\tConvert image to pdf files')
     t = time.time()
     if self.settings['output_images']: shutil.copy2(file_path,self.output_image_location)
     if self.settings['output_pdf']:
         output_pdf = os.path.join(self.temp_pdf_folder,file_name+'.pdf')
         image_tools.compressFile(file_path,output_pdf)
     time_stat['Convert to pdf'] = time.time()-t
     return time_stat
def createPdfFromFolder(src, file_dest,temp_folder,
                        quality=50,resize_pct=50,valid_exts=['jpg','tif']):
    '''
    Use ImageMagick to create one pdf from all the images in a folder and 
    output to a given destination.
    
    Create a pdf of each image and place in temp folder. Merge output pdf-files
    to pdf-dest and remove temp folder.
    
    '''
    image_paths = fs.getFilesInFolderWithExts(src, valid_exts)
    for image in image_paths:
        # Handle spaces in filenames
        image = '"' + image + '"'
        input_path = os.path.join(src,image)
        file_name,_ = os.path.splitext(image)
        output_file_name = file_name+'.pdf'
        output_path = os.path.join(temp_folder,output_file_name)
        image_tools.compressFile(input_path, output_path, quality, resize_pct)
    pdf_misc.mergePdfFilesInFolder(temp_folder,file_dest)
    fs.clear_folder(temp_folder,also_folder=True)