node常用转换:PDF转JPG,PNG转TIFF

1、PDF转JPG

// 转换图片文件

const path = require('path')
const fs = require('fs')
const pdf2jpg = require('pdf2jpg');

// const sourceDir = '/Volumes/Elements SE/20240401云南省图期刊数据/精细化标引/2600/object/PNG'
// const targetDir = '/Volumes/Elements SE/20240401云南省图期刊数据/精细化标引/2600/object/TIFF'

const sourceDir = '/Volumes/ElementsSE/2024安徽合并数据/安徽对象文件/07/PDF'
const targetDir = '/Volumes/ElementsSE/2024安徽合并数据/安徽对象文件/07/jpg'

async function convertPdfToJpg(pdfPath, outputFolder) {

      return new Promise((resolve, reject) => {
        const source = fs.readFileSync(pdfPath);

        pdf2jpg(source).then(buffer => {
            fs.writeFileSync(outputFolder , buffer)
            console.info('转图片'+outputFolder)
            resolve(1)
        })
      })

}


const isExist = (path) => { // 判断文件夹是否存在, 不存在创建一个
    if (!fs.existsSync(path)) {
        fs.mkdirSync(path)
    }
}

isExist(targetDir)

const copyFile = async (sourcePath, targetPath) => {
    const sourceFile = fs.readdirSync(sourcePath, { withFileTypes: true })

    for (let i=0;i<sourceFile.length;i++) {
        const file = sourceFile[i]
        const newSourcePath = path.resolve(sourcePath, file.name)
        const newTargetPath = path.resolve(targetPath, file.name)
        if (file.isDirectory()) {
            isExist(newTargetPath)
            copyFile(newSourcePath, newTargetPath)
        }
        if (file.name.endsWith('.pdf')) {
            console.info('转图片')
            await convertPdfToJpg(newSourcePath, newTargetPath.replace('.pdf','.jpg'))
        }
    }
    /*sourceFile.forEach(file => {
        const newSourcePath = path.resolve(sourcePath, file.name)
        const newTargetPath = path.resolve(targetPath, file.name)
        if (file.isDirectory()) {
            isExist(newTargetPath)
            copyFile(newSourcePath, newTargetPath)
        }
        if (file.name.endsWith('.pdf')) {
            await convertPdfToJpg(newSourcePath, newTargetPath.replace('.pdf','.jpg'))
        }
    })*/
}

copyFile(sourceDir, targetDir)

2、PNG转TIFF

const path = require('path')
const fs = require('fs')

const sourceDir = '/Volumes/Elements SE/20240401云南省图期刊数据/精细化标引/2600/object/PNG'
const targetDir = '/Volumes/Elements SE/20240401云南省图期刊数据/精细化标引/2600/object/TIFF'

const sharp = require('sharp');

// 转换图片格式的函数
async function convertImageFormat(inputFile, outputFile) {
    try {
        await sharp(inputFile)
            .toFormat('tif') // 转换为PNG格式,可以改为其他格式,如 'jpeg', 'webp' 等
            .toFile(outputFile);
        console.log('图片格式转换成功!'+outputFile);
    } catch (err) {
        console.error('转换过程中发生错误:', err);
    }
}


const isExist = (path) => { // 判断文件夹是否存在, 不存在创建一个
    if (!fs.existsSync(path)) {
        fs.mkdirSync(path)
    }
}

isExist(targetDir)

const copyFile = (sourcePath, targetPath) => {
    const sourceFile = fs.readdirSync(sourcePath, { withFileTypes: true })

    sourceFile.forEach(file => {
        const newSourcePath = path.resolve(sourcePath, file.name)
        const newTargetPath = path.resolve(targetPath, file.name)
        if (file.isDirectory()) {
            isExist(newTargetPath)
            copyFile(newSourcePath, newTargetPath)
        }
        if (file.name.endsWith('.png')) { // 需要复制其他的格式的文件修改 .mp4 既可
            //fs.copyFileSync(newSourcePath, newTargetPath.replace('.png','.tif'))
            // 使用示例
            convertImageFormat(newSourcePath, newTargetPath.replace('.png','.tif')) // 将JPG图片转换为PNG格式
            console.info(newTargetPath)
        }
    })
}

copyFile(sourceDir, targetDir)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值