Cloud Data Loss Prevention (DLP) can redact sensitive text from an image. Using infoType detectors and Cloud Vision, Cloud DLP inspects a base64-encoded image for text, detects sensitive data within the text, and then returns a base64-encoded image with any matching sensitive data obscured by an opaque rectangle.
For example, consider the following "before" and "after" images. The original image is an example of a typical image file generated from a scan of a paper document. In this example, Cloud DLP has been configured to redact US Social Security numbers, email addresses, and telephone numbers using rectangles of different colors, depending on the content.

- Scanned image before image redaction
- Scanned image after image redaction
Redacting all default infoTypes from an image
To redact sensitive data from an image, you submit a base64-encoded image to the
DLP API's
image.redact
method.
Unless you specify specific information types (infoTypes)
to search for, Cloud DLP searches for the most common infoTypes.
To redact default infoTypes from an image:
- Encode the image as base64.
- Submit a request to the DLP API's
image.redact
method. The request need only contain the base64-encoded image if you want to redact default infoTypes.
For example, consider the following image. This image is an example of a typical image file generated from a scan of a paper document.

To redact the default infoTypes from this image, send the following request to
the DLP API's
image.redact
method:
Protocol
{ "byteItem": { "data": "[BASE64-ENCODED-IMAGE]", "type": "IMAGE_JPEG" } }
Cloud DLP returns the following:
{ "redactedImage": "[BASE64-ENCODED-IMAGE]" }
Java
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
After decoding the base64-encoded image, the image appears as follows:

Note that, in addition to masking the handwritten Social Security number, the email address, and the phone number, Cloud DLP has also redacted the year. Assuming that's not optimal behavior, the next example demonstrates how to redact only certain infoTypes.
Redacting specific infoTypes from an image
If you want to redact only certain sensitive data from an image, specify their corresponding built-in infoTypes.
To redact specific infoTypes from an image:
- Encode the image as base64.
- Submit a request to the DLP API's
image.redact
method. The request must include:- The base64-encoded image.
- One or more infoType detectors.
Consider the original image from the previous section. To redact only
US Social Security numbers, email addresses, and telephone numbers, send the
following JSON to the DLP API's
image.redact
method:
Protocol
{ "byteItem": { "data": "[BASE64-ENCODED-IMAGE]", "type": "IMAGE_JPEG" }, "imageRedactionConfigs": [ { "infoType": { "name": "US_SOCIAL_SECURITY_NUMBER" } }, { "infoType": { "name": "EMAIL_ADDRESS" } }, { "infoType": { "name": "PHONE_NUMBER" } } ] }
Cloud DLP returns the following:
{ "redactedImage": "[BASE64-ENCODED-IMAGE]" }
Java
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
C#
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
After decoding the base64-encoded image, the image appears as follows:

You can color code redacted information by infoType when you want to tell at a glance what's been redacted. See the following section for more information.
Redacting infoTypes from an image with color coding
To color code redacted information by infoType, you pair infoType detectors with RGB color space values.
To color code infoTypes redacted from an image:
- Encode the image as base64.
- Submit a request to the DLP API's
image.redact
method. The request must include:- The base64-encoded image.
- One or more infoType detectors, each of which is assigned a color using RGB color space values.
Consider the original image from the first section. To redact
US Social Security numbers with a purple box, email addresses with a green box,
and telephone numbers with an orange box, send the following JSON to the
DLP API's
image.redact
method:
Protocol
{ "byteItem": { "data": "[BASE64-ENCODED-IMAGE]", "type": "IMAGE_JPEG" }, "imageRedactionConfigs": [ { "infoType": { "name": "US_SOCIAL_SECURITY_NUMBER" }, "redactionColor": { "red": 0.3, "green": 0.1, "blue": 0.6 } }, { "infoType": { "name": "EMAIL_ADDRESS" }, "redactionColor": { "red": 0.5, "blue": 0.5, "green": 1 } }, { "infoType": { "name": "PHONE_NUMBER" }, "redactionColor": { "red": 1, "blue": 0, "green": 0.6 } } ] }
Cloud DLP returns the following:
{ "redactedImage": "[BASE64-ENCODED-IMAGE]" }
Java
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
After decoding the base64-encoded image, the image appears as follows:

Redacting all text from an image
Cloud DLP also contains an option to redact all detected text in an image.
To redact all text in an image:
- Encode the image as base64.
- Submit a request to the DLP API's
image.redact
method. The request must contain:- The base64-encoded image.
- The
redactAllText
option set totrue
.
Consider the original image from the first section. To redact all text, send the
following JSON to the DLP API's
image.redact
method:
Protocol
{ "byteItem": { "data": "[BASE64-ENCODED-IMAGE]", "type": "IMAGE_JPEG" }, "imageRedactionConfigs": [ { "redactAllText": true } ] }
Cloud DLP returns the following:
{ "redactedImage": "[BASE64-ENCODED-IMAGE]" }
Python
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
Java
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
The API returns the same image(s) you gave it, but any text identified as containing sensitive information according to your criteria has been redacted.
After decoding the base64-encoded image, the image appears as follows:

Code examples
Following is sample code in several languages that demonstrates how to use Cloud DLP to redact sensitive text from an image.
Node.js
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
Python
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
Go
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
PHP
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
C#
To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.
Try it out
You can try out each of the examples on this page yourself—or experiment
with your own images—in the APIs Explorer on the reference page for
image.redact
:
What's next
- Learn more about image inspection and redaction.
- Learn how to inspect images for sensitive data.