Wie kann ich Numpy -Array -Ausgänge von PDFIUM und STB_IMAGE ausrichten, wenn ich identische Inhalte rendert?Python

Python-Programme
Anonymous
 Wie kann ich Numpy -Array -Ausgänge von PDFIUM und STB_IMAGE ausrichten, wenn ich identische Inhalte rendert?

Post by Anonymous »

In C ++ versuche ich, ein Numpy -Array von einer PDF -Seite mit PDFIUM zu erhalten: < /p>

Code: Select all

py::array_t render_page_helper(FPDF_PAGE page, int target_width = 0, int target_height = 0, int dpi = 80) {
int width, height;

if (target_width > 0 && target_height > 0) {
width = target_width;
height = target_height;
} else {
width = static_cast(FPDF_GetPageWidth(page) * dpi / 72.0);
height = static_cast(FPDF_GetPageHeight(page) * dpi / 72.0);
}

FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 1);
if (!bitmap) throw std::runtime_error("Failed to create bitmap");

FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT);

int stride = FPDFBitmap_GetStride(bitmap);
uint8_t* buffer = static_cast(FPDFBitmap_GetBuffer(bitmap));

// Return numpy array with shape (height, width, 4) = BGRA
auto result = py::array_t({height, width, 4}, buffer);
FPDFBitmap_Destroy(bitmap);
return result;
}
< /code>
Das Ergebnis wird dann wieder in Python [url=viewtopic.php?t=23808]übergeben[/url] und mit: < /p>
verarbeitetarr = arr_bgra[:, :, [2, 1, 0]]
< /code>
Um den Alpha -Wert abzuschneiden und in das RGB -Format umzuordnen.py::array_t render_image(const std::string& filename, int target_width = 224, int target_height = 224) {
int width, height, channels;
unsigned char* rgba = stbi_load(filename.c_str(), &width, &height, &channels, 4); // force RGBA
if (!rgba) throw std::runtime_error("Failed to load image");

// Temporary buffer (still RGBA after resize)
std::vector resized(target_width * target_height * 4);
stbir_resize_uint8(rgba, width, height, 0,
resized.data(), target_width, target_height, 0, 4);
stbi_image_free(rgba);

// Allocate Python-owned buffer for final RGB output
py::array_t result({target_height, target_width, 3});
auto buf = result.mutable_unchecked();

// Convert RGBA → RGB (drop alpha)
for (int y = 0; y < target_height; ++y) {
for (int x = 0; x < target_width; ++x) {
int idx = (y * target_width + x) * 4;
buf(y, x, 0) = resized[idx + 0]; // R
buf(y, x, 1) = resized[idx + 1]; // G
buf(y, x, 2) = resized[idx + 2]; // B
}
}

return result;
}
, um ein Numpy -Array direkt zu verarbeiten und zurückzugeben. Und ich frage mich, ob es überhaupt möglich ist, Ergebnisse zu erzielen, die ohne PDFIUM ähnlich sind, da es eine gewisse Anforderung ist. Beispiel.>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post