python FPDF大小调整不正确

python FPDF大小调整不正确,python,fpdf,Python,Fpdf,我正在从一个目录中获取一个图像列表,我正在尝试将一个图像列表转换为PDF。我得到他们的宽度和高度,并使用图像模块。当程序运行时,我打开PDF文件,图片看起来非常大,只有图片的一角 from fpdf import FPDF from PIL import Image import glob import os image_directory = '/Users/myuser/pics/' extensions = ('*.jpg','*.png','*.gif') pdf = FPDF() i

我正在从一个目录中获取一个图像列表,我正在尝试将一个图像列表转换为PDF。我得到他们的宽度和高度,并使用图像模块。当程序运行时,我打开PDF文件,图片看起来非常大,只有图片的一角

from fpdf import FPDF
from PIL import Image
import glob
import os

image_directory = '/Users/myuser/pics/'
extensions = ('*.jpg','*.png','*.gif')
pdf = FPDF()
imagelist=[]
for ext in extensions:
    imagelist.extend(glob.glob(os.path.join(image_directory,ext)))

for imageFile in imagelist:
    cover = Image.open(imageFile)
    width, height = cover.size
    pdf.add_page()
    # 1 px = 0.264583 mm (FPDF default is mm)
    pdf.image(imageFile, 0, 0, float(width * 0.264583), float(height * 0.264583))
pdf.output(image_directory + "file.pdf", "F")
图像是左边的,右边是PDF
我认为问题在于图像大小超过了pdf大小(默认为A4),纵向为210mm x 297mm,横向为反向。您应该检查并根据需要调整大小。您还可以根据页面的高度和宽度设置页面方向

from fpdf import FPDF
from PIL import Image
import glob
import os

image_directory = '/Users/myuser/pics/'
extensions = ('*.jpg','*.png','*.gif')
pdf = FPDF()
imagelist=[]
for ext in extensions:
imagelist.extend(glob.glob(os.path.join(image_directory,ext)))

for imageFile in imagelist:
    cover = Image.open(imageFile)
    width, height = cover.size

    # convert pixel in mm with 1px=0.264583 mm
    width, height = float(width * 0.264583), float(height * 0.264583)

    # given we are working with A4 format size 
    pdf_size = {'P': {'w': 210, 'h': 297}, 'L': {'w': 297, 'h': 210}}

    # get page orientation from image size 
    orientation = 'P' if width < height else 'L'

    #  make sure image size is not greater than the pdf format size
    width = width if width < pdf_size[orientation]['w'] else pdf_size[orientation]['w']
    height = height if height < pdf_size[orientation]['h'] else pdf_size[orientation]['h']

    pdf.add_page(orientation=orientation)

    pdf.image(imageFile, 0, 0, width, height)
pdf.output(image_directory + "file.pdf", "F")
从fpdf导入fpdf
从PIL导入图像
导入glob
导入操作系统
image_directory='/Users/myuser/pics/'
扩展=('*.jpg','*.png','*.gif')
pdf=FPDF()
imagelist=[]
对于ext-in扩展:
extend(glob.glob(os.path.join(image\u目录,ext)))
对于imagelist中的imageFile:
封面=图像。打开(图像文件)
宽度、高度=封面尺寸
#以毫米为单位转换像素,1px=0.264583毫米
宽度,高度=浮动(宽度*0.264583),浮动(高度*0.264583)
#鉴于我们正在使用A4格式的尺寸
pdf_size={'P':{'w':210,'h':297},'L':{'w':297,'h':210}
#从图像大小获取页面方向
方向='P'如果宽度<高度,则为'L'
#确保图像大小不大于pdf格式的大小
宽度=宽度,如果宽度
我认为问题在于图像大小超过了pdf大小(默认为A4),纵向为210mm x 297mm,横向为反向。您应该检查并根据需要调整大小。您还可以根据页面的高度和宽度设置页面方向

from fpdf import FPDF
from PIL import Image
import glob
import os

image_directory = '/Users/myuser/pics/'
extensions = ('*.jpg','*.png','*.gif')
pdf = FPDF()
imagelist=[]
for ext in extensions:
imagelist.extend(glob.glob(os.path.join(image_directory,ext)))

for imageFile in imagelist:
    cover = Image.open(imageFile)
    width, height = cover.size

    # convert pixel in mm with 1px=0.264583 mm
    width, height = float(width * 0.264583), float(height * 0.264583)

    # given we are working with A4 format size 
    pdf_size = {'P': {'w': 210, 'h': 297}, 'L': {'w': 297, 'h': 210}}

    # get page orientation from image size 
    orientation = 'P' if width < height else 'L'

    #  make sure image size is not greater than the pdf format size
    width = width if width < pdf_size[orientation]['w'] else pdf_size[orientation]['w']
    height = height if height < pdf_size[orientation]['h'] else pdf_size[orientation]['h']

    pdf.add_page(orientation=orientation)

    pdf.image(imageFile, 0, 0, width, height)
pdf.output(image_directory + "file.pdf", "F")
从fpdf导入fpdf
从PIL导入图像
导入glob
导入操作系统
image_directory='/Users/myuser/pics/'
扩展=('*.jpg','*.png','*.gif')
pdf=FPDF()
imagelist=[]
对于ext-in扩展:
extend(glob.glob(os.path.join(image\u目录,ext)))
对于imagelist中的imageFile:
封面=图像。打开(图像文件)
宽度、高度=封面尺寸
#以毫米为单位转换像素,1px=0.264583毫米
宽度,高度=浮动(宽度*0.264583),浮动(高度*0.264583)
#鉴于我们正在使用A4格式的尺寸
pdf_size={'P':{'w':210,'h':297},'L':{'w':297,'h':210}
#从图像大小获取页面方向
方向='P'如果宽度<高度,则为'L'
#确保图像大小不大于pdf格式的大小
宽度=宽度,如果宽度
我认为
cover.size
将返回像素大小,FPDF默认大小为
mm
。FPDF的可接受单位为
pt
cm
in
。您可以将pdf中的
cover.size
转换为
pt
cm
。在我转换为mm后,现在图片缺少图片的四分之一。我想这也取决于显示器的大小。试着纠正这个问题。我更新了这个问题。现在,图片大小正确,但盒子容器的大小与打印机页面大小相同。您知道如何将每个容器更改为与图片大小相同的大小吗?您可以通过将A片段传递给FPDF来设置它,如
FPDF('P','mm',(100150))
在纵向模式下将其设置为100X150 mm我想
封面。size
将返回像素大小,FPDF默认大小为
mm
。FPDF的可接受单位为
pt
cm
in
。您可以将pdf中的
cover.size
转换为
pt
cm
。在我转换为mm后,现在图片缺少图片的四分之一。我想这也取决于显示器的大小。试着纠正这个问题。我更新了这个问题。现在,图片大小正确,但盒子容器的大小与打印机页面大小相同。您知道如何将每个容器更改为与图片大小相同的大小吗?您可以通过向FPDF传递一个片段来设置它,如
FPDF('P','mm',(100150))
在纵向模式下将其设置为100X150 mm。您只需为图像设置一个以mm为单位的宽度或高度,即可轻松解决此问题。FPDF将自动为您调整大小,保持纵横比。通过这种方式,您可以使用pdf将其设置为页面的宽度。w如果您希望它填充整个页面而不是所有页面,您可以通过为图像设置以mm为单位的宽度或高度来轻松解决此问题。FPDF将自动为您调整大小,保持纵横比。这样,如果你想让它填满整个页面,你可以用pdf.w将它设置为页面的宽度