Skip to content

Support for PDFs? #324

Open
Open

Description

@1313e

I am actually very surprised that this question exists, given the number of image formats that are supported, but is there any reason why PDFs are not supported as an image format or are there any plans to include this in the future?

Activity

  1. almarklein commented on Mar 23, 2018

    @almarklein
    Member

    I think that this would be very interesting indeed. Do you know what options currently exist to do this from Python?

  2. 1313e commented on Mar 26, 2018

    @1313e
    Author

    Hmmm, I know that matplotlib fully supports PDFs, in that it can save anything in it. Since I always use imageio for working with images, I do not really know of any other packages that can maybe do this. I actually fully expected it to be capable of working with PDFs, given how many formats are supported and how common PDF is.

    I can go and check if there are some options of loading PDFs into Python, if that is what you were asking? It would make my life a lot easier, since I prefer saving my figures in PDF over PNG, since PDFs have lower file sizes and save more details about the picture (and are easier to export).

  3. almarklein commented on Mar 26, 2018

    @almarklein
    Member

    It would be good to distinguish between vector graphics and raster graphics in this discussion. Matplotlib has an internal description of lines, text and other shapes that it can export to formats like eps and pdf, giving small file sizes and crisp images even when zoomed in. It can also export to e.g. PNG, in which case a "snapshot" is made at a certain resolution and dpi. (This generally results in a larger file size, unless your plot contains a lot of points.)

    Imageio is targeted entirely on raster images. You read an image, and get a numpy array that contains the pixels (e.g. NxMx3 for RGB images). Similarly, you can use imageio to save such numpy arrays to a variety of formats.

    So when you store an image with imageio to both png and pdf (when we have a plugin for it), the pdf will likely be larger than the png, because the PDF will simply embed the raster image bitmap.

    The use-case for pfd support in imageio would be to make it easy to e.g. add images to a pdf presentation. And to e.g. read all images contained in a PDF document. In the latter case, its probably only feasable to read the images that are already in raster format, i.e. reading a figure exported by matplotlib will likely not work --- unless we make use of some sort of external program that can "render" the image.

  4. 1313e commented on Mar 28, 2018

    @1313e
    Author

    Hmm, okay, I understand.
    I didn't know about the differences in types of graphics.

    I am using imageio mostly for making movies out of large series of plots that I have created with matplotlib. I prefer to save these plots in PDF, because of the reasons you mentioned (also saving the lines and text, such that you have higher quality images when zoomed in).
    Using that for a movie would be very useful, but I can see how that would become really hard to do.

    Now I am just saving the plots in PNG at much higher dpi, such that I can get a movie with high enough resolution, but this does cost much more hard drive storage space. (And, when you make millions of those plots, it becomes something to think about at some point.)

  5. almarklein commented on Mar 28, 2018

    @almarklein
    Member

    For what its worth, using PNG should generally still be quite efficient, because of the kind of compression that is used is very good at efficiently encoding the large areas of background that most plots have. You could try using JPEG if your plots contain images or if your plots have a lot of content.

    If the movies that you make are meant to be viewed as a movie, you can also consider using ffmpeg to save as mp4. The compression is very good, and also uses the temporal dimension (i.e. it makes use of the fact that two subsequent frames are very similar). Though when pausing such a movie the quality of the viewed frame might be less good.

  6. antoinecarme commented on Mar 29, 2019

    @antoinecarme

    +1

  7. FirefoxMetzger commented on Feb 11, 2022

    @FirefoxMetzger
    Contributor

    As of 2022 we support writing of PDFs (through pillow):

    import imageio.v3 as iio
    
    image = iio.imread("imageio:chelsea.png")
    iio.imwrite("chelsea.pdf", image)

    Reading PDF, however, remains unsupported until someone volunteers to implement it.

  8. almarklein commented on Feb 15, 2022

    @almarklein
    Member

    Reading PDF is also not well defined. E.g. would it read the first image in the document, or would it create a screenshot for each page? I'd be happy to consider this out of scope.

  9. FirefoxMetzger commented on Feb 15, 2022

    @FirefoxMetzger
    Contributor

    I would have defaulted to rastering the page (taking a screenshot) and returning that for each page. I think this is what pillow does when you open an EPS or PS file and seems like a natural extension of that.

    Extracting embedded pictures from PDF is a frequent task as well though so I do see your point. Either way, we can always postpone making a definite decision until there is an actual use-case for reading PDFs and then evaluate if the use-case fits in ImageIOs scope and if we can support it.

  10. sweiss435 commented on Oct 31, 2023

    @sweiss435

    Hello, I was asked to fix an api that used PDDocument to separate pdf pages, then BufferedImage's, and then is suppose to create individual page pdfs. However, it is using ImageIO and can only create pngs or jpegs. New front ends that consume this I'm working on want pdfs. It sounds like you are saying there is no use case for ImageIO to create pdfs. There absolutely is, everyone wants and uses pdfs. If ImageIO is going to remain oncomplete can you refer me to a tool that can create pdfs?

  11. almarklein commented on Nov 1, 2023

    @almarklein
    Member

    We meant that we were not aware of an actual use-case of someone needing this functionality. And since it's not well-defined what should happen (e.g. reading pages or extracting images, or maybe we can support both?) we held off the implementation.

    It sounds like you do have an actual use-case. You are welcome to submit a PR to add this functionality to imageio 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions