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)