提问人:YvanKOB 提问时间:10/4/2023 最后编辑:YvanKOB 更新时间:10/4/2023 访问量:38
为什么在尝试预处理图像时会出现重复的图像?
Why do I get duplicate images when I try to pre-process them?
问:
我的 python 函数的结果不是我想要的。你能帮我解决这个问题吗?
我想将 pdf 转换为黑白图像,同时在转换过程中对其进行预处理。但是我遇到的问题是我得到了两个结果:一个是将 pdf 转换为没有预处理的图像,第二个(我想要的)是 image_processor 函数应用的图像预处理。我只想获得第二个结果,但以 pdf_to_images 函数中编写的形式。以下是有问题的两个函数。你有什么建议来解决这个问题?
MAX_SIZE_MB = 5
def check_image_size(image_path):
file_size_MB = os.path.getsize(image_path) / (1024 * 1024)
if file_size_MB > MAX_SIZE_MB:
raise ValueError(f"The image size ({file_size_MB:.2f} MB) exceeds the allowed limit of {MAX_SIZE_MB} MB.")
print(f"Image size: {file_size_MB:.2f} MB")
def processor(input_image_path, contrast_factor=1.5, brightness_factor=1.5, scale_factor=2):
img_pil = Image.open(input_image_path)
img_np = np.array(img_pil)
if len(img_np.shape) == 3:
img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
else:
img_cv = img_np
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
img_clahe = clahe.apply(img_cv)
img_clahe_pil = Image.fromarray(img_clahe)
enhancer_contrast = ImageEnhance.Contrast(img_clahe_pil)
img_enhanced = enhancer_contrast.enhance(contrast_factor)
enhancer_brightness = ImageEnhance.Brightness(img_enhanced)
img_enhanced_bright = enhancer_brightness.enhance(brightness_factor)
img_enhanced_bright_convert = img_enhanced_bright.convert('L')
_, img_binary = cv2.threshold(np.array(img_enhanced_bright_convert), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
img_binary_pil = Image.fromarray(img_binary)
new_width = int(img_binary_pil.width * scale_factor)
new_height = int(img_binary_pil.height * scale_factor)
img_upscaled = img_binary_pil.resize((new_width, new_height), Image.BICUBIC)
output_path = input_image_path.replace('.jpeg', '_processed.png')
img_upscaled.save(output_path, "PNG", quality=100)
check_image_size(output_path)
return output_path
def pdf_to_images(pdf_file, output_dir, dpi=600):
doc = fitz.open(pdf_file)
scale_factor = dpi / 72
zoom_matrix = fitz.Matrix(scale_factor, scale_factor)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for page in doc:
pix = page.get_pixmap(alpha=False, matrix=zoom_matrix)
img = Image.frombytes("RGB", (pix.width, pix.height), pix.samples)
img_gray = img.convert("L")
output_file = f"{output_dir}/page{page.number}.jpeg"
# img_gray.filename = output_file
# img_gray.save(output_file, format='JPEG', quality=100, optimize=True)
quality = 100
while True:
with open(output_file, 'wb') as f:
img_gray.save(f, format='JPEG', quality=quality, optimize=True)
size = f.tell()
if size <= 1.5 * 1024 * 1024:
break
if quality <= 10:
break
quality -= 5
processed_image_path = processor(output_file)
print(f"Processed image saved at {processed_image_path}")
这是我得到的结果:
2023-10-03 08:01:45.310 python[4319:178909] +[CATransaction synchronize] called within transaction
Image size: 1.07 MB
Processed image saved at /Users/Desktop/page0_processed.png
Image size: 0.97 MB
Processed image saved at /Users/Desktop/page1_processed.png
Image size: 0.78 MB
Processed image saved at /Users/Desktop/page2_processed.png
Image size: 1.00 MB
Processed image saved at /Users/Desktop/page3_processed.png
Image size: 0.85 MB
Processed image saved at /Users/Desktop/page4_processed.png
Image size: 0.66 MB
Processed image saved at /Users/Desktop/page5_processed.png
Image size: 0.81 MB
Processed image saved at /Users/Desktop/page6_processed.png
Image size: 0.55 MB
Processed image saved at /Users/Desktop/page7_processed.png
Image size: 0.45 MB
Processed image saved at /Users/Desktop/page8_processed.png
Image size: 0.44 MB
Processed image saved at /Users/Desktop/page9_processed.png
Image size: 0.43 MB
Processed image saved at /Users/Desktop/page10_processed.png
Image size: 0.38 MB
Processed image saved at /Users/Desktop/page11_processed.png
Image size: 0.37 MB
Processed image saved at /Users/Desktop/page12_processed.png
答: 暂无答案
评论