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)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用node-canvas库将svg转换png。 安装: ``` npm install canvas ``` 示例代码: ``` const { createCanvas, loadImage } = require('canvas') const fs = require('fs') async function svg2png(svg, png) { const image = await loadImage(svg) const canvas = createCanvas(image.width, image.height) const ctx = canvas.getContext('2d') ctx.drawImage(image, 0, 0) const buffer = canvas.toBuffer() fs.writeFileSync(png, buffer) } svg2png('input.svg', 'output.png') ``` 此代码将“input.svg”文件转换为“output.png”文件。 ### 回答2: SVG是一种可伸缩矢量图形格式,而PNG是一种位图图像格式。如果我们想把SVG文件转换PNG,我们可以使用Node.js来完成。 首先,我们需要安装一些必要的Node.js库。我们可以使用`npm`命令来安装`svg2png`库,这个库可以帮助我们将SVG转换PNG。打开终端窗口,并输入以下命令来安装`svg2png`库: ``` npm install svg2png ``` 安装完成后,我们可以创建一个Node.js脚本来执行转换操作。在脚本中,我们首先需要引入`svg2png`库和`fs`库,`fs`库用于读取和写入文件。然后,我们可以使用`svg2png`库的`convert`方法来执行转换。下面是一个示例代码: ```javascript const fs = require('fs'); const svg2png = require('svg2png'); const svgFilePath = 'path_to_svg_file.svg'; const pngFilePath = 'path_to_output_png_file.png'; fs.readFile(svgFilePath, (err, data) => { if (err) throw err; svg2png(data) .then(buffer => { fs.writeFile(pngFilePath, buffer, (err) => { if (err) throw err; console.log('SVG转换PNG成功!'); }); }) .catch(error => { console.error('转换失败:', error); }); }); ``` 在上面的代码中,我们首先读取SVG文件,然后使用`svg2png`库的`convert`方法将SVG转换PNG。最后,我们将转换后的PNG图像数据写入到指定的输出文件中。完成后,我们会在控制台输出成功或失败的相关信息。 要使用上述代码进行转换,我们需要将`path_to_svg_file.svg`替换为SVG文件的实际路径,将`path_to_output_png_file.png`替换为要生成的PNG文件的实际路径。执行脚本后,我们将得到一个转换成功的PNG图像文件。 需要注意的是,由于`svg2png`库依赖于Cairo库,因此在使用此方法之前,我们需要确保已经在计算机上安装了Cairo库。 ### 回答3: 使用Node将SVG转换PNG可以通过使用一些库来实现。 首先,你需要安装`svg2png`库。你可以使用npm命令安装它:`npm install svg2png`。 安装完库后,你需要在项目中引入它:`const svg2png = require('svg2png');`。 接下来,你可以使用以下代码将SVG文件转换PNG: ```javascript const fs = require('fs'); fs.readFile('input.svg', (err, data) => { if (err) throw err; // 将SVG数据转换PNG文件 svg2png(data) .then(buffer => { fs.writeFile('output.png', buffer, err => { if (err) throw err; console.log('PNG文件已生成'); }); }) .catch(error => { console.error('转换失败:', error); }); }); ``` 上述代码中,我们首先使用`fs`模块来读取SVG文件的数据。然后,我们使用`svg2png`函数将SVG数据转换PNG格式的数据。最后,我们使用`fs.writeFile`方法将PNG数据写入文件。 请确保在运行上述代码前,你已经准备好了一份名为`input.svg`的SVG文件。一旦代码执行完成,你将会在项目中看到一个名为`output.png`的PNG文件。 这就是使用Node将SVG转换PNG的简单方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值