[Python-Cv2] 彩色图转素描:Just for fun.

[ 观前声明:本文章仅限娱乐!!!素材取自网络。侵权删。]

一、效果对比(镇楼)

个人感觉效果还是蛮好的。

二、原理及实现

[Step.1] cv2下载

在cmd中输入下面这行代码:

pip install OpenCV-python

很简单,一句话搞定,等待安装结束就好,速度还可以,大概用了两分钟。有时候默认的pip源安装得太慢,可以试试下面这个:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python

这个对pip进行了临时换源,使用了清华的镜像源。如果安装的库比较多,觉得过于麻烦,可以尝试我开发的一键换源工具:[Python] pip换源工具(V0.0.1):第三方拓展库下载神器。具体可参考这篇文章。

[Step.2] 转换程序编写

这是程序核心函数,输入参数“图片地址”“保存地址”即可对图片进行处理并保存。

def change_photo(path, save_place):
    img = cv2.imread(path)

    # Image to Gray Image
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Gray Image to Inverted Gray Image
    inverted_gray_image = 255 - gray_image

    # Blurring The Inverted Gray Image
    blurred_inverted_gray_image = cv2.GaussianBlur(inverted_gray_image, (19, 19), 0)

    # Inverting the blurred image
    inverted_blurred_image = 255 - blurred_inverted_gray_image

    # Preparing Photo sketching
    sketck = cv2.divide(gray_image, inverted_blurred_image, scale=256.0)

    # cv2.imshow("Original Image", img)
    # cv2.imshow("Pencil Sketch", sketck)
    cv2.imwrite(save_place, sketck)
    cv2.waitKey(0)

[Step.3] gui编写

这次开发页面仍然使用Tkinter编写。

创建页面:

def page():
    root = tk.Tk()
    root.title('python 彩图转素描 [Made by Soaing_Hz]')
    ctypes.windll.shcore.SetProcessDpiAwareness(1)
    ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)
    root.tk.call('tk', 'scaling', ScaleFactor / 75)
    root.geometry('400x250+500+300')
    root.configure(background='white')
    root.resizable(False, False)
    root.mainloop()

很明显,我们需要2个输入框(原图地址,输出地址)和1个按钮(确认键)。将这些小组件添加到页面中:

    tk.Label(root, text='↓请输入原图地址:', background='white').place(x=20, y=13)
    entry_path = tk.Entry(root, width=30, background='white', relief=tk.FLAT)
    entry_path.place(x=20, y=40)
    tk.Frame(root, width=273, height=2, background='black').place(x=20, y=65)

    tk.Label(root, text='↓请输入输出地址:', background='white').place(x=20, y=93)
    entry_path = tk.Entry(root, width=30, background='white', relief=tk.FLAT)
    entry_path.place(x=20, y=120)
    tk.Frame(root, width=273, height=2, background='black').place(x=20, y=145)

    label = tk.Label(root, width=10, text='开始转换', bg='#3399ff', fg='white')
    label.place(x=275, y=200)

为Label绑定事件,使他能够成为一个按钮:

label.bind('<Button-1>', change)     # <Button-1>代表鼠标左键单击事件
label.bind('<ButtonRelease-1>', back)# <ButtonRelease-1>代表鼠标按键释放事件

编写一个中转站类型的函数:

    def back(event):
        label['bg'] = '#3399ff'

    def change(event):
        label['bg'] = '#0080ff'

        path = entry_path.get()
        new_path = entry_path2.get()

        change_photo(path=path, save_place=new_path)

这个函数还包含对按钮的一些动态处理,使他看起来更为真实。至此,这个程序的编写已大致完成。

三、总结

这个程序很大部分只是娱乐用 ,但也包含了cv2的粗略用法。=)

在这个小项目中,我们将探索如何使用Python和OpenCV库将一张彩色图片转换成一张具有艺术感的素描图。这不仅仅是一个技术练习,更是一次创意的尝试,让我们能够以一种全新的视角欣赏我们周围的世界。

当然,如果你喜欢的话,欢迎在评论区留言交流!对于代码中的细节问题或扩展需求,也可以提出你的想法,我们可以一起探讨。

感谢观看!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值