有没有办法使用PIL和matplotlib来放置EPS或SVG (或任何可伸缩的矢量格式)的徽标,以便将徽标放在图形上并将最终文件输出为EPS。现在我得到了一个糟糕的渲染图形,因为有一个.png文件试图转换为EPS格式,目标是将图像保存为.eps或.svg。
我认为这可能是由于后端的限制,我愿意改变我使用的。
这是不起作用的:
ax1.set_axis_bgcolor('#fafafa')
img = image.imread('./static/images/logo.png')
image_axis = fig.add_axes(ax1.get_position())
image_axis.patch.set_visible(False)
image_axis.yaxis.set_visible(False)
image_axis.xaxis.set_visible(False)
image_axis.set_xlim(0,19.995)
image_axis.set_ylim(0,11.25)
image_axis.imshow(img, extent=(11.79705,18.99525,.238125,1.313625), zorder=-1, alpha=0.15) #need to keep a 5.023 x by y ratio (.4 x .079)
fig.savefig('static/images/graphs/'+filename+'.eps', format='eps', bbox_inches='tight')有什么更新吗?
发布于 2020-11-21 03:27:15
这是一个古老的问题,但对于想要做同样事情的现在和未来的访问者来说,这是值得回答的。
有一种方法可以加载SVG文件并将其转换为路径对象,在svgpath2mpl包中实现。在项目的页面上:
中的路径是由一个‘
d="(path data)"’元素定义的,该元素包含一个路径属性,该属性包含移动、直线、曲线(三次和二次贝塞尔曲线)、圆弧和闭合路径指令。Matplotlib实际上本机支持所有这些指令,但没有提供解析器或完全兼容的API。
发布于 2015-09-21 19:23:15
在这种情况下,我通常会使用如下内容来实现更好的渲染:
from PIL import Image
logo = Image.open('icons\logo.png')
# TODO define bbox as a matplotlib.transforms.Bbox
image_axis.imshow(logo, clip_box=bbox, aspect='equal',
origin='lower', interpolation='nearest')(Matplotlib 1.4.2,如果您使用旧版本,使用interpolation和origin选项可能会有所帮助)
发布于 2016-02-03 05:40:04
我删除了我的代码片段,因为它没有用。
在Matplotlib中不能使用原生SVG作为图像。Matplotlib本机只支持png,然后回退到枕头,它也不支持svg,请参阅http://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html或matplotlib图像文档。
枕头支持eps,所以会工作的!(我不确定matplotlib会在内部做什么)
我是这样使用它的:
# Pillow must be there
import matplotlib.image as mpimg
def Save_svg(fig):
# expects a matplotlib figure
# And puts a logo into a frameless new axes
imagefile = 'logo.eps'
svgfile = 'output.svg'
logoax = [0.265, 0.125, 0.3, 0.3]
img=mpimg.imread(imagefile)
#
newax = fig.add_axes(logoax, anchor='SW', zorder=100)
newax.imshow(img)
newax.axis('off')
#
fig.savefig(svgfile,dpi=300)https://stackoverflow.com/questions/30311198
复制相似问题