lessthanoptimal / PyBoof

Python wrapper around the BoofCV Computer Vision Library
Apache License 2.0
67 stars 12 forks source link

ndarray_to_boof crashes for larger images #30

Open nscheer opened 1 week ago

nscheer commented 1 week ago

Hi!

I'm currently playing around with PyBoof, as it seems to be the only library that is able to scan for MicroQR codes :)

When converting PDF files to images using a high DPI value (e.g. 600), I get crashes for ndarray_to_boof, whereas loading the image file directly using load_single_band works fine.

I don't know if I'm doing it wrong - here is my reproduction case:

import os
from PIL import Image
import numpy as np
import pyboof as pb

image_small_filename = "image_small.ppm"
image_big_filename = "image_big.ppm"

image_small = Image.new("RGB", (1000, 1000), (255,255,255))
image_small.save(image_small_filename, format="PPM")

image_big = Image.new("RGB", (5000, 5000), (255,255,255))
image_small.save(image_big_filename, format="PPM")

# directly loading the files works
print(f"loading file {image_small_filename}")
boof_image = pb.load_single_band(image_small_filename, np.uint8)
print("done")

print(f"loading file {image_big_filename}")
boof_image = pb.load_single_band(image_big_filename, np.uint8)
print("done")

os.remove("image_small.ppm")
os.remove("image_big.ppm")

# converting the small image to ndarray and loading it works
print(f"convert {image_small_filename} to ndarray and load it")
image_small = image_small.convert("L") 
image_array = np.array(image_small, dtype=np.uint8)
boof_image = pb.ndarray_to_boof(image_array)
print("done")

# ... but it crashes for an image of bigger dimensions
print(f"convert {image_big_filename} to ndarray and load it")
image_big = image_big.convert("L") 
image_array = np.array(image_big, dtype=np.uint8)
boof_image = pb.ndarray_to_boof(image_array)
print("done")

For me, running this using Python 3.10.12 on Ubuntu 22.04.5 LTS results in:

loading file image_small.ppm
done
loading file image_big.ppm
done
convert image_small.ppm to ndarray and load it
done
convert image_big.ppm to ndarray and load it
Traceback (most recent call last):
  File "/home/nscheer/src/qrtest/qr2.py", line 40, in <module>
    boof_image = pb.ndarray_to_boof(image_array)
  File "/home/nscheer/.local/lib/python3.10/site-packages/pyboof/image.py", line 218, in ndarray_to_boof
    return mmap_numpy_to_boof_U8(npimg, boof_img)
  File "/home/nscheer/.local/lib/python3.10/site-packages/pyboof/image.py", line 565, in mmap_numpy_to_boof_U8
    mm.write(numpy_image.data)
ValueError: data out of range

Any help/insight is appreciated :)