我已经设置了这个窗口,在这个窗口中,我在屏幕上画一个椭圆.我希望椭圆形是全白的,边框光滑.它看起来可以接受,但当我将椭圆添加到白色背景时,椭圆的边框就会显示出来.

import pygame
import cv2
import numpy as np

pygame.init()

# Set up the Pygame window
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))

def drawAACircle(surf, color, center, radius, width, angle):
    circle_image = np.zeros((radius*2, radius*2, 4), dtype = np.uint8)
    circle_image = cv2.ellipse(circle_image, (radius, radius), (radius-width, radius-width), (angle*-.5)-90 , 0, angle, (*color, 255), width, lineType=cv2.LINE_AA)  
    #draw it on the surface
    surf.blit(pygame.image.frombuffer(circle_image.tobytes(), circle_image.shape[1::-1], "RGBA").convert_alpha(), (center[0]-radius, center[1]-radius))

# Wait for the user to close the window
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    
    screen.fill((255,255,255))

    drawAACircle(screen, (255,255,255), (screen_width/2,screen_height/2), 200, 20, 360)

    # Update the display
    pygame.display.flip()

当我将背景更改为全白时,我注意到了边框:

black and white ellipse

我目前正在开发一个贴花工具,可以将多个椭圆堆叠在一起.我用copy()刷新背景屏幕,如下所示:

def takeSnapshot():
    global snapshot
    snapshot = screen.copy()

def clearSnapshot():
    global snapshot
    snapshot = None

----
# Run the main loop
running = True
while running:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            running = False      

    
    # Clear the screen
    if snapshot is None:
        screen.fill((0,0,0))
         
    else:
        screen.blit(snapshot, (0,0))

我已经使用填充的多边形try 了gfx.aapolygon ICM.但我就是不能把它弄得像OpenCV椭圆一样干净利落.

如果有人知道另一种 Select ,我会很高兴听到的,或者我可能忽略了一些我可以只得到完全白色的OpenCV椭圆的地方.

编辑:为了清楚起见,我 Select 了OpenCV椭圆作为厚度选项,可以做出一个弧形,看起来很流畅.

推荐答案

问题是,抗锯齿不仅应用于Alpha通道,还应用于 colored颜色 通道(RGB通道).基本上,这意味着 colored颜色 通道已经与Alpha通道相乘.要正确混合该纹理(请参见blit),必须使用"BRAND_PREMULTIPLIED"模式:

def drawAACircle(surf, color, center, radius, width, angle):
    circle_image = np.zeros((radius*2, radius*2, 4), dtype = np.uint8)
    circle_image = cv2.ellipse(circle_image, (radius, radius), (radius-width, radius-width), (angle*-.5)-90 , 0, angle, (*color, 255), width, lineType=cv2.LINE_AA)  
    circle_surf = pygame.image.frombuffer(circle_image.tobytes(), circle_image.shape[1::-1], "RGBA")
    pos = (center[0]-radius, center[1]-radius)
    
    surf.blit(circle_surf, pos, special_flags=pygame.BLEND_PREMULTIPLIED)

(我不在这里显示结果,因为它完全是白色的.)

Python相关问答推荐

线性模型PanelOLS和statmodels OLS之间的区别

如何使用pandasDataFrames和scipy高度优化相关性计算

在Python中处理大量CSV文件中的数据

通过Selenium从页面获取所有H2元素

如何使用数组的最小条目拆分数组

在pandas中使用group_by,但有条件

Python导入某些库时非法指令(核心转储)(beautifulsoup4."" yfinance)

启用/禁用shiny 的自动重新加载

在不同的帧B中判断帧A中的子字符串,每个帧的大小不同

使用Openpyxl从Excel中的折线图更改图表样式

使用__json__的 pyramid 在客户端返回意外格式

pandas:在操作pandora之后将pandora列转换为int

使用python playwright从 Select 子菜单中 Select 值

裁剪数字.nd数组引发-ValueError:无法将空图像写入JPEG

如何在SQLAlchemy + Alembic中定义一个"Index()",在基表中的列上

pytest、xdist和共享生成的文件依赖项

上传文件并使用Panda打开时的Flask 问题

正在try 让Python读取特定的CSV文件

如何计算Pandas 中具有特定条件的行之间的天差

使用Django标签显示信息