Oct-07-2023, 04:58 PM
Dear deanhystad, thank you very much for your help!!
Here I am sending what I made from your information.
I scanned a DIN A4 page (with part of the "hangerfinder's" code) and am now a bit surprised by the values.
Am I making a mistake in line 29?
Unfortunately my scanned pdf is too large to attach it.
Best regards,
flash77
Here I am sending what I made from your information.
I scanned a DIN A4 page (with part of the "hangerfinder's" code) and am now a bit surprised by the values.
Am I making a mistake in line 29?
Unfortunately my scanned pdf is too large to attach it.
Best regards,
flash77
import numpy as np from pdf2image import convert_from_path from PIL import Image import time def primary_color_ratio(pdf_name): """Return ratio of pixels that are the "background" color.""" pages = convert_from_path(pdf_name, 300) pages[0].save("1.bmp", "BMP") image_rgb = Image.open("1.bmp").convert('RGB') #examine just every xth pixel rgb = np.array(image_rgb).reshape(-1, 3)[::x] #get 24Bit Color b24 = rgb[:, 0] * 65535 + rgb[:, 1] * 256 + rgb[:, 2] _, counts = np.unique(b24, return_counts=True) return max(counts) / max(len(b24), 1) #examine every xth pixel x = 100 #"filled" is the content that can be read by humans (for example: writing) #"userdef_image_filled_ratio_whole_page": the ratio at which an image is considered filled userdef_image_filled_ratio_whole_page = 0.2 userdef_image_filled_ratio_x = userdef_image_filled_ratio_whole_page / x pdf_name = "t.pdf" startTime = time.time() primColorRatio = primary_color_ratio(pdf_name) print("primColorRatio = " + str(primColorRatio)) filled_ratio = 1 - primColorRatio endTime = time.time() print("filled_ratio = " + str(filled_ratio)) print(endTime - startTime) if filled_ratio >= userdef_image_filled_ratio_whole_page: print("The image is filled.") else: print("The image is empty.")