为了进行设置,我使用了 svgwrite
用于创建示例 SVG 图像的库(在长度为 400 的显示尺寸上随机位置上的 20 个长度为 100 的正方形)
import svgwrite
import random
random.seed(42)
dwg = svgwrite.Drawing('x.svg', size=(400,400))
dwg.add(dwg.rect(insert=(0,0), size=('100%', '100%'), fill='white')) # White background
for i in range(20):
coordinates = (random.randint(0,399), random.randint(0,399))
color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
dwg.add(dwg.rect(coordinates, (100, 100),
stroke='black',
fill=svgwrite.rgb(*color),
stroke_width=1)
)
dwg.save()
然后我编写了一个示例 pygame 程序来生成相同示例的 PNG 图像。 (A
seed
已被用于生成相同的正方形序列。)import pygame
import random
random.seed(42)
display = pygame.display.set_mode((400,400))
display.fill((255,255,255)) # White background
for i in range(20):
coordinates = (random.randint(0,399), random.randint(0,399))
color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
pygame.draw.rect(display, color, coordinates+(100,100), 0)
pygame.draw.rect(display, (0,0,0), coordinates+(100,100), 1) #For black border
pygame.image.save(display, "x.png")
这些是我得到的图像(SVG 不能上传到 SO,所以我提供了一个截图。不过,上面的程序可以运行以输出相同的内容)。
我的问题是,为什么 PNG(左侧)比相应的 SVG 图像更丰富、更清晰?相比之下,SVG 看起来模糊而乏味。
编辑:可以注意到左上角前两个正方形之间的细白线。在 SVG 中不是很清楚。
最佳答案
我认为可能会影响两件事:
SVG
图片。我认为所有矢量图像查看器都会获得实际的屏幕尺寸,然后将矢量图像导出为矩阵图像,其大小取决于您拥有的屏幕尺寸。然后他们显示矩阵图像。如果他们以柔和的清晰度渲染图像,或者他们在获取屏幕尺寸时遇到问题,则图像可能会模糊。PNG
图片,您使用 pygame
. 但是您正在使用另一个模块 制作 SVG
图片。此模块的功能可能不同,并且导出的图像质量可能与使用 pygame
导出时不同。 .对我个人而言,
SVG
图像因 Gimp 而变得模糊,例如,但不是与另一个 SVG
观众。所以我认为问题来自您的图像查看器。
关于python-3.x - 为什么生成的 SVG 图像不如相应的 PNG 图像丰富,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62450932/