Package: presidio-image-redactor
Presidio Image RedactorPlease notice, this package is still in alpha and not production ready.DescriptionThe Presidio Image Redactor is a Python based module for detecting and redacting PII text entities in images.Deploy Presidio image redactor to AzureUse the following button to deploy presidio image redactor to your Azure subscription.Process for standard images:Process for DICOM files:InstallationPre-requisites:InstallTesseract OCRby following the
instructions on how to install it for your operating system.For best performance, please use the most up-to-date version of Tesseract OCR. Presidio was tested withv5.2.0.As packageTo get started with Presidio-image-redactor, run the following:pipinstallpresidio-image-redactorOnce Installed, run the following command to download the default spacy model needed for
Presidio Analyzer:python-mspacydownloaden_core_web_lgGetting started (standard image types)The engine will receive 2 parameters:Image to redact.Color fill to redact with, by default color fill will be black. Can either be an int
or tuple (0,0,0)fromPILimportImagefrompresidio_image_redactorimportImageRedactorEngine# Get the image to redact using PIL lib (pillow)image=Image.open("presidio-image-redactor/tests/integration/resources/ocr_test.png")# Initialize the engineengine=ImageRedactorEngine()# Redact the image with pink colorredacted_image=engine.redact(image,(255,192,203))# save the redacted imageredacted_image.save("new_image.png")# uncomment to open the image for viewing# redacted_image.show()As docker serviceIn folder presidio/presidio-image-redactor run:docker-compose up -dHTTP APIredactReceives an image and color fill (optional, default is black). Redact the image PII text
and returns a new redacted image.POST /redactPayload:Sent as multipart-form. Contains image file and data of the required color fill.{"data":"{'color_fill':'0,0,0'}"}Result:200 OKcurl example:# use ocr_test.png as the image to redact, and 255 as the color fill. 
# out.png is the new redacted image received from the server.
curl -XPOST "http://localhost:3000/redact" -H "content-type: multipart/form-data" -F "image=@ocr_test.png" -F "data=\"{'color_fill':'255'}\"" > out.pngPython script example can be found under:
/presidio/e2e-tests/tests/test_image_redactor.pyGetting started (DICOM images)This module only redacts pixel data and does not scrub text PHI which may exist in the DICOM metadata.We highly recommend using the DICOM image redactor engine to redact text from imagesbeforescrubbing metadata PHI. To redact sensitive information from metadata, consider using another package such as theTools for Health Data Anonymization.To redact burnt-in text PHI in DICOM images, see the below sample code:importpydicomfrompresidio_image_redactorimportDicomImageRedactorEngine# Set input and output pathsinput_path="path/to/your/dicom/file.dcm"output_dir="./output"# Initialize the engineengine=DicomImageRedactorEngine()# Option 1: Redact from a loaded DICOM imagedicom_image=pydicom.dcmread(input_path)redacted_dicom_image=engine.redact(dicom_image,fill="contrast")# Option 2: Redact from a loaded DICOM image and return redacted regionsredacted_dicom_image,bboxes=engine.redact_and_return_bbox(dicom_image,fill="contrast")# Option 3: Redact from DICOM file and save redacted regions as json fileengine.redact_from_file(input_path,output_dir,padding_width=25,fill="contrast",save_bboxes=True)# Option 4: Redact from directory and save redacted regions as json filesocr_kwargs={"ocr_threshold":50}engine.redact_from_directory("path/to/your/dicom",output_dir,fill="background",save_bboxes=True,ocr_kwargs=ocr_kwargs)See the example notebook for more details and visual confirmation of the output:docs/samples/python/example_dicom_image_redactor.ipynb.Side note for WindowsIf you are using a Windows machine, you may run into issues if file paths are too long. Unfortunatley, this is not rare when working with DICOM images that are often nested in directories with descriptive names.To avoid errors where the code may not recognize a path as existing due to the length of the characters in the file path, pleaseenable long paths on your system.DICOM Data CitationThe DICOM data used for unit and integration testing forDicomImageRedactorEngineare stored in this repository with permission from the original dataset owners. Please see the dataset information as follows:Rutherford, M., Mun, S.K., Levine, B., Bennett, W.C., Smith, K., Farmer, P., Jarosz, J., Wagner, U., Farahani, K., Prior, F. (2021). A DICOM dataset for evaluation of medical image de-identification (Pseudo-PHI-DICOM-Data) [Data set]. The Cancer Imaging Archive. DOI:https://doi.org/10.7937/s17z-r072