def _resize_and_copy_figure(
    filename,
    origin_folder,
    destination_folder,
    resize_image,
    image_size,
    compress_pdf,
    pdf_resolution,
    convert_png_to_jpg=False,
    png_quality=50,
    png_size_threshold=0.5,
    verbose=False
):
    """Resizes and copies the input figure (either JPG, PNG, or PDF).

    Parameters:
        filename: The input filename
        origin_folder: The folder containing the input filename
        destination_folder: The folder to copy the output filename to
        resize_image: Whether to resize the image
        image_size: The maximum size of the image in pixels
        compress_pdf: Whether to compress the PDF file
        convert_png_to_jpg: Whether to convert PNG files to JPG format. Note that this will override resize_image for PNG files.
        png_quality: JPG quality for converted PNG files (0-100)
        png_size_threshold: Minimum file size in MB to apply quality reduction
        verbose: Enable verbose logging
    
    Returns:
        str: The actual output filename (may differ from input if PNG was converted)
    """
    _create_dir_if_not_exists(
        os.path.join(destination_folder, os.path.dirname(filename))
    )
    
    if convert_png_to_jpg and os.path.splitext(filename)[1].lower() in ['.png']:
        original_size_mb = os.path.getsize(os.path.join(origin_folder, filename)) / (1024 * 1024)
        im = Image.open(os.path.join(origin_folder, filename))
        # Determine quality based on file size
        if original_size_mb < png_size_threshold:
            quality = 100  # Keep high quality for small files
            if verbose:
                print(f"Keeping original quality for small PNG: {filename}")
        else:
            quality = png_quality
            if verbose:
                print(f"Converting PNG to JPG with quality {quality}: {filename}")
        
        # Convert PNG to JPG
        output_filename = os.path.splitext(filename)[0] + '.jpg'
        rgb_img = im.convert('RGB')
        rgb_img.save(os.path.join(destination_folder, output_filename), 'JPEG', quality=quality)
        
        if verbose:
            print(f"Converted: {filename} -> {output_filename}")
          
        return output_filename
                    
    if resize_image and os.path.splitext(filename)[1].lower() in [
        '.jpg',
        '.jpeg',
        '.png',
    ]:
        try:
            im = Image.open(os.path.join(origin_folder, filename))
            if max(im.size) > image_size:
                im = im.resize(
                    tuple([int(x * float(image_size) / max(im.size)) for x in im.size]),
                    Image.Resampling.LANCZOS,
                )
            
            if os.path.splitext(filename)[1].lower() in ['.jpg', '.jpeg']:
                im.save(os.path.join(destination_folder, filename), 'JPEG', quality=90)
                return filename
                
            elif os.path.splitext(filename)[1].lower() in ['.png']:
                im.save(os.path.join(destination_folder, filename), 'PNG')
                return filename
                    
        except Exception as e:
            if verbose:
                print(f"Failed to process image {filename}: {e}")
            # Fall back to simple copy
            shutil.copy(
                os.path.join(origin_folder, filename),
                os.path.join(destination_folder, filename),
            )
            return filename

    elif compress_pdf and os.path.splitext(filename)[1].lower() == '.pdf':
        _resize_pdf_figure(
            filename, origin_folder, destination_folder, pdf_resolution
        )
        return filename
    else:
        shutil.copy(
            os.path.join(origin_folder, filename),
            os.path.join(destination_folder, filename),
        )
        return filename
Resizes and copies the input figure (either JPG, PNG, or PDF).

    Parameters:
        filename: The input filename
        origin_folder: The folder containing the input filename
        destination_folder: The folder to copy the output filename to
        resize_image: Whether to resize the image
        image_size: The maximum size of the image in pixels
        compress_pdf: Whether to compress the PDF file
        convert_png_to_jpg: Whether to convert PNG files to JPG format. Note that this will override resize_image for PNG files.
        png_quality: JPG quality for converted PNG files (0-100)
        png_size_threshold: Minimum file size in MB to apply quality reduction
        verbose: Enable verbose logging
    
    Returns:
        str: The actual output filename (may differ from input if PNG was converted)
    
arxiv_latex_cleaner/arxiv_latex_cleaner.py