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

Function main

dlp/redactImageFileAllText.js:19–72  ·  view source on GitHub ↗
(projectId, filepath, outputPath)

Source from the content-addressed store, hash-verified

17// description: Redact all detected text in an image.
18// usage: node redactImageFileAllText.js my-project filepath outputPath
19function main(projectId, filepath, outputPath) {
20 // [START dlp_redact_image_all_text]
21 // Imports the Google Cloud Data Loss Prevention library
22 const DLP = require('@google-cloud/dlp');
23
24 // Imports required Node.js libraries
25 const mime = require('mime');
26 const fs = require('fs');
27
28 // Instantiates a client
29 const dlp = new DLP.DlpServiceClient();
30
31 // The project ID to run the API call under
32 // const projectId = 'my-project';
33
34 // The path to a local file to inspect. Can be a JPG or PNG image file.
35 // const filepath = 'path/to/image.png';
36
37 // The local path to save the resulting image to.
38 // const outputPath = 'result.png';
39
40 async function redactImageAllText() {
41 // Enable redaction of all text.
42 const imageRedactionConfigs = [{redactAllText: true}];
43
44 // Load image
45 const fileTypeConstant =
46 ['image/jpeg', 'image/bmp', 'image/png', 'image/svg'].indexOf(
47 mime.getType(filepath)
48 ) + 1;
49 const fileBytes = Buffer.from(fs.readFileSync(filepath)).toString('base64');
50
51 // Construct the Redact request to be sent by the client.
52 // Do not specify the type of info to redact.
53 const request = {
54 parent: `projects/${projectId}/locations/global`,
55 byteItem: {
56 type: fileTypeConstant,
57 data: fileBytes,
58 },
59 imageRedactionConfigs: imageRedactionConfigs,
60 };
61
62 // Run image redaction request
63 const [response] = await dlp.redactImage(request);
64
65 // Parse the response and process results.
66 const image = response.redactedImage;
67 fs.writeFileSync(outputPath, image);
68 console.log(`Saved image redaction results to path: ${outputPath}`);
69 }
70 redactImageAllText();
71 // [END dlp_redact_image_all_text]
72}
73
74process.on('unhandledRejection', err => {
75 console.error(err.message);

Callers 1

Calls 1

redactImageAllTextFunction · 0.85

Tested by

no test coverage detected