MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

dlp/redactImage.js:20–81  ·  view source on GitHub ↗
(projectId, filepath, minLikelihood, infoTypes, outputPath)

Source from the content-addressed store, hash-verified

18// usage: node redactImage.js my-project filepath minLikelihood infoTypes outputPath
19
20function main(projectId, filepath, minLikelihood, infoTypes, outputPath) {
21 infoTypes = transformCLI(infoTypes);
22 // [START dlp_redact_image]
23 // Imports the Google Cloud Data Loss Prevention library
24 const DLP = require('@google-cloud/dlp');
25
26 // Imports required Node.js libraries
27 const mime = require('mime');
28 const fs = require('fs');
29
30 // Instantiates a client
31 const dlp = new DLP.DlpServiceClient();
32
33 // The project ID to run the API call under
34 // const projectId = 'my-project';
35
36 // The path to a local file to inspect. Can be a JPG or PNG image file.
37 // const filepath = 'path/to/image.png';
38
39 // The minimum likelihood required before redacting a match
40 // const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';
41
42 // The infoTypes of information to redact
43 // const infoTypes = [{ name: 'EMAIL_ADDRESS' }, { name: 'PHONE_NUMBER' }];
44
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}`);

Callers 1

redactImage.jsFile · 0.70

Calls 2

transformCLIFunction · 0.70
redactImageFunction · 0.70

Tested by

no test coverage detected