- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想我正在做一些微不足道的标准任务:我正在转换一个 (py) cairo
表面到 PIL
(低)图像。原版cairo
表面用途ARGB
模式。目标PIL
图片使用 RGBA
,即我想保留所有颜色和 alpha channel 。然而,转换中的事情变得非常奇怪:似乎 cairo
在内部将其数据存储为 BGRA
,所以我实际上需要在转换过程中交换颜色 channel ,请参见此处:
import cairo
import gi
gi.require_version('Rsvg', '2.0')
from gi.repository import Rsvg
from PIL import Image
w, h = 600, 600
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h,)
ctx = cairo.Context(surface)
ctx.set_source_rgba(1.0, 1.0, 1.0, 1.0) # explicitly draw white background
ctx.rectangle(0, 0, w, h)
ctx.fill()
# tested with https://raw.githubusercontent.com/pleiszenburg/abgleich/v0.0.7/src/abgleich/share/icon.svg
layout = Rsvg.Handle.new_from_file('icon.svg')
layout.render_cairo(ctx)
# EXPORT TEST #1: cairo
surface.write_to_png('export_cairo.png') # ok, looks as expected
pil = Image.frombuffer(mode = 'RGBA', size = (w, h), data = surface.get_data(),)
b, g, r, a = pil.split() # Color swap, part 1: Splitting the channels
pil = Image.merge('RGBA', (r, g, b, a)) # Color swap, part 2: Rearranging the channels
# EXPORT TEST #2: PIL
pil.save('export_pil.png') # ok, looks as expected IF COLORS ARE REARRANGED AS ABOVE
以上测试使用
rsvg
,但也可以简单地用
cairo
画几条彩色线条来重现。 .
最佳答案
从开罗文档( https://www.cairographics.org/manual/cairo-Image-Surfaces.html#cairo-format-t ):
CAIRO_FORMAT_ARGB32
each pixel is a 32-bit quantity, with alpha in the upper 8 bits, then red, then green, then blue. The 32-bit quantities are stored native-endian. Pre-multiplied alpha is used. (That is, 50% transparent red is 0x80800000, not 0x80ff0000.) (Since 1.0)
'RGBa'
.
or is this actually the right way to do it?
buf = surface.get_data()
a = numpy.ndarray (shape=(w,h,4), dtype=numpy.uint8, buffer=buf)
# draw a vertical line
a[:,40,0] = 255 # byte 0 is blue on little-endian systems
a[:,40,1] = 0
a[:,40,2] = 0
因此,要从(用枕头说)BGRa 转换为 RGBa,您可以执行以下操作来交换红色和蓝色 channel (其中
a
是与上述类似的缓冲区):
(a[:,:,0], a[:,:,2]) = (a[:,:,2], a[:,:,0])
如果这真的比您通过中级的方法更好
Image
……好吧,我不知道。您必须判断执行此操作的最佳方法是什么。至少你现在应该能够解释为什么它是必要的(cairo 和 PIL 都没有支持通用的图像格式)
关于python - 将 ARGB32 (py)cairo 表面转换为 PIL(low) 图像 - 反色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63482663/
过去我在学习 Cairo 时拍过一些照片,但总是继续支持其他一些图形库。我的问题是我找不到一个很好的教程来为我的表面提供一个简单的显示。我总是在 GTK 或 QT 文档中挖掘与我想做的事情无关的事情。
我的看法 @api_view(['GET']) @renderer_classes((JSONRenderer,TemplateHTMLRenderer,BrowsableAPIRendere
这听起来很简单,但我找不到将 PDF 加载到开罗的方法。我希望能够以与 cairo.ImageSurface.create_from_png 相同的方式执行 cairo.PDFSurface.crea
我在让 Pango Cairo 自动换行时遇到问题。下面是一些演示代码。我将布局的宽度设置为与红色矩形相同,因此我希望它环绕到红色矩形。实际上,它只是在每一行上放置一个单词,就好像宽度设置得很小一样。
这是我的观点: from django.conf import settings from django.http import HttpResponse from django.template.l
我想在 linux (fedora 12) 下使用 GTK 3 创建一个窗口,并使用 cairo-gl 后端在其上绘制一个简单的矩形,为此我想创建一个 cairo-gl 表面。我该怎么做,任何人都可以
我正在尝试从线程绘制到 cairo 图像表面,但出现断言错误: gtk_mt: /build/buildd/cairo-1.10.2/src/cairo-surface.c:385: _cairo_s
我收到 OSError: dlopen() failed to load a library: cairo / cairo-2在新安装后尝试执行 Django 时。使用 Windows。 根据完整的跟
我正在运行 OSX Lion 并尝试为 goocanvas 导入 python 模块, 使用 python2.7. 我设法成功编译了 pygoocanvas-0.14.1,但是当我尝试通过 pytho
我正在使用 Stack 和 Nix 构建一个 Haskell 项目,并依赖于来自 Hackage 的 cairo 库。 当我构建项目时,出现错误:无法找到 pkg-config >= 0.9.0 或
我是开罗的新手,我已经阅读了其网站上的教程/文档。 现在我可以制作线条、矩形,基本上我可以渲染图像但不能渲染文本。 我正在使用以下代码 cairo_select_font_face (cr, "mon
根据cairo example code ,以下代码 double x=25.6, y=128.0; double x1=102.4, y1=230.4, x2=153.6, y2=2
我在两个位置安装了 python,在操作系统中默认为 2.6.6,在/usr/local/bin/python2.7 中为 2.7。 我已经使用 configure/make/make install
我希望我在开罗的文本遵循绘制的路径。类似于 this . 现在这个链接来自a post在声称已经对此进行编码的开罗邮件列表中。只有代码链接位于 svn.gnome.org这似乎已被撤下。我的问题是,有
我正在尝试学习如何使用 Cairo 2D drawing library与 xlib 曲面。 我编写了一个允许创建多个窗口的小测试程序。每个函数可能有一个自定义的paint()函数,定期调用该函数以向
如果我们不检查 cairo 上下文是否存在会发生什么,如果返回 false 会发生什么,例如: bool MyClass::on_draw(const Cairo::RefPtr& cr) {
我在尝试运行以下命令时不断收到此错误: python -m weasyprint http://weasyprint.org weasyprint.pdf 错误: raise OSError("d
我有一个简单的 Cairo 程序,它试图在 600x600 PNG 中绘制由点组成的对角线。但是,每当我尝试使用对 cairo_stroke() 的一次调用来渲染所有点时,输出似乎被截断了。 具体来说
我有一个 cairo_t cr,我可以用它来使用 cairo。我想尝试在这个 cairo 图形上创建一个突出显示效果,它应该执行以下操作之一: 调亮整个图像,使其看起来更亮一些 将背景,即图像的透明部
我正在使用 C++ 和 GTK3 开发一个应用程序,但我被卡住了。我用 glade 创建了一个可视化应用程序,它具有三列,其中一列(中间一列)是 DrawingArea。在那个 DrawingArea
我是一名优秀的程序员,十分优秀!