gpt4 book ai didi

python - 使用reportlab将背景图像添加到EPS

转载 作者:行者123 更新时间:2023-12-01 09:14:10 30 4
gpt4 key购买 nike

我正在使用reportlab来创建图表。所以我想生成一个 eps 格式文档,在其中添加带有图表的背景图像。当我尝试将背景图像添加到文档而不是作为背景时,它会堆叠在图表下方。当我运行 pdf 时使用相同的逻辑它可以工作。为了找出问题所在,我查看了reportlab库中的renderPS.py和renderPDF.py,它们分别是用于创建EPS和PDF的文件。在 renderPDF 中,我们使用 drawInlineImage() 在图表中生成内联图像,但 renderPS 不存在同样的情况。路径是正确的,因为我可以对 PDF 执行相同的操作,并且图像大小也正确。我也无法将drawInlineImage函数添加到renderPS.py。

import reportlab
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin,Image
from reportlab.lib.colors import blue, PCMYKColor
from reportlab.graphics.charts.textlabels import LabelOffset
from reportlab.graphics.charts.legends import Legend

class VBarChartWLineBarLabels01(_DrawingEditorMixin,Drawing):
def __init__(self,width=200,height=300,*args,**kw):
Drawing.__init__(self,width,height,*args,**kw)
self._add(self,VerticalBarChart(),name='chart',validate=None,desc=None)
self._add(self,Legend(),name='legend',validate=None,desc=None)
self.legend.columnMaximum = 2
self.legend.alignment='right'
self.legend.dx = 6
self.legend.dy = 6
self.legend.dxTextSpace = 5
self.legend.deltay = 10
self.legend.strokeWidth = 0
self.legend.strokeColor = None
self.legend.subCols[0].minWidth = 75
self.legend.subCols[0].align = 'left'
self.legend.subCols[1].minWidth = 25
self.legend.subCols[1].align = 'right'
self.legend.boxAnchor = 'c'
self.legend.y = 150
self.legend.colorNamePairs = [(PCMYKColor(0,54,100,10,alpha=100), ('1985')),(PCMYKColor(100,60,0,50,alpha=100), ('2035'))]
self.width = 400
self.legend.x = 440
self.chart.y = 30
self.chart.x = 35
self.chart.width=self.width-self.chart.x - 60
self.chart.height=self.height-2*self.chart.y
self.chart.valueAxis.drawGridLast = False
self.chart.categoryAxis.drawGridLast = False
self.chart.valueAxis.valueMax = 30
self.chart.valueAxis.valueMin = 0
self.chart.valueAxis.valueStep = 5
self.chart.valueAxis.gridStrokeDashArray = None
self.chart.valueAxis.gridEnd = self.width-60
self.chart.valueAxis.gridStart = 35
self.chart.valueAxis.visibleGrid =1
self.chart.valueAxis.labelTextFormat = '%s%%'
self.chart.valueAxis.maximumTicks = 20
self.chart.valueAxis.minimumTickSpacing = 10
self.chart.valueAxis.gridStrokeColor = PCMYKColor(0,0,0,25,alpha=85)
self.chart.valueAxis.labels.dx = -10
self.chart.barWidth = 3
self.chart.groupSpacing = 5
self.chart.data = [(15,17,14,12,15),(23,26,25,23,23)]
self.chart.bars[0].fillColor = PCMYKColor(0,54,100,10,alpha=100)
self.chart.bars[1].fillColor = PCMYKColor(100,90,0,50,alpha=100)
self.chart.categoryAxis.categoryNames = ["England","Wales","Scotland","Northern\nIreland","UK"]
self.chart.categoryAxis.strokeColor = PCMYKColor(0,0,0,25,alpha=85)
self.chart.categoryAxis.labels.textAnchor='middle'
self.chart.valueAxis.strokeColor = PCMYKColor(0,0,0,25,alpha=85)
self.chart.bars.strokeColor = None
self.chart.bars.strokeWidth = 0
inPath = 'gepgraphic-allocation-piechart-map.jpg'
img = Image(0, 0, self.chart.width, self.chart.height, inPath)
self.chart.background = img
if __name__=="__main__": #NORUNTESTS
VBarChartWLineBarLabels01().save(formats=['eps'],outDir='.',fnRoot=None)

这是上述问题的代码,它也适用于 pdf 以及在formats=['eps'] 中的最后一行代码,我们需要用 pdf 替换 eps 并尝试运行我想要生成的文件的代码它以 eps 格式生成相同的内容。

最佳答案

这是reportlab的一个错误,问题出在renderPS.py文件上。以下是您需要在函数 def drawImage(self, image):def drawImage(self, image, x1,y1, width=None,height=None):< 中更新的代码/strong> 分别在类_PSRenderer(Renderer)PSCanvas中。

def drawImage(self, image):
from reportlab.lib.utils import ImageReader
im = ImageReader(image.path)
self._canvas.drawImage(im._image,image.x,image.y,image.width,image.height)


def drawImage(self, image, x1,y1, width=None,height=None): # Postscript Level2 version
# select between postscript level 1 or level 2
if self.PostScriptLevel==1:
self._drawImageLevel1(image, x1,y1, width, height)
elif self.PostScriptLevel==2:
self._drawImageLevel2(image, x1, y1, width, height)
else :
raise ValueError('Unsupported Postscript Level %s' % self.PostScriptLevel)

注意:它适用于 reportlab 版本 2.5

关于python - 使用reportlab将背景图像添加到EPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51400869/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com