ValueError: need more than 3 values to unpack
发生错误的原因可能是
img.split()
unpacking
到4个变量,
r g b a
. 检查长度
img.split()
相应地返回并分配给变量,或者使用索引来访问前3个元素,如
image_parts = img.split()
r = image_parts[0]
g = image_parts[1]
b = image_parts[2]
更新:
from PIL import Image
import xlsxwriter
from io import BytesIO
workbook = xlsxwriter.Workbook('filename.xlsx')
worksheet1 = workbook.add_worksheet()
img = Image.open("/home/username/folder/custom-addons/my_module/static/description/logo.png")
image_parts = img.split()
r = image_parts[0]
g = image_parts[1]
b = image_parts[2]
img = Image.merge("RGB", (r, g, b))
fo = BytesIO()
img.save(fo, format='bmp')
worksheet1.insert_image('A1', 'python.png', {'image_data': fi})
workbook.close()
好的,我在中找到了必需的函数
xlwt
库的源代码。
from PIL import Image
import xlwt
from io import BytesIO
workbook = xlwt.Workbook()
worksheet1 = workbook.add_sheet('Test')
img = Image.open("/home/username/folder/custom-addons/my_module/static/description/logo.png")
image_parts = img.split()
r = image_parts[0]
g = image_parts[1]
b = image_parts[2]
img = Image.merge("RGB", (r, g, b))
fo = BytesIO()
img.save(fo, format='bmp')
worksheet1.insert_bitmap_data(fo.getvalue(),0,0)
workbook.save('filename.xls')
img.close()