我正在尝试以 PIL 样式缩放图像,以便保持纵横比。看来 openpyxl 图像没有调整大小属性。我收到错误“AttributeError:'Image' 对象没有属性'resize'”。如果我使用 PIL 来调整大小,那么在添加图像“UnboundLocalError: local variable 'rel' referenced before assignment”时会出错。有没有办法重新缩放图像,就像我试图做的那样。
import openpyxl
import PIL
from PIL import ImageGrab
#from PIL import Image
from openpyxl.drawing.image import Image
image_scale=2
column_start=3
row_start=3
def screen_capture():
img=ImageGrab.grab()
img.save('test.jpg','JPEG')
def screen_scaling():
image_scale=3
img = Image('test.jpg')
img = img.resize((int(img.size[0]/image_scale), int(img.size[1]/image_scale)),PIL.Image.ANTIALIAS)
img.save('test.jpg','JPEG')
wb = openpyxl.Workbook()
ws = wb.active
screen_capture()
screen_scaling()
img1=Image('test.jpg')
img1.anchor = ws.cell(row=row_start, column=column_start).coordinate
ws.add_image(img1)
wb.save('Save_test_data.xlsx')
wb.close()