Skip to main content
 首页 » 编程设计

python之给 reportlab.lib.utils.ImageReader 一个字节

2024年11月24日9yjmyzz

我读到您可以使用类似字节的对象来reportlab.lib.utils.ImageReader()。如果我读取文件路径,它工作正常,但我想使用类似字节的对象,这样我就可以将我想要的绘图保存在内存中,而不必不断地在驱动器上保存更新的绘图。

这是我找到将图像转换为字符串的代码的地方 https://www.programcreek.com/2013/09/convert-image-to-string-in-python/

这是一个如何使用 BytesIO 作为 ImageReader() 输入的例子 How to draw image from raw bytes using ReportLab?

此类用于制作绘图并使用 BytesIO() 将其保存到内存中。 string 是我稍后要传递的值

#imports 
 
import PyPDF2 
from io import BytesIO 
 
from reportlab.lib import utils 
from reportlab.lib.pagesizes import landscape, letter 
from reportlab.platypus import (Image, SimpleDocTemplate, 
                                Paragraph, Spacer) 
from reportlab.lib.styles import getSampleStyleSheet 
from reportlab.lib.units import inch, mm 
import datetime 
import os 
import csv 
import io 
import base64 
import urllib 
from django.contrib import admin 
from django.forms import model_to_dict 
from django.http import HttpResponse 
from django.urls import path 
from django.views.decorators.csrf import csrf_protect 
from django.utils.decorators import method_decorator 
from reporting import models, functions, functions2 
import matplotlib 
matplotlib.use('agg') 
from matplotlib import pyplot as plt 
import numpy as np 
def make_plot(data): 
    items = [tuple(item) for item in data.items()] 
    keys = [item[0] for item in items] 
    vals = [item[1] for item in items] 
    fig, ax = plt.subplots() 
    ind = np.arange(len(keys))  # the x locations for the groups 
    width = 0.35  # the width of the bars 
    rects1 = ax.bar(ind - width/2, vals, width) 
    ax.set_ylabel('Count') 
    ax.set_xticks(ind) 
    ax.set_xticklabels(keys) 
    buf = io.BytesIO() 
    fig.savefig(buf, format='png') 
    buf.seek(0) 
    string = base64.b64encode(buf.read()) 
    return 'data:image/png;base64,' + urllib.parse.quote(string), string 

这是显示信息如何移动到错误发生位置的最少代码。

class ProgressReportAdmin(ReadOnlyAdmin): 
    current_extra_context = None 
 
    @csrf_protect_m 
    def changelist_view(self, request, extra_context=None): 
       plot = make_plot(data) 
       self.current_extra_context = plot[1] 
 
    def export(self, request): 
       image = self.current_extra_context 
       pdf = functions.LandscapeMaker(image, fname, rotate=True) 
       pdf.save() 

这里是错误发生的地方,在scaleImage函数中

class LandscapeMaker(object): 
    def __init__(self, image_path, filename, rotate=False): 
           self.pdf_file = os.path.join('.', 'media', filename) 
           self.logo_path = image_path 
           self.story = [Spacer(0, 1*inch)] 
 
    def save(self): 
        fileObj = BytesIO() 
        self.doc = SimpleDocTemplate(fileObj, pagesize=letter, 
                                         leftMargin=1*inch) 
        self.doc.build(self.story, 
                       onFirstPage=self.create_pdf) 
 
    def create_pdf(self, canvas, doc): 
        logo = self.scaleImage(self.logo_path) 
 
    def scaleImage(self, img_path, maxSize=None): 
        #Error1 occurs on 
        img = utils.ImageReader(img_path) 
        img.fp.close() 
 
        #Error2 
        #image = BytesIO(img_path) 
        #img = utils.ImageReader(image) 
        #img.fp.close() 

对于 Error1,我收到:

raise IOError('Cannot open resource "%s"' % name)
img = utils.ImageReader(img_path)
"OSError: Cannot open resource "b'iVBORw0KGgoAAA' etc.,

对于 Error2 我收到了

OSError: cannot identify image file <_io.BytesIO object at 0x7f8e4057bc50>
cannot identify image file <_io.BytesIO object at 0x7f8e4057bc50>
fileName=<_io.BytesIO object at 0x7f8e4057bc50> identity=[ImageReader@0x7f8e43fd15c0]

请您参考如下方法:

我认为你必须以某种方式将 buff 传递给 ImageReader。

我正在使用这个函数来保存和绘制我用 matplotlib 生成的图形,它非常适合我。

seek(offset, whence=SEEK_SET)将流位置更改为给定的偏移量。行为取决于 whence 参数。 whence 的默认值为 SEEK_SET。

getvalue() 不工作,除了 seek(0)

def save_and_draw(fig, x_img, y_img, width_img=width_img, height_img=height_img):  
    imgdata = BytesIO() 
    fig.savefig(imgdata, format='png') 
    imgdata.seek(0) 
    imgdata = ImageReader(imgdata) 
             
    self.c.drawImage(imgdata, x_img, y_img, width_img, height_img) 
    plt.close(fig)