使用PythonMagick在渲染PNG时如何设置背景色

21

我正在尝试使用PythonMagick将SVG渲染为PNG,但是似乎忽略了背景颜色。

使用ImageMagick命令,一切都按预期工作:

tmp$ convert -background none access.svg access.png
tmp$ convert -background red access.svg access2.png

在使用PythonMagick时,似乎忽略了背景色:

这导致分别产生透明和红色的背景。

import PythonMagick
svg = PythonMagick.Image('access.svg')
svg.backgroundColor().to_std_string()
'#FFFFFFFFFFFF'
svg.backgroundColor().alpha()
0.0
svg.backgroundColor('none')
svg.backgroundColor().to_std_string()
'#0000000000000000'
svg.backgroundColor().alpha()
1.0
svg.write('access.png')
svg.backgroundColor('red')
svg.backgroundColor().to_std_string()
'#FFFF00000000'
svg.backgroundColor().alpha()
0.0
svg.write('access2.png')

检查输出结果,发现两个PNG文件都具有白色背景。是否有另一种方法可以设置背景颜色,从而在编写图像时被识别?


我不了解PythonMagic,但你确定在__init__write期间不能设置背景颜色吗?例如:PythonMagick.Image('access.svg', 'red') 或者 svg.write('access2.png', 'red') - Aprillion
6个回答

1

ImageMagick(以及其Python API PythonMagick)对SVG的支持非常差。甚至不要期望它会按照SVG规范渲染SVG文件。它将SVG转换为内部MVG语言,然后再转换为PNG。请参见http://www.imagemagick.org/script/magick-vector-graphics.php

ImageMagick不是用于操作矢量图形的工具。


0
这可能是一个绕弯的方式来达到解决方案,但你可不可以创建一个单独的背景图片,选择你想要的颜色,并将这些图片组合在一起呢?
我认为它应该看起来像这样:
image.composite(background, 0, 0, PythonMagick.CompositeOperator.SrcOverDst)

我对PythonMagick的经验非常有限,但在无法设置背景后,这就是我会尝试的。

来源:


0

如果使用命令可以解决问题,那我就没问题了:

image = 'convert -background red access.svg access.png'
os.system(image)

别忘了导入os模块

import os

此外,您可以在命令中使用变量(不要忘记重命名变量):

image = 'convert -background '+background+' '+file_from+' '+file_to

5
那不是一个非常便携的解决方案。 - I82Much

0

这里说默认的背景颜色是白色,所以看起来好像没有获取到它:http://www.imagemagick.org/script/command-line-options.php#background

也许你需要将通道设置为“RGBA”,可能默认为RGB,但如果是这样的话,背景应该是正确的颜色。就像这里的许多泛洪填充示例一样:http://www.imagemagick.org/Usage/masking/#floodfill

这是对象的输出:

['class', 'delattr', 'dict', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'init', 'instance_size', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', 'adaptiveThreshold', 'addNoise', 'adjoin', 'affineTransform', 'animationDelay', 'animationIterations', 'annotate', 'antiAlias', 'attribute', 'backgroundColor', 'backgroundTexture', 'baseColumns', 'baseFilename', 'baseRows', 'blur', 'border', 'borderColor', 'boundingBox', 'boxColor', 'cacheThreshold', 'channel', 'channelDepth', 'charcoal', 'chop', 'chromaBluePrimary', 'chromaGreenPrimary', 'chromaRedPrimary', 'chromaWhitePoint', 'classType', 'clipMask', 'colorFuzz', 'colorMap', 'colorMapSize', 'colorSpace', 'colorize', 'columns', 'comment', 'compare', 'compose', 'composite', 'compressType', 'contrast', 'convolve', 'crop', 'cycleColormap', 'debug', 'defineSet', 'defineValue', 'density', 'depth', 'despeckle', 'directory', 'display', 'draw', 'edge', 'emboss', 'endian', 'enhance', 'equalize', 'erase', 'fileName', 'fileSize', 'fillColor', 'fillPattern', 'fillRule', 'filterType', 'flip', 'floodFillColor', 'floodFillOpacity', 'floodFillTexture', 'flop', 'font', 'fontPointsize', 'fontTypeMetrics', 'format', 'frame', 'gamma', 'gaussianBlur', 'geometry', 'gifDisposeMethod', 'iccColorProfile', 'implode', 'interlaceType', 'iptcProfile', 'isValid', 'label', 'lineWidth', 'magick', 'magnify', 'map', 'matte', 'matteColor', 'matteFloodfill', 'meanErrorPerPixel', 'medianFilter', 'minify', 'modifyImage', 'modulate', 'modulusDepth', 'monochrome', 'montageGeometry', 'negate', 'normalize', 'normalizedMaxError', 'normalizedMeanError', 'oilPaint', 'opacity', 'opaque', 'page', 'penColor', 'penTexture', 'ping', 'pixelColor', 'process', 'profile', 'quality', 'quantize', 'quantizeColorSpace', 'quantizeColors', 'quantizeDither', 'quantizeTreeDepth', 'raise', 'read', 'readPixels', 'reduceNoise', 'registerId', 'renderingIntent', 'resolutionUnits', 'roll', 'rotate', 'rows', 'sample', 'scale', 'scene', 'segment', 'shade', 'sharpen', 'shave', 'shear', 'signature', 'size', 'solarize', 'spread', 'statistics', 'stegano', 'stereo', 'strokeAntiAlias', 'strokeColor', 'strokeDashOffset', 'strokeLineCap', 'strokeLineJoin', 'strokeMiterLimit', 'strokePattern', 'strokeWidth', 'subImage', 'subRange', 'swirl', 'syncPixels', 'textEncoding', 'texture', 'threshold', 'throwImageException', 'tileName', 'totalColors', 'transform', 'transformOrigin', 'transformReset', 'transformRotation', 'transformScale', 'transformSkewX', 'transformSkewY', 'transparent', 'trim', 'type', 'unregisterId', 'unsharpmask', 'verbose', 'view', 'wave', 'write', 'writePixels', 'x11Display', 'xResolution', 'yResolution', 'zoom']

从这里:PythonMagick文档和示例,你可以在其中看到通道。


0
在 PHP Imagick 中,您可以这样做:
$im->setBackgroundColor(new ImagickPixel('transparent'));

其中$im是你新创建的Imagick对象,还没有加载任何内容。

希望能对你有所帮助。


0

PythonMagick文档质量较差(姑且这么说),所以你可能想选择一些有更好文档的东西,比如PythonMagickWand。总的来说,我建议使用Python Imaging Library,但它不支持SVG,所以对你来说行不通。

我猜测你代码的问题可能是,当你修改一个图像时,你可能创建了一个新的图像,尽管你看到了对你保留引用的对象的更改。所以试试

svg = svg.backgroundColor('none')
svg.write('access.png')
svg = svg.backgroundColor('red')
svg.write('access2.png')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接