Skip to content

Commit e9150e6

Browse files
authored
Fix missing devices image download and enforce png (Koenkk#2543)
1 parent c531802 commit e9150e6

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ node_modules
44
dist
55
.idea
66
.DS_Store
7-
docgen/missing-device-images/
7+
docgen/missing-device-images/
8+
venv/

docgen/missing_device_images.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as gis from 'async-g-i-s';
77
import * as httpsClient from 'https';
88
import * as httpClient from 'http';
99
import * as easyimage from 'easyimage';
10+
import {execSync} from 'child_process';
1011

1112
const missingImagesPath = path.join(__dirname, 'missing-device-images');
1213

@@ -32,13 +33,16 @@ export async function downloadImage(url: string, path: string) {
3233
});
3334
}
3435

35-
export async function ensureJpg(imagePath: string) {
36-
if (path.parse(imagePath).ext !== '.jpg') {
37-
const imagePathJpg = `${path.join(missingImagesPath, path.parse(imagePath).name)}.jpg`;
38-
await easyimage.convert({src: imagePath, dst: imagePathJpg});
36+
export async function ensurePngWithoutBackground(imagePath: string) {
37+
if (path.parse(imagePath).ext !== '.png') {
38+
const imagePathPng = `${path.join(missingImagesPath, path.parse(imagePath).name)}.png`;
39+
await easyimage.convert({src: imagePath, dst: imagePathPng});
3940
fs.rmSync(imagePath);
40-
imagePath = imagePathJpg;
41+
imagePath = imagePathPng;
4142
}
43+
const imagePathBackground = `${imagePath}.background.png`
44+
fs.renameSync(imagePath, imagePathBackground);
45+
execSync(`rembg i ${imagePathBackground} ${imagePath}`);
4246
return imagePath;
4347
}
4448

@@ -60,8 +64,8 @@ export async function downloadMissing() {
6064
// Download
6165
await downloadImage(image.url, imagePath);
6266

63-
// Convert to jpg
64-
imagePath = await ensureJpg(imagePath);
67+
// Convert to png
68+
imagePath = await ensurePngWithoutBackground(imagePath);
6569

6670
// Make sqaure
6771
const info = await easyimage.info(imagePath);
@@ -84,13 +88,13 @@ async function moveMissing() {
8488
for (const file of fs.readdirSync(missingImagesPath)) {
8589
try {
8690
let source = path.join(missingImagesPath, file);
87-
source = await ensureJpg(source);
91+
// source = await ensurePngWithoutBackground(source);
8892
const name = path.basename(source);
89-
const match = name.match('(.+)_\\d+\\.jpg');
93+
const match = name.match('(.+)_\\d+\\.png');
9094
if (!match) throw new Error(`Failed to match '${name}'`)
91-
const target = path.join(imageBaseDir, `${match[1]}.jpg`);
95+
const target = path.join(imageBaseDir, `${match[1]}.png`);
9296
fs.copyFileSync(source, target);
93-
const size = 150;
97+
const size = 512;
9498
await easyimage.resize({width: size, height: size, src: target, dst: target});
9599
} catch (error) {
96100
console.error(`Failed to handle '${file}' (${error})`)

docgen/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function getImageName(model) {
2020
];
2121
let image = model;
2222
replaceByDash.forEach((r) => image = image.replace(r, '-'));
23-
return `${ image }.jpg`;
23+
return `${ image }.png`;
2424
}
2525

2626
export async function generatePage(content, target) {

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@vuepress/plugin-docsearch": "^2.0.0-beta.53",
2626
"@vuepress/plugin-google-analytics": "^2.0.0-beta.53",
2727
"@vuepress/plugin-register-components": "^2.0.0-beta.53",
28-
"async-g-i-s": "^1.5.1",
28+
"async-g-i-s": "^1.5.2",
2929
"cross-env": "^7.0.3",
3030
"easyimage": "^3.1.1",
3131
"glob": "^8.0.3",

0 commit comments

Comments
 (0)