I am wondering if there is a software library that enables opening a jpeg and converting a subset of it to an uncompressed array (such as Pillow). This would be in contrast to the more usual method which is to open the file, convert it fully to a bit array, then take a subset of the bit array.
In my case the subset that I have in mind is the upper left corner. The files decompress to 2544 × 4200 pixels, but I am only interested in the top left 150 x 900 pixels.
The background is below:
I am hopeful that the JPEG format is a string of compressed subpanels and an algorithm could stop when it had processed enough subpanels to fulfill the required subset of the image.
I have been searching for a while but have not found any mention of such an algorithm which, admittedly, is a special case.
Background
I use pyzbar to capture a barcode from the top left corner of a JPEG image as produced a high-speed scanner. Generally this required about 250 msecs per image. The actual Pyzbar time is about 2.5 msecs while the other 99% of the time is spent reading the image from a file, having it decompressed using Pillow, extracting the upper left corner.
The non-profit where I do this work as a volunteer cannot really afford to replace the $25K scanner and the channel that this clunker has is the overall bottleneck. Telling the scanner to send uncompressed images would slow the whole process down by at least 90%
I don't know of an existing library that can do this, but it is possible to modify jpegtran.c
and jdtrans.c
from the IJG C library to only read as many MCU rows as necessary when cropping, and in your specific case the decode time should be reduced by ~75% (900 / 4200 lines).
Since you are using Python, you could obtain a suitably cropped jpeg with:
os.popen("jpegtran -crop 152x904 %s" % inputfile).read()
This assumes the input jpeg is not progressive.