21

I'm trying to render an SVG->PNG using PythonMagick, but it seems that the background color is ignored.

Using the ImageMagick command, things work as expected:

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

which results in transparent and red backgrounds respectively.

But using PythonMagick, it seems the background color is ignored:

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')

Checking the output shows that both pngs have a white background. Is there another way to set the background color so that it is recognised when writing the image?

Michael Nelson
  • 478
  • 3
  • 7
  • i don't know PythonMagic, but are you sure you can't set the background color during `__init__` or `write`? e.g. `PythonMagick.Image('access.svg', 'red')` or `svg.write('access2.png', 'red')` ... – Aprillion Mar 09 '12 at 19:12

6 Answers6

1

ImageMagick (and PythonMagick as its python API) has very poor SVG support. Do not even expect that it will render SVG file as it is written in the SVG specification. It converts SVG into internal MVG language and then converts to PNG. See http://www.imagemagick.org/script/magick-vector-graphics.php

ImageMagick is not a tool for manipulating vector graphics.

Maksym Polshcha
  • 18,030
  • 8
  • 52
  • 77
0

PythonMagick is poorly documented (to be charitable), so you might want to stick with something that has better documentation, like PythonMagickWand. In general I recommend sticking with the Python Imaging Library but it does not support SVG so it won't work for you.

My guess as to what is wrong with your code is that when you modify an image, you may be creating a new image, even though you are seeing changes to the object you are retaining a reference to. So try

svg = svg.backgroundColor('none')
svg.write('access.png')
svg = svg.backgroundColor('red')
svg.write('access2.png')
Old Pro
  • 24,624
  • 7
  • 58
  • 106
0

Says here the default background color is white, so it seems like it is not getting it: http://www.imagemagick.org/script/command-line-options.php#background

Maybe you need to set the channel to 'RGBA', possibly this defaults to RGB however it seems like the background should be the correct color if so. As many of the flood fill examples do here: http://www.imagemagick.org/Usage/masking/#floodfill

Here's the output of the object:

['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']

From here: Documents and examples of PythonMagick, you can see channel in there.

Community
  • 1
  • 1
Ryan Christensen
  • 7,843
  • 1
  • 27
  • 25
0

In Imagick for PHP you'd go:

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

Where $im is your newly created Imagick object without anything loaded onto it.

Hope it helps.

RedRoosterMobile
  • 786
  • 10
  • 21
0

I don't see the problem if it works with command then use this:

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

And don't forget to import os

import os

additional you can use vars inside the command (don't forget to rename the vars):

image = 'convert -background '+background+' '+file_from+' '+file_to
Serg
  • 668
  • 1
  • 4
  • 6
0

This might be a circuitous way of getting to the solution, but could you create a separate background image in the colour you want and then compose the images together?

I think it would look something like this:

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

I have very little experience with PythonMagick, but that is what I would have tried after failing to set the background.

Sources:

brice
  • 24,329
  • 7
  • 79
  • 95