| 45 | // The local path to save the resulting image to. |
| 46 | // const outputPath = 'result.png'; |
| 47 | async function redactImage() { |
| 48 | const imageRedactionConfigs = infoTypes.map(infoType => { |
| 49 | return {infoType: infoType}; |
| 50 | }); |
| 51 | |
| 52 | // Load image |
| 53 | const fileTypeConstant = |
| 54 | ['image/jpeg', 'image/bmp', 'image/png', 'image/svg'].indexOf( |
| 55 | mime.getType(filepath) |
| 56 | ) + 1; |
| 57 | const fileBytes = Buffer.from(fs.readFileSync(filepath)).toString('base64'); |
| 58 | |
| 59 | // Construct image redaction request |
| 60 | const request = { |
| 61 | parent: `projects/${projectId}/locations/global`, |
| 62 | byteItem: { |
| 63 | type: fileTypeConstant, |
| 64 | data: fileBytes, |
| 65 | }, |
| 66 | inspectConfig: { |
| 67 | minLikelihood: minLikelihood, |
| 68 | infoTypes: infoTypes, |
| 69 | }, |
| 70 | imageRedactionConfigs: imageRedactionConfigs, |
| 71 | }; |
| 72 | |
| 73 | // Run image redaction request |
| 74 | const [response] = await dlp.redactImage(request); |
| 75 | const image = response.redactedImage; |
| 76 | fs.writeFileSync(outputPath, image); |
| 77 | console.log(`Saved image redaction results to path: ${outputPath}`); |
| 78 | } |
| 79 | redactImage(); |
| 80 | // [END dlp_redact_image] |
| 81 | } |