@@ -7,6 +7,7 @@ import * as gis from 'async-g-i-s';
7
7
import * as httpsClient from 'https' ;
8
8
import * as httpClient from 'http' ;
9
9
import * as easyimage from 'easyimage' ;
10
+ import { execSync } from 'child_process' ;
10
11
11
12
const missingImagesPath = path . join ( __dirname , 'missing-device-images' ) ;
12
13
@@ -32,13 +33,16 @@ export async function downloadImage(url: string, path: string) {
32
33
} ) ;
33
34
}
34
35
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 } ) ;
39
40
fs . rmSync ( imagePath ) ;
40
- imagePath = imagePathJpg ;
41
+ imagePath = imagePathPng ;
41
42
}
43
+ const imagePathBackground = `${ imagePath } .background.png`
44
+ fs . renameSync ( imagePath , imagePathBackground ) ;
45
+ execSync ( `rembg i ${ imagePathBackground } ${ imagePath } ` ) ;
42
46
return imagePath ;
43
47
}
44
48
@@ -60,8 +64,8 @@ export async function downloadMissing() {
60
64
// Download
61
65
await downloadImage ( image . url , imagePath ) ;
62
66
63
- // Convert to jpg
64
- imagePath = await ensureJpg ( imagePath ) ;
67
+ // Convert to png
68
+ imagePath = await ensurePngWithoutBackground ( imagePath ) ;
65
69
66
70
// Make sqaure
67
71
const info = await easyimage . info ( imagePath ) ;
@@ -84,13 +88,13 @@ async function moveMissing() {
84
88
for ( const file of fs . readdirSync ( missingImagesPath ) ) {
85
89
try {
86
90
let source = path . join ( missingImagesPath , file ) ;
87
- source = await ensureJpg ( source ) ;
91
+ // source = await ensurePngWithoutBackground (source);
88
92
const name = path . basename ( source ) ;
89
- const match = name . match ( '(.+)_\\d+\\.jpg ' ) ;
93
+ const match = name . match ( '(.+)_\\d+\\.png ' ) ;
90
94
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 ` ) ;
92
96
fs . copyFileSync ( source , target ) ;
93
- const size = 150 ;
97
+ const size = 512 ;
94
98
await easyimage . resize ( { width : size , height : size , src : target , dst : target } ) ;
95
99
} catch ( error ) {
96
100
console . error ( `Failed to handle '${ file } ' (${ error } )` )
0 commit comments