SVG绘制的路径不完全相同,但颜色不同

时间:2018-11-07 13:50:32

标签: svg graphics cairo antialiasing

我正在使用pycairo,下面是完整的代码段:

from math import pi
from cairo import PDFSurface, Context, Matrix
WIDTH = 6 * 72
HEIGHT = 4 * 72
s = PDFSurface('example1.pdf', WIDTH, HEIGHT)
c = Context(s)
# Draw a circle
c.save()
c.set_line_width(0)
for i in range(1000): 
    c.arc(1 * 72, 3 * 72, 0.5 * 72, 0, 2 * pi)
    c.set_source_rgb(1.0, 0, 0)
    c.fill()
    c.arc(1 * 72, 3 * 72, 0.5 * 72, 0, 2 * pi)
    c.set_source_rgb(1.0, 1.0, 1.0)
    c.fill()
s.finish()

我希望生成的pdf为空白。但是,您会看到一个红色圆圈。我知道这是由抗锯齿引起的,很久以前在这里也问过同样的问题: Two exactly same SVG paths not drawn exactly the same way 但是答案并没有解释抗锯齿是如何引起这种效果的。

如果有人可以告诉我它是怎么发生的,将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

想象一下,您想用红色绘制半个像素:执行cairo_rectangle(cr, 42, 42, 0.5, 1);得到一个半个像素宽的矩形。然后设置红色的源颜色并将其绘制(在白色背景上)。

预期结果是什么?

好吧,一半像素是白色((1, 1, 1)),一半像素是红色((1, 0, 0))。因此,逻辑上的做法是将红色和白色混合在一起,得到(1, 0.5, 0.5)之类的颜色。

接下来,您要用白色绘制相同的“半像素”。像素的“左半部分”为红色而“右半部分”为白色的信息未保存在任何地方。因此,像素只有这种“红白色混合”。因此,再次需要一些混合:(1, 0.5, 0.5)的一半(1, 1, 1)(1, 0.75, 0.75)的一半[W 23:00:48.600 LabApp] Error loading server extension ipyparallel.nbextension Traceback (most recent call last): File "/Users/MyName/anaconda3/lib/python3.7/site-packages/notebook/notebookapp.py", line 1546, in init_server_extensions mod = importlib.import_module(modulename) File "/Users/MyName/anaconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 953, in _find_and_load_unlocked File "", line 219, in _call_with_frames_removed File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 965, in _find_and_load_unlocked ModuleNotFoundError: No module named 'ipyparallel' [I 23:00:48.605 LabApp] JupyterLab extension loaded from /Users/MyName/anaconda3/lib/python3.7/site-packages/jupyterlab [I 23:00:48.605 LabApp] JupyterLab application directory is /Users/MyName/anaconda3/share/jupyter/lab [W 23:00:48.608 LabApp] JupyterLab server extension not enabled, manually loading... [I 23:00:48.609 LabApp] JupyterLab extension loaded from /Users/MyName/anaconda3/lib/python3.7/site-packages/jupyterlab [I 23:00:48.609 LabApp] JupyterLab application directory is /Users/MyName/anaconda3/share/jupyter/lab [I 23:00:48.611 LabApp] Serving notebooks from local directory: /Users/MyName [I 23:00:48.611 LabApp] The Jupyter Notebook is running at: [I 23:00:48.612 LabApp] http://localhost:8888/?token=8b2eb70aca4d7230313e1ec5178745ea84875981d0482c16 [I 23:00:48.612 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 23:00:48.616 LabApp] Copy/paste this URL into your browser when you connect for the first time, to login with a token: http://localhost:8888/?token=8b2eb70aca4d7230313e1ec5178745ea84875981d0482c16 [I 23:00:49.252 LabApp] Accepting one-time-token-authenticated connection from ::1 [W 23:00:49.856 LabApp] Could not determine jupyterlab build status without nodejs [E 23:00:49.862 LabApp] Uncaught exception GET /api/contents/?content=1&1543561249855 (::1) HTTPServerRequest(protocol='http', host='localhost:8888', method='GET', uri='/api/contents/?content=1&1543561249855', version='HTTP/1.1', remote_ip='::1') Traceback (most recent call last): File "/Users/MyName/anaconda3/lib/python3.7/site-packages/tornado/web.py", line 1592, in _execute result = yield result File "/Users/MyName/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 1133, in run value = future.result() File "/Users/MyName/anaconda3/lib/python3.7/site-packages/tornado/gen.py", line 326, in wrapper yielded = next(result) File "/Users/MyName/anaconda3/lib/python3.7/site-packages/notebook/services/contents/handlers.py", line 112, in get path=path, type=type, format=format, content=content, File "/Users/MyName/anaconda3/lib/python3.7/site-packages/notebook/services/contents/filemanager.py", line 431, in get model = self._dir_model(path, content=content) File "/Users/MyName/anaconda3/lib/python3.7/site-packages/notebook/services/contents/filemanager.py", line 337, in _dir_model if self.should_list(name) and not is_file_hidden(os_path, stat_res=st): File "/Users/MyName/anaconda3/lib/python3.7/site-packages/notebook/utils.py", line 145, in is_file_hidden_posix stat_res = os.stat(abs_path) OSError: [Errno 62] Too many levels of symbolic links: '/Users/MyName/config' [W 23:00:49.870 LabApp] Unhandled error [E 23:00:49.871 LabApp] { "Host": "localhost:8888", "Pragma": "no-cache", "Accept": "*/*", "Authorization": "token 8b2eb70aca4d7230313e1ec5178745ea84875981d0482c16", "Accept-Language": "en-us", "Accept-Encoding": "gzip, deflate", "Cache-Control": "no-cache", "Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.1 Safari/605.1.15", "Connection": "keep-alive", "Referer": "http://localhost:8888/lab", "Cookie": "username-localhost-8888=\"2|1:0|10:1543561249|23:username-localhost-8888|44:YmRiOThiODMxODA3NDM1NDhmMmNlYjZiMDM2YmFhZWI=|4cec00ae573ce2c242270e1ad51ea5b8bf633143c50175883402000209508f9e\"; username-localhost-8889=\"2|1:0|10:1543560086|23:username-localhost-8889|44:ZmQyNTdmMjU3OTQ4NDg4MjllNWE5MThiMDY4YmMxOTk=|0516e335b3a1b0666b7b03fe9e19820005734c51f9cff9aaa4d19bbe5ac3b72c\"; username-localhost-8890=\"2|1:0|10:1543352001|23:username-localhost-8890|44:MTlkMTFlNGYyNWQ5NDE3NDg0NDk4MTMyMmExMDRhMzY=|1f0e8c67a9bfa9bfe8367f0bc3fef515c76590f21c0f0f26576e466bfa7bd8b6\"; _xsrf=2|d4da0c6c|43dea648c15b852bca19be92af6821cf|1543351994" } [E 23:00:49.871 LabApp] 500 GET /api/contents/?content=1&1543561249855 (::1) 13.31ms referer=http://localhost:8888/lab ,或者1/4的红色和3/4的白色的混合。

因此,即使我们先用红色然后用白色填充了相同的几何图形,红色仍然部分可见。

这与抗锯齿有什么关系?圆并不完全适合圆网格。因此,在边缘附近,您会得到一些仅部分位于圆内的像素。抗锯齿意味着完成了与我上面解释的类似的操作:背景色和圆的填充色混合在一起。因此,这种混合意味着在再次“擦除”圆之后,某些第一色仍保持可见。