pyfpdf icon indicating copy to clipboard operation
pyfpdf copied to clipboard

Full PIL/Pillow image format support

Open belono opened this issue 7 years ago • 4 comments

Method _parsegif is valid to get image info not only from GIF but all image formats supported by PIL/Pillow.

I've renamed method _parsegif to _parseall and removed file extension checks from method image, except for JPG and PNG, so PIL is capable to identify image formats based on the contents of the file. If a file name is passed to the image function with no extension or one different from JPG or PNG, the file is sent to PIL/Pillow who tries to determine the format and save image to PNG that can be parsed by _parsepng. If image format is invalid then an exception is launched and an error message shown.

Backward compatibility is kept so PIL is only used when image format is different from JPG, PNG or unknown. I've tested this modifications with bmp and ico images with success.

belono avatar Aug 02 '17 22:08 belono

looks good to me. Whats the code that you used to to test it? - so that the tests in the repository can include it. (ps i dont have edit rights to this repository though)

alexanderankin avatar Aug 04 '17 18:08 alexanderankin

Just pass an image path to the pdf.image(name) method, no matter the image format, it will be rendered to the pdf output file:

from fpdf import FPDF

# Path to the image image_path = 'my_image.bmp' # or any format supported by PIL/Pillow

pdf = FPDF('P', 'mm', 'A4') pdf.set_margins(10, 0, 10) pdf.add_page() pdf.image(image_path) pdf.output('test_image.pdf', 'F')

belono avatar Aug 08 '17 22:08 belono

Good to me. Deciding the file type by the extension looks cumbersome.

Marxlp avatar Sep 27 '18 19:09 Marxlp

Note that with this PR PNG files are still not processed by Pillow. Meaning, pyfpdf is not capable of handling interlaced PNGs. This has been addressed in #118.

pennersr avatar Nov 13 '18 13:11 pennersr