如下,我通过ps查到了reportlab制作的pdf(只有文字)的宽高
因此我取一个大概的值,用skimage处理图片,宽高大于这个值则将图片按比例缩小
from skimage import transform, io
from reportlab.platypus import Image
# a是每做完一张图片后就加1的数字,以此避免重名
img1 = io.imread(f'{a}.png')
# img1.shape[1] 是图片的宽, img1.shape[0] 是图片的高
if img1.shape[1] > 1800 or img1.shape[0] > 2500:
new_img = transform.rescale(img1, 1800 / img1.shape[1])
if img1.shape[0] * (1800 / img1.shape[1]) > 2500:
new_img = transform.rescale(img1, 2500 / img1.shape[0])
io.imsave(f'{a}.png', new_img)
else:
io.imsave(f'{a}.png', img1)
# 几次会提示 Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.
img_file = Image(f'{a}.png')
但最后还是会报错:
reportlab.platypus.doctemplate.LayoutError: Flowable <Image at 0x19bba608 frame=normal filename=0.png> too large on page 2 in frame 'normal'(439.27559055118115 x 685.8897637795277*) of template 'Later'