2011-04-27 57 views
21

我试图渲染SVG->使用PythonMagick PNG PNG格式时,设置背景颜色,但它似乎忽略背景颜色。如何渲染使用PythonMagick

使用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图像有一个白色的背景。是否有另一种方式来设置背景颜色,使其写入图像时识别?

+0

我不知道PythonMagic,但你确定你不能在'__init__'或'write'期间设置背景颜色吗?例如'PythonMagick.Image('access.svg','red')''或'svg.write('access2.png','red')'...... – Aprillion 2012-03-09 19:12:30

回答

0

我看不出问题,如果它与命令的工作,然后用这个:

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

而且不要忘记导入操作系统命令里面

import os 

另外,您可以使用瓦尔(别忘了重新命名的增值经销商):

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

这不是一个非常便携的解决方案。 – I82Much 2011-06-06 14:32:28

0

这可能是让该解决方案的迂回方式,但ÿ可能OU创建您想要的颜色单独的背景图像,然后组成图像融合在一起?

我认为这将是这个样子:

image.composite(background, 0, 0, PythonMagick.CompositeOperator.SrcOverDst) 

我有PythonMagick的经验非常少,但这是我会没有设置背景后,都试过了。

来源:

0

PythonMagick的不良记录(以慈善),所以你可能想坚持的东西,有更好的文档,如PythonMagickWand。总的来说,我建议用Python Imaging Library坚持,但它不支持SVG,所以它不会为你工作。

我,什么是你的代码错误的猜测是,当您修改图像,你可以创建一个新的形象,即使你看到改变你保留了一个参考的对象。因此,尝试

svg = svg.backgroundColor('none') 
svg.write('access.png') 
svg = svg.backgroundColor('red') 
svg.write('access2.png') 
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'] 

从这里:Documents and examples of PythonMagick,你可以在那里看到通道。

1

ImageMagick(和PythonMagick作为它的python API)对SVG的支持很差。不要指望它会呈现SVG文件,因为它是在SVG规范中编写的。它将SVG转换为内部MVG语言,然后转换为PNG。请参阅http://www.imagemagick.org/script/magick-vector-graphics.php

ImageMagick 不是用于处理矢量图形的工具。

0

在Imagick为PHP你会去:

$im->setBackgroundColor(new ImagickPixel('transparent')); 

哪里是不什么新创建Imagick对象加载到它$ IM。

希望它有帮助。