Skip to content

Commit

Permalink
Extract images enhancements (Stirling-Tools#1757)
Browse files Browse the repository at this point in the history
* fix

* extarct images

* langs

* logging

* cuke fix

---------

Co-authored-by: a <a>
  • Loading branch information
Frooodle authored Aug 27, 2024
1 parent 63dfcfe commit 47314a0
Show file tree
Hide file tree
Showing 43 changed files with 131 additions and 22 deletions.
Binary file modified cucumber/exampleFiles/images.pdf
Binary file not shown.
6 changes: 2 additions & 4 deletions cucumber/features/general.feature
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ Feature: API Validation


@extract-images
Scenario Outline: Extract Image Scans
Scenario Outline: Extract Image Scans duplicates
Given I use an example file at "exampleFiles/images.pdf" as parameter "fileInput"
And the request data includes
| parameter | value |
| format | <format> |
When I send the API request to the endpoint "/api/v1/misc/extract-images"
Then the response content type should be "application/octet-stream"
And the response file should have extension ".zip"
And the response ZIP should contain 20 files
And the response ZIP should contain 2 files
And the response file should have size greater than 0
And the response status code should be 200

Expand All @@ -112,5 +112,3 @@ Feature: API Validation
| png |
| gif |
| jpeg |


Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -36,7 +39,8 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import stirling.software.SPDF.model.api.PDFWithImageFormatRequest;
import stirling.software.SPDF.model.api.PDFExtractImagesRequest;
import stirling.software.SPDF.utils.ImageProcessingUtils;
import stirling.software.SPDF.utils.WebResponseUtils;

@RestController
Expand All @@ -51,11 +55,11 @@ public class ExtractImagesController {
summary = "Extract images from a PDF file",
description =
"This endpoint extracts images from a given PDF file and returns them in a zip file. Users can specify the output image format. Input: PDF Output: IMAGE/ZIP Type: SIMO")
public ResponseEntity<byte[]> extractImages(@ModelAttribute PDFWithImageFormatRequest request)
public ResponseEntity<byte[]> extractImages(@ModelAttribute PDFExtractImagesRequest request)
throws IOException, InterruptedException, ExecutionException {
MultipartFile file = request.getFileInput();
String format = request.getFormat();

boolean allowDuplicates = request.isAllowDuplicates();
System.out.println(
System.currentTimeMillis() + " file=" + file.getName() + ", format=" + format);
PDDocument document = Loader.loadPDF(file.getBytes());
Expand All @@ -75,7 +79,7 @@ public ResponseEntity<byte[]> extractImages(@ModelAttribute PDFWithImageFormatRe
String filename =
Filenames.toSimpleFileName(file.getOriginalFilename())
.replaceFirst("[.][^.]+$", "");
Set<Integer> processedImages = new HashSet<>();
Set<byte[]> processedImages = new HashSet<>();

if (useMultithreading) {
// Executor service to handle multithreading
Expand All @@ -92,7 +96,13 @@ public ResponseEntity<byte[]> extractImages(@ModelAttribute PDFWithImageFormatRe
executor.submit(
() -> {
extractImagesFromPage(
page, format, filename, pageNum, processedImages, zos);
page,
format,
filename,
pageNum,
processedImages,
zos,
allowDuplicates);
return null;
});

Expand All @@ -110,7 +120,8 @@ public ResponseEntity<byte[]> extractImages(@ModelAttribute PDFWithImageFormatRe
// Single-threaded extraction
for (int pgNum = 0; pgNum < document.getPages().getCount(); pgNum++) {
PDPage page = document.getPage(pgNum);
extractImagesFromPage(page, format, filename, pgNum + 1, processedImages, zos);
extractImagesFromPage(
page, format, filename, pgNum + 1, processedImages, zos, allowDuplicates);
}
}

Expand All @@ -137,21 +148,34 @@ private void extractImagesFromPage(
String format,
String filename,
int pageNum,
Set<Integer> processedImages,
ZipOutputStream zos)
Set<byte[]> processedImages,
ZipOutputStream zos,
boolean allowDuplicates)
throws IOException {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
logger.error("MD5 algorithm not available for extractImages hash.", e);
return;
}
if (page.getResources() == null || page.getResources().getXObjectNames() == null) {
return;
}
int count = 1;
for (COSName name : page.getResources().getXObjectNames()) {
if (page.getResources().isImageXObject(name)) {
PDImageXObject image = (PDImageXObject) page.getResources().getXObject(name);
int imageHash = image.hashCode();
synchronized (processedImages) {
if (processedImages.contains(imageHash)) {
continue; // Skip already processed images
if (!allowDuplicates) {
byte[] data = ImageProcessingUtils.getImageData(image.getImage());
byte[] imageHash = md.digest(data);
synchronized (processedImages) {
if (processedImages.stream()
.anyMatch(hash -> Arrays.equals(hash, imageHash))) {
continue; // Skip already processed images
}
processedImages.add(imageHash);
}
processedImages.add(imageHash);
}

RenderedImage renderedImage = image.getImage();
Expand All @@ -160,7 +184,7 @@ private void extractImagesFromPage(
BufferedImage bufferedImage = convertToRGB(renderedImage, format);

// Write image to zip file
String imageName = filename + "_" + imageHash + " (Page " + pageNum + ")." + format;
String imageName = filename + "_page_" + pageNum + "_" + count++ + "." + format;
synchronized (zos) {
zos.putNextEntry(new ZipEntry(imageName));
ByteArrayOutputStream imageBaos = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package stirling.software.SPDF.model.api;

import io.swagger.v3.oas.annotations.media.Schema;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class PDFExtractImagesRequest extends PDFWithImageFormatRequest {

@Schema(
description =
"Boolean to enable/disable the saving of duplicate images, true to enable duplicates")
private boolean allowDuplicates;
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,5 @@ public static boolean createDir(String path) {
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package stirling.software.SPDF.utils;

import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.nio.ByteBuffer;

public class ImageProcessingUtils {

Expand Down Expand Up @@ -29,4 +33,30 @@ static BufferedImage convertColorType(BufferedImage sourceImage, String colorTyp
}
return convertedImage;
}

public static byte[] getImageData(BufferedImage image) {
DataBuffer dataBuffer = image.getRaster().getDataBuffer();
if (dataBuffer instanceof DataBufferByte) {
return ((DataBufferByte) dataBuffer).getData();
} else if (dataBuffer instanceof DataBufferInt) {
int[] intData = ((DataBufferInt) dataBuffer).getData();
ByteBuffer byteBuffer = ByteBuffer.allocate(intData.length * 4);
byteBuffer.asIntBuffer().put(intData);
return byteBuffer.array();
} else {
int width = image.getWidth();
int height = image.getHeight();
byte[] data = new byte[width * height * 3];
int index = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = image.getRGB(x, y);
data[index++] = (byte) ((rgb >> 16) & 0xFF); // Red
data[index++] = (byte) ((rgb >> 8) & 0xFF); // Green
data[index++] = (byte) (rgb & 0xFF); // Blue
}
}
return data;
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/messages_ar_AR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=معالجة PDF باستخدام OCR
extractImages.title=استخراج الصور
extractImages.header=استخراج الصور
extractImages.selectText=حدد تنسيق الصورة لتحويل الصور المستخرجة إلى
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=استخراج


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_bg_BG.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Обработка на PDF чрез OCR
extractImages.title=Извличане на изображения
extractImages.header=Извличане на изображения
extractImages.selectText=Изберете формат на изображението, в който да преобразувате извлечените изображения
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Извличане


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_ca_CA.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Processa PDF amb OCR
extractImages.title=Extreu Imatges
extractImages.header=Extreu Imatges
extractImages.selectText=Selecciona el format d'imatge al qual convertir les imatges extretes
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Extreu


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_cs_CZ.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Zpracovat PDF s OCR
extractImages.title=Extrahovat obrázky
extractImages.header=Extrahovat obrázky
extractImages.selectText=Vyberte formát obrázku pro extrahované obrázky
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Extrahovat


Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/messages_da_DK.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
###########
# Generic #
###########
# the direction that the language is written (ltr = left to right, rtl = right to left)
# the direction that the language is written (ltr=left to right, rtl = right to left)
language.direction=ltr

pdfPrompt=Vælg PDF-fil(er)
Expand Down Expand Up @@ -802,6 +802,7 @@ ocr.submit=Process PDF with OCR
extractImages.title=Extract Images
extractImages.header=Extract Images
extractImages.selectText=Select image format to convert extracted images to
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Extract


Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/messages_de_DE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# the direction that the language is written (ltr=left to right, rtl = right to left)
language.direction=ltr

pdfPrompwt=PDF auswählen
pdfPrompt=Select PDF(s)
multiPdfPrompt=PDFs auswählen(2+)
multiPdfDropPrompt=Wählen Sie alle gewünschten PDFs aus (oder ziehen Sie sie per Drag & Drop hierhin)
imgPrompt=Wählen Sie ein Bild
Expand Down Expand Up @@ -802,6 +802,7 @@ ocr.submit=PDF mit OCR verarbeiten
extractImages.title=Bilder extrahieren
extractImages.header=Bilder extrahieren
extractImages.selectText=Wählen Sie das Bildformat aus, in das extrahierte Bilder konvertiert werden sollen
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Extrahieren


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_el_GR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Επεξεργασία PDF με OCR
extractImages.title=Εξαγωγή Εικόνων
extractImages.header=Εξαγωγή Εικόνων
extractImages.selectText=Επιλέξτε μορφή εικόνας για να μετατρέψετε τις εξαγόμενες εικόνες
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Εξαγωγή


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_en_GB.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Process PDF with OCR
extractImages.title=Extract Images
extractImages.header=Extract Images
extractImages.selectText=Select image format to convert extracted images to
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Extract


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Process PDF with OCR
extractImages.title=Extract Images
extractImages.header=Extract Images
extractImages.selectText=Select image format to convert extracted images to
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Extract


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_es_ES.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Procesar PDF con OCR
extractImages.title=Extraer imágenes
extractImages.header=Extraer imágenes
extractImages.selectText=Seleccionar el formato de imagen para convertir las imágenes extraídas
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Extraer


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_eu_ES.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=PDF prozesatu OCR-rekin
extractImages.title=Atera irudiak
extractImages.header=Atera irudiak
extractImages.selectText=Hautatu irudi-formatua ateratako irudiak bihurtzeko
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Atera


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_fr_FR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Traiter
extractImages.title=Extraire les images
extractImages.header=Extraire les images
extractImages.selectText=Format d’image dans lequel convertir les images extraites
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Extraire


Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/messages_ga_IE.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
###########
# Generic #
###########
# the direction that the language is written (ltr = left to right, rtl = right to left)
# the direction that the language is written (ltr=left to right, rtl = right to left)
language.direction=ltr

pdfPrompt=Roghnaigh PDF(s)
Expand Down Expand Up @@ -802,6 +802,7 @@ ocr.submit=Próiseáil PDF le OCR
extractImages.title=Sliocht Íomhánna
extractImages.header=Sliocht Íomhánna
extractImages.selectText=Roghnaigh formáid íomhá chun íomhánna bainte a thiontú go
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Sliocht


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_hi_IN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=OCR के साथ PDF प्रोसेस करें
extractImages.title=छवियां निकालें
extractImages.header=छवियां निकालें
extractImages.selectText=निकाली गई छवियों को कन्वर्ट करने के लिए छवि प्रारूप चुनें
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=निकालें


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_hr_HR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Obradi PDF sa OCR-om
extractImages.title=Ekstrakt slika
extractImages.header=Ekstrakt slika
extractImages.selectText=Odaberite format slike za pretvaranje izdvojenih slika
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Izdvajanje


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_hu_HU.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=PDF feldolgozása OCR-rel
extractImages.title=Képek kinyerése
extractImages.header=Képek kinyerése
extractImages.selectText=Válassza ki a képformátumot a kinyert képek konvertálásához
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Kinyerés


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_id_ID.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Memproses PDF dengan OCR
extractImages.title=Ekstrak Gambar
extractImages.header=Mengekstrak Gambar
extractImages.selectText=Pilih format gambar yang akan dikonversi
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Ekstrak


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_it_IT.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=Scansiona testo nel PDF con OCR
extractImages.title=Estrai immagini
extractImages.header=Estrai immagini
extractImages.selectText=Seleziona il formato in cui salvare le immagini estratte
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=Estrai


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_ja_JP.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=OCRでPDFを処理する
extractImages.title=画像の抽出
extractImages.header=画像の抽出
extractImages.selectText=抽出した画像のフォーマットを選択
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=抽出


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_ko_KR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ ocr.submit=인식
extractImages.title=이미지 추출
extractImages.header=이미지 추출
extractImages.selectText=추출된 이미지를 변환할 이미지 형식을 선택합니다.
extractImages.allowDuplicates=Save duplicate images
extractImages.submit=추출


Expand Down
Loading

0 comments on commit 47314a0

Please sign in to comment.