Skip to content

Commit 6935df1

Browse files
committed
remove double pages
1 parent 456c496 commit 6935df1

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

main.ts

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,70 @@ const queue: Array<{started: boolean, info: any}> = []
4747
if (!fs.existsSync(path.join(__dirname, "data"))) fs.mkdirSync(path.join(__dirname, "data"))
4848
fs.writeFileSync(path.join(__dirname, "data/Helvetica.afm"), Helvetica)
4949

50+
const removeDoubles = async (images: string[], dontProcessAll?: boolean) => {
51+
images = images.sort(new Intl.Collator(undefined, {numeric: true, sensitivity: "base"}).compare)
52+
53+
let doubleImages: string[] = []
54+
let widthMap = {} as any
55+
56+
for (let i = 0; i < images.length; i++) {
57+
const metadata = await sharp(images[i]).metadata()
58+
const width = metadata.width || 0
59+
if (widthMap[width]) {
60+
widthMap[width] += 1
61+
} else {
62+
widthMap[width] = 1
63+
}
64+
}
65+
66+
let commonWidth = 0
67+
let freq = 0
68+
for (let i = 0; i < Object.keys(widthMap).length; i++) {
69+
const key = Object.keys(widthMap)[i]
70+
const value = Object.values(widthMap)[i]
71+
if (freq < Number(value)) {
72+
freq = Number(value)
73+
commonWidth = Number(key)
74+
}
75+
}
76+
77+
for (let i = 0; i < images.length; i++) {
78+
const metadata = await sharp(images[i]).metadata()
79+
const width = metadata.width || 0
80+
if (width > commonWidth * 1.5) {
81+
doubleImages.push(images[i])
82+
}
83+
}
84+
85+
// If all images have the same width, treat all of them as doubles
86+
if (!doubleImages.length) {
87+
if (!dontProcessAll) doubleImages = images
88+
}
89+
90+
for (let i = 0; i < doubleImages.length; i++) {
91+
const metadata = await sharp(doubleImages[i]).metadata()
92+
const width = metadata.width || 0
93+
const height = metadata.height || 0
94+
const newWidth = Math.floor(width / 2)
95+
const page1 = `${path.dirname(doubleImages[i])}/${path.basename(doubleImages[i], path.extname(doubleImages[i]))}.1${path.extname(doubleImages[i])}`
96+
const page2 = `${path.dirname(doubleImages[i])}/${path.basename(doubleImages[i], path.extname(doubleImages[i]))}.2${path.extname(doubleImages[i])}`
97+
await sharp(doubleImages[i])
98+
.extract({left: newWidth, top: 0, width: newWidth, height: height})
99+
.toFile(page1)
100+
await sharp(doubleImages[i])
101+
.extract({left: 0, top: 0, width: newWidth, height: height})
102+
.toFile(page2)
103+
}
104+
105+
const promiseArray: any[] = []
106+
for (let i = 0; i < doubleImages.length; i++) {
107+
promiseArray.push(new Promise<void>((resolve) => {
108+
fs.unlink(doubleImages[i], () => resolve())
109+
}))
110+
}
111+
await Promise.all(promiseArray)
112+
}
113+
50114
ipcMain.handle("remove-duplicate-subs", async (event, files: string[]) => {
51115
for (let i = 0; i < files.length; i++) {
52116
const content = fs.readFileSync(files[i]).toString().split("\n")
@@ -264,19 +328,6 @@ ipcMain.handle("pdf-cover", async (event, files: string[]) => {
264328

265329
let openDir = ""
266330

267-
for (let i = 0; i < directories.length; i++) {
268-
const dir = directories[i]
269-
let images = fs.readdirSync(dir).map((i) => path.join(dir, i))
270-
images = images.filter((f) => path.extname(f).toLowerCase() === ".jpg" || path.extname(f).toLowerCase() === ".png" || path.extname(f).toLowerCase() === ".jpeg")
271-
await extractCover(dir, images)
272-
try {
273-
fs.rmdirSync(dir)
274-
} catch (e) {
275-
console.log(e)
276-
}
277-
if (!openDir) openDir = directories[0]
278-
}
279-
280331
for (let i = 0; i < PDFs.length; i++) {
281332
const dir = path.dirname(PDFs[i])
282333
const saveFilename = path.basename(PDFs[i], path.extname(PDFs[i]))
@@ -299,8 +350,16 @@ ipcMain.handle("pdf-cover", async (event, files: string[]) => {
299350
if (!openDir) openDir = PDFs[0]
300351
}
301352

353+
for (let i = 0; i < directories.length; i++) {
354+
const dir = directories[i]
355+
let images = fs.readdirSync(dir).map((i) => path.join(dir, i))
356+
images = images.filter((f) => path.extname(f).toLowerCase() === ".jpg" || path.extname(f).toLowerCase() === ".png" || path.extname(f).toLowerCase() === ".jpeg")
357+
await removeDoubles(images, true)
358+
if (!openDir) openDir = images[0]
359+
}
360+
302361
if (images.length) {
303-
await extractCover(images[0], images)
362+
await removeDoubles(images)
304363
if (!openDir) openDir = images[0]
305364
}
306365
shell.openPath(path.dirname(openDir))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "image-compressor",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"description": "Compresses, resizes, and renames images.",
55
"main": "dist/main.js",
66
"scripts": {

0 commit comments

Comments
 (0)