彩色框图去掉彩色背景转为黑白框图

最近写专利文书时遇到一个有趣的要求,论文中的彩色神经网络结构图要去掉所有的彩色,变成黑白的。


test.jpg

但是作为懒人,肯定是不想重画,因此写了一段Python代码来解决这个问题。主要思路就是过滤像素点,将亮度超过阈值的像素全部设为白色,对于亮度较低的像素则只进行灰度化,不进行额外处理。

import numpy as np
from PIL import Image

def adjust_brightness(image_path, threshold):
    image = Image.open(image_path)
    grayscale_image = image.convert("L")
    pixel_array = np.array(grayscale_image)
    # 按照阈值筛选像素点
    bright_pixels = np.where(pixel_array > threshold)
    dark_pixels=np.where(pixel_array <= threshold)
    # 将高亮像素点设为白色(255)
    pixel_array[bright_pixels] = 255
    #pixel_array[dark_pixels] = 0  #考虑到jpg格式的压缩,彻底二值化的显示效果实测不是很好
    result_image = Image.fromarray(pixel_array)
    result_image.save("result_image.jpg")
    return result_image
 
image_path = "test.jpg"
threshold = 180  #亮度阈值,可以灵活调整
result_image = adjust_brightness(image_path, threshold)
result_image.show()

最后的结果:


result_image.jpg

最后图片的效果还是很满意的,虽然有一点点糊,但这个主要是jpg等压缩格式导致的,反正对专利文书来说,肯定是够了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容