My PDF converter needed image fixes and whitespace fixes together
The latest PDF conversion change in ebook2text fixes two things that don’t sound particularly related: extracting images, and putting spaces between words.
They meet in the text that comes out of the converter. A PDF can contain text I can extract directly, images that need OCR, or both. I need the result to read like the original document regardless of which path produced it.

A separate example of comparing extracted text with a scanned page: Proofread Page editor, screenshot by Tacsipacsi, displayed document by US Congress, CC BY-SA 4.0; unmodified. The interface is also subject to the GNU GPL.
In my November 10 change, the image work takes most of the diff. The whitespace fix takes one character.
The missing space
My text extractor collects pieces of text from the PDF layout and joins them. It uses punctuation at the end of a piece as a clue that there should be a newline. Otherwise, the text continues on the same line.
That second case looked like this:
else line + ""
Adding an empty string doesn’t put anything between the pieces. If the extracted text doesn’t already end with whitespace, the last word of one piece and the first word of the next become one word.
Here is a small teaching example, separate from the converter:
pieces = ["A paragraph can continue", "on another line"]
print("".join(pieces))
# A paragraph can continueon another line
print(" ".join(pieces))
# A paragraph can continue on another line
The fix in the converter keeps the existing punctuation check and changes the continuation case:
return "".join(
line + "\n"
if self.converter.ends_with_punctuation(line)
else line + " "
for line in paragraph
)
This still makes an assumption about the document. A line ending with punctuation isn’t necessarily the end of a paragraph, and a line without punctuation isn’t necessarily a continuation. Adding the space repairs the word joining. It doesn’t solve PDF layout.
An image needs more than its dimensions
The image extractor had a different assumption. It passed every image stream to Pillow as mode "1", which describes one-bit pixels, then converted the result to grayscale.
But the PDF image object also carries information about its color space and the number of bits used for each component. If I ignore that information, I can give Pillow the correct width and height and still ask it to interpret the bytes incorrectly.
The revised code reads BitsPerComponent and ColorSpace, selects a Pillow mode, and passes that mode into the image creation method. It handles DeviceGray, DeviceRGB, and DeviceCMYK explicitly. A one-bit grayscale image keeps mode "1"; other grayscale input uses "L".
There is a separate helper for expanding two-bit and four-bit values into eight-bit values. For each packed value, it scales the available range to the range from zero to 255. One-bit and eight-bit data pass through that helper unchanged.
That division makes the conversion easier to follow. Reading the PDF’s image properties happens before creating the Pillow image. The image creation method no longer has to pretend that every stream has the same representation.
It also changes the intermediate image format from JPEG to PNG. The method now converts one-bit images to grayscale and CMYK images to RGB before saving them. That image is then encoded as base64 for the OCR path.
There are still limits in this code. Unknown color spaces fall back to RGB, which is an assumption I would want fixtures to exercise. The size checks also reject images the code considers too small or probably full-page images. Those choices matter to which images reach OCR at all.
Checking the output of both paths
The text extractor combines OCR text with the text it extracted from the page. Fixing image interpretation helps the OCR input; fixing the separator helps the directly extracted text. Both changes affect what somebody gets back from the same conversion.
I need tests at those smaller boundaries, including known packed pixel values and text pieces with no existing separator. Then I need PDF files that put the paths together. A successful image conversion won’t catch two words stuck together farther along in the process.
The continuation now supplies the missing space. I’d like to see both fixes work on the same PDF, with text and images I can recognize in the output.