惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

小陈同学 - 脚本

FFmpeg 从入门到熟练:玩转视频剪辑、压缩与滤镜的神级工具 PIP 更换国内安装源 Conda安装与镜像源配置 vi/vim 编辑器使用 Liunx系统安装node和pm2
Python 绘制古风山水画
Caleb · 2022-10-25 · via 小陈同学 - 脚本

利用灰度梯度与虚拟光源计算浮雕阴影,将普通山水照片一键渲染为淡雅水墨风。

使用过部分 python 工具之后,想着是否可以通过操作图片来生成山水画,遂开始了想法。

上原图:

山水画原图

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import numpy as np
from PIL import Image 

class Landscape():
    def __init__(self, file, output_path):
        self.file           = file
        self.output_path    = output_path
        self.make_image(self.file)

    def make_image(self, file):
        new_filename = f"new_{os.path.split(file)[1]}"
        output = os.path.join(self.output_path, new_filename)
        a = np.asarray(Image.open(file).convert('L')).astype('float')

        # 根据灰度变化来模拟人类视觉的明暗程度
        depth = 6.              # 预设虚拟深度值为6 范围为0-100 
        grad = np.gradient(a)   # 提取梯度值
        grad_x, grad_y = grad   # 提取x y方向梯度值 解构赋给grad_x, grad_y

        # 利用像素之间的梯度值和虚拟深度值对图像进行重构
        grad_x = grad_x * depth / 100.
        grad_y = grad_y * depth / 100. #根据深度调整x y方向梯度值

        # 梯度归一化 定义z深度为1.  将三个梯度绝对值转化为相对值,在三维中是相对于斜对角线A的值
        A = np.sqrt(grad_x ** 2 + grad_y ** 2 + 1.)
        uni_x = grad_x / A 
        uni_y = grad_y / A 
        uni_z = 1./ A

        # 令三维中是相对于斜对角线的值为1
        vec_el = np.pi / 2.1                    # 光源俯视角度   弧度值  接近90度
        vec_az = np.pi / 4.                     # 光源方位角度   弧度值  45度
        dx = np.cos(vec_el) * np.cos(vec_az)    # 光源对x轴的影响 对角线在x轴投影
        dy = np.cos(vec_el) * np.sin(vec_az)    # 光源对y轴的影响 对角线在y轴投影
        dz = np.sin(vec_el)                     # 光源对z轴的影响 对角线在z轴投影

        b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z) # 光源归一化
        b = b.clip(0, 255)                               # 为了避免数据越界,生成灰度值限制在0-255区间
        im = Image.fromarray(b.astype('uint8'))          # 图像更构 
        im.save(output)                                  # 保存图片

def main():
    Landscape("./mountain.png", "./image")

if __name__ == "__main__":
    main()

简单的图片操作后,我们来看下效果图:

山水画_水墨

作者: 文章链接: https://reinness.com/posts/184 版权声明:  本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自小陈同学