Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with --negate-colors Parameter Not Inverting Colors as Expected #435

Open
xingwozhonghua126 opened this issue Dec 10, 2024 · 5 comments

Comments

@xingwozhonghua126
Copy link

xingwozhonghua126 commented Dec 10, 2024

Description:

Hello CairoSVG team,

I've encountered an issue where the --negate-colors or -n parameter is not functioning as intended when converting SVG files to PNG format. The documentation suggests that this parameter should replace every vector color with its complement, but it seems to have no effect on the output.

Steps to Reproduce:

Use the CairoSVG command with the --negate-colors flag: cairosvg input.svg -o output.png -n (240 / 255, 248 / 255, 255 / 255, 1)

Observe that the colors in the output PNG do not change as expected.

Expected Behavior:

The vector colors in the SVG should be replaced with their complements in the output PNG.

Actual Behavior:

The colors remain unchanged in the output PNG.

Environment:
I have tried to work around this issue by generating the PNG first and then processing the image with additional code to invert the colors. While this is a viable solution, it would be more efficient if the --negate-colors parameter could perform this task directly.

Code for Color Inversion Post-PNG Generation:

import cairosvg
import os

inputFolder = "C:/Users/fab/Desktop/TEMP/SVG/"   
outputFolder = "C:/Users/fab/Desktop/TEMP/PNG/"  

for root, dirs, files in os.walk(inputFolder):
    for f in files:
        svgFile = os.path.join(root,f)  
        if f[-3:] == "svg":    
            pngFile = outputFolder + "/" + f.replace("svg","png") 
            cairosvg.svg2png(url=svgFile, write_to=pngFile,negate_colors=(240 / 255, 248 / 255, 255 / 255, 1))
@liZe
Copy link
Member

liZe commented Dec 10, 2024

Hi!

--negate-colors doesn’t take an argument, it inverts all colors. You should use cairosvg input.svg -o output.png -n instead. Setting a tuple for negate_colors has the same effect as setting True.

@xingwozhonghua126
Copy link
Author

png_replace_color

import os
from PIL import Image

# 定义输入和输出文件夹路径
input_folder = 'C:/Users/temp/Desktop/TEMP/PNG/'  # 替换为包含PNG文件的文件夹路径
output_folder = 'C:/Users/temp/Desktop/TEMP/PNG_OUT/'  # 替换为要保存PNG文件的文件夹路径

# 确保输出文件夹存在,如果不存在则创建
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# 定义要替换的颜色及其新颜色
# 例如:将所有像素值为(255, 0, 0)的红色像素替换为(0, 255, 0)的绿色像素
old_color = (255, 255, 255)
new_color = (177, 181, 188)

# 遍历文件夹中的所有PNG文件
for filename in os.listdir(input_folder):
    if filename.endswith('.png'):
        # 构建完整的文件路径
        input_path = os.path.join(input_folder, filename)
        output_path = os.path.join(output_folder, filename)
        
        # 打开图像
        img = Image.open(input_path)
        img = img.convert("RGBA")  # 将图像转换为可编辑的模式(RGBA表示带Alpha通道的RGB模式)
        pixels = img.load()
        
        # 替换所有目标颜色的像素
        for y in range(img.height):
            for x in range(img.width):
                if pixels[x, y][:3] == old_color:  # 只比较RGB部分,忽略Alpha通道
                    pixels[x, y] = new_color + (pixels[x, y][3],)  # 保留Alpha通道
        
        # 保存替换颜色后的图像
        img.save(output_path, 'PNG')
        print(f'转换: {input_path} -> {output_path}')

print('转换完成!')

rotated

import os
from PIL import Image

# 定义输入和输出文件夹路径
input_folder = 'C:/Users/fab/Desktop/TEMP/PNG/'  # 替换为包含PNG文件的实际输入文件夹路径
output_folder = 'C:/Users/fab/Desktop/TEMP/PNG_OUT/'  # 替换为要保存旋转后PNG文件的实际输出文件夹路径

# 确保输出文件夹存在,如果不存在则创建
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# 遍历文件夹中的所有PNG文件
for filename in os.listdir(input_folder):
    if filename.endswith('.png'):
        # 构建完整的文件路径
        input_path = os.path.join(input_folder, filename)
        output_path = os.path.join(output_folder, filename)
        
        # 打开图像
        img = Image.open(input_path)
        
        # 对图像进行90度旋转(逆时针旋转90度,如需顺时针旋转90度可使用Image.ROTATE_90)
        rotated_img = img.rotate(90, expand=True)  # expand=True表示扩展画布大小以适应旋转后的图像
        
        # 保存旋转后的图像
        rotated_img.save(output_path, 'PNG')
        print(f'旋转: {input_path} -> {output_path}')

print('图片旋转完成!')

My intention is not to invert the colors. The SVG file is colorless and I need to color the text part of the converted PNG, while leaving the other parts blank. I am currently writing a piece of code to achieve this conversion.

@xingwozhonghua126
Copy link
Author

Is there a way to set a fixed size when converting to PNG? Currently, it is a streamlined conversion, but generally, PNG requires a square shape, while the SVG is directly converted into a rectangle.

@xingwozhonghua126
Copy link
Author

Is there a way to support the background color function? The internal icon remains transparent, while the outer color can be set. The background can be a rounded rectangle, a regular rectangle, or a circle, and a radian parameter needs to be set.

@liZe
Copy link
Member

liZe commented Dec 14, 2024

If you need multiple image manipulation features, you really should use a third-party library (PIL, ImageMagick…). CairoSVG is a SVG-to-raster-image converter, not an image manipulation library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants