【教学类-102-04】自制剪纸图案(留白边、沿线剪)04——Python制作白色描边和黑线虚线间隔(透明png图片)

背景需求:

之前成功运用deepseek实现了透明背景PNG图片的主体图案的白色描边,在描边外延添加点状虚线

【教学类-102-03】自制剪纸图案(留白边、沿线剪)03——Python制作白色描边和黑点间隔(透明png图片)-优快云博客文章浏览阅读894次,点赞39次,收藏2次。【教学类-102-03】自制剪纸图案(留白边、沿线剪)03——Python制作白色描边和黑点间隔(透明png图片) https://blog.youkuaiyun.com/reasonsummer/article/details/147002991?spm=1011.2415.3001.5331

在以上代码基础上,把点状虚线轮廓改为线条虚线轮廓


'''
剪纸外轮廓描边虚线制作(黑色虚线)沿线剪——平均边缘虚线的距离(最终效果)
deepseek 阿夏
20250405
修改为虚线版本
'''

from PIL import Image, ImageDraw
import os
import math

# 白边宽度(像素)
white_border_width = 30
# 虚线参数
dash_length = 20    # 每段虚线长度(像素)
dash_gap = 10       # 虚线间距(像素)
dash_width = 3      # 虚线粗细(像素)

def get_edge_pixels(image):
    """获取图像中不透明像素与透明像素交界的边缘像素坐标"""
    edge_pixels = []
    pixels = image.load()
    width, height = image.size
    
    for y in range(height):
        for x in range(width):
            if pixels[x, y][3] > 0:  # 不透明像素
                # 检查4邻域
                for dx, dy in [(-1,0),(1,0),(0,-1),(0,1)]:
                    nx, ny = x+dx, y+dy
                    if 0 <= nx < width and 0 <= ny < height:
                        if pixels[nx, ny][3] == 0:  # 邻域透明
                            edge_pixels.append((x, y))
                            break
    return edge_pixels

def expand_edge_pixels(edge_pixels, distance, width, height):
    """扩展边缘像素坐标到指定距离"""
    expanded_pixels = set()
    for x, y in edge_pixels:
        for dy in range(-distance, distance+1):
            for dx in range(-distance, distance+1):
                nx, ny = x+dx, y+dy
                if 0 <= nx < width and 0 <= ny < height:
                    expanded_pixels.add((nx, ny))
    return expanded_pixels

def get_contour_pixels(border_pixels, width, height):
    """获取白边区域的外轮廓像素(使用边缘追踪算法)"""
    # 找到起始点(最左上角的边界像素)
    start_point = None
    for y in range(height):
        for x in range(width):
            if (x,y) in border_pixels:
                start_point = (x,y)
                break
        if start_point:
            break
    
    if not start_point:
        return []
    
    # 使用Moore-Neighbor追踪算法获取轮廓
    contour = []
    current = start_point
    previous = (current[0]-1, current[1])  # 假设从左侧开始
    
    directions = [
        (0, -1), (1, -1), (1, 0), (1, 1),
        (0, 1), (-1, 1), (-1, 0), (-1, -1)
    ]
    
    while True:
        contour.append(current)
        # 找到下一个边界点
        found = False
        start_dir = (directions.index((previous[0]-current[0], previous[1]-current[1])) + 1) % 8
        for i in range(8):
            dir_idx = (start_dir + i) % 8
            dx, dy = directions[dir_idx]
            neighbor = (current[0]+dx, current[1]+dy)
            
            if 0 <= neighbor[0] < width and 0 <= neighbor[1] < height:
                if neighbor in border_pixels:
                    previous = current
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿夏reasonsummer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值