Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将SVG转换为灰度 脚本_Python_Svg_Anaconda_Grayscale - Fatal编程技术网

在python中将SVG转换为灰度 脚本

在python中将SVG转换为灰度 脚本,python,svg,anaconda,grayscale,Python,Svg,Anaconda,Grayscale,我正在尝试将一个彩色的example.svg文件转换为Windows10设备上Anaconda4.8.3版python 3.6中的灰度example\gs.svg 尝试 首先,我尝试应用正则表达式将“rgb(xxx,yyy,zzz)”转换为黑色,但这会创建一个黑色矩形,并在转换过程中丢失图像。接下来,我安装了inkscape并运行了一个灰度命令,该命令似乎正在工作,但没有修改example.svg。枕头图像的第三次尝试未加载.svg MWE 问题: 如何在windows设备上用python 3.

我正在尝试将一个彩色的
example.svg
文件转换为Windows10设备上Anaconda4.8.3版python 3.6中的灰度
example\gs.svg

尝试 首先,我尝试应用正则表达式将“rgb(xxx,yyy,zzz)”转换为黑色,但这会创建一个黑色矩形,并在转换过程中丢失图像。接下来,我安装了inkscape并运行了一个灰度命令,该命令似乎正在工作,但没有修改
example.svg
。枕头图像的第三次尝试未加载
.svg

MWE 问题:
如何在windows设备上用python 3.6将彩色
.svg
文件更改为灰度图像?

您选择了正确的工具来转换为灰度。您的最后一次尝试很好,但需要导入提供svg2png函数的。然后,使用Pillow加载png文件并将其转换为np.array,然后您可以轻松地使用openCV加载该文件并将其转换为灰度。最后,您可以使用svglib和reportlab导出svg中的图像。 以此代码段为例:

将矢量图像转换为PNG光栅图像,然后再转换回SVG矢量图像将丢失大量信息—如果它能正常工作的话。您希望如何进行反向转换?从svglib文档来看,它根本无法接受PNG作为输入,只能接受SVG。此外,我很高兴了解您更好的解决方案并从中学习。这只是我能想到的第一个解决方案。你链接的代码将光栅图像的每个像素转换成1*1px的矩形。生成的图像,虽然从技术上讲是矢量图像,但将失去其可伸缩性:将其缩放10倍,您将看到的只是一些块。-我没有好的解决方案,我的评论是关于光栅和矢量图像之间的一般关系以及矢量到光栅转换的数学事实。
# conda install -c conda-forge inkscape
# https://www.commandlinefu.com/commands/view/2009/convert-a-svg-file-to-grayscale
# inkscape -f file.svg --verb=org.inkscape.color.grayscale --verb=FileSave --verb=FileClose
import re
import os
import fileinput
from PIL import Image
import cv2

# Doesn't work, creates a black square. Perhaps set a threshold to not convert rgb white/bright colors
def convert_svg_to_grayscale(filepath):
    # Read in the file
    with open(filepath, 'r') as file :
      filedata = file.read()

    # Replace the target string
    filedata = re.sub(r'rgb\(.*\)', 'black', filedata)

    # Write the file out again
    with open(filepath, 'w') as file:
      file.write(filedata)
    
# opens inkscape, converts to grayscale but does not actually export to the output file again
def convert_svg_to_grayscale_inkscape(filepath):
   command = f'inkscape -f {filepath} --verb=org.inkscape.color.grayscale --verb=FileSave --verb=FileClose'
   os.system(f'cmd /k {command}')
   
# Pillow Image is not able to import .svg files
def grayscale(filepath):
    image = Image.open(filepath)
    cv2.imwrite(f'{filepath}', image.convert('L'))


# walks through png files and calls function to convert the png file to .svg
def main():
    filepath = 'example.svg'            
    convert_svg_to_grayscale_inkscape(filepath)
    convert_svg_to_grayscale(filepath)
    grayscale(filepath)
                

if __name__ == '__main__':
    main()