【问题标题】:How to set the background color when rendering a PNG using PythonMagick使用 PythonMagick 渲染 PNG 时如何设置背景颜色
【发布时间】:2011-08-13 23:49:28
【问题描述】:

我正在尝试使用 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') ...

标签: python imagemagick


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    如果它与命令一起使用,我看不到问题,然后使用这个:

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

    别忘了导入操作系统

    import os
    

    您还可以在命令中使用 vars(不要忘记重命名 vars):

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

    【讨论】:

    • 这不是一个非常便携的解决方案。
    【解决方案3】:

    这可能是获得解决方案的一种迂回方式,但您能否以您想要的颜色创建一个单独的背景图像,然后将这些图像组合在一起?

    我认为它看起来像这样:

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

    我对 PythonMagick 的经验很少,但这是我在设置背景失败后会尝试的。

    来源:

    【讨论】:

      【解决方案4】:

      PythonMagick 的文档记录很差(出于慈善目的),因此您可能希望坚持使用具有更好文档的东西,例如 PythonMagickWand。一般来说,我建议坚持使用Python Imaging Library,但它不支持 SVG,所以它不适合你。

      我对您的代码有什么问题的猜测是,当您修改图像时,您可能正在创建一个新图像,即使您看到保留引用的对象发生了变化。所以试试

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

      【讨论】:

        【解决方案5】:

        这里说默认背景颜色是白色,所以它似乎没有得到它: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,你可以在那里看到频道。

        【讨论】:

          【解决方案6】:

          在 Imagick for PHP 你会去:

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

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

          希望对你有帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-06-26
            • 1970-01-01
            • 2010-12-11
            • 1970-01-01
            • 1970-01-01
            • 2021-09-29
            • 1970-01-01
            • 2013-06-13
            相关资源
            最近更新 更多