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

cha*_*dmc 2 python django file reportlab

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

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

这是如何使用 BytesIO 作为如何使用 ReportLab 从原始字节绘制图像的输入的示例ImageReader()

此类用于绘制绘图并通过BytesIO(). 字符串是我稍后要传递的值

#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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

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

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()
Run Code Online (Sandbox Code Playgroud)

这是发生错误的地方,在 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()
Run Code Online (Sandbox Code Playgroud)

对于 Error1,我收到:

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

对于 Error2 我收到

OSError: 无法识别图像文件 <_io.BytesIO 对象在 0x7f8e4057bc50>
无法识别图像文件 <_io.BytesIO 对象在 0x7f8e4057bc50>
fileName=<_io.BytesIO 对象在 0x7f8e4057bc50> identity=[ImageReader@40x]

Mur*_*nio 6

我认为你必须buffImageReader某种方式传递给。

我正在使用此功能来保存和绘制我生成的图形matplotlib,它非常适合我。

seek(offset, whence=SEEK_SET)将流位置更改为给定的偏移量。行为取决于 whence 参数。wherece 的默认值是 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)
Run Code Online (Sandbox Code Playgroud)