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

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

小陈同学 - 脚本

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 许可协议。转载请注明来自小陈同学