Code Monkey home page Code Monkey logo

Comments (4)

mara004 avatar mara004 commented on September 22, 2024 1

@koaning This is actuall a completely different matter. Your error simply means that the Pillow library is not installed (see e.g. #249 (comment) for considerations)

This issue originally was about whether we should check the return of FPDF_BitmapCreate() to be non-null.

from pypdfium2.

mara004 avatar mara004 commented on September 22, 2024

Seems like it's unnecessary - FPDF_BitmapCreate() should always return LP_struct_fpdf_bitmap_t__ (unless errors occured when creating the bitmap). Moreover, FPDFBitmap_Destroy() apparently doesn't fail if the argument is None.

from pypdfium2.

koaning avatar koaning commented on September 22, 2024

I happen to have a situation where this does occur. Figured I'd share.

tests/test_basics.py:8: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
prodigy_pdf/__init__.py:27: in generate_pdf_pages
    "image": page_to_image(page),
prodigy_pdf/__init__.py:13: in page_to_image
    pil_image = page.render().to_pil()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <PdfBitmap uuid:5129eced>

    def to_pil(self):
        """
        Convert the bitmap to a :mod:`PIL` image, using :func:`PIL.Image.frombuffer`.
    
        For ``RGBA``, ``RGBX`` and ``L`` buffers, PIL is supposed to share memory with
        the original bitmap buffer, so changes to the buffer should be reflected in the image, and vice versa.
        Otherwise, PIL will make a copy of the data.
    
        Returns:
            PIL.Image.Image: PIL image (representation or copy of the bitmap buffer).
    
        .. versionchanged:: 4.16 Set ``image.readonly = False`` so that changes to the image are also reflected in the buffer.
        """
    
        # https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.frombuffer
        # https://pillow.readthedocs.io/en/stable/handbook/writing-your-own-image-plugin.html#the-raw-decoder
    
        dest_mode = pdfium_i.BitmapTypeToStrReverse[self.format]
>       image = PIL.Image.frombuffer(
            dest_mode,                  # target color format
            (self.width, self.height),  # size
            self.buffer,                # buffer
            "raw",                      # decoder
            self.mode,                  # input color format
            self.stride,                # bytes per line
            1,                          # orientation (top->bottom)
        )
E       AttributeError: 'NoneType' object has no attribute 'Image'

This is using pypdfium2==4.20.0

from pypdfium2.

mara004 avatar mara004 commented on September 22, 2024

Concerning the original issue, I now think I was wrong by then. For the C functions, it is generally good practice to check the return for an error code (in this case NULL/None), even if it does not usually happen in reality. CC #208.

Note, operations on NULL are dangerous in C. However, pdfium functions generally make sure the input is non-null and return error otherwise. But again, I believe it may be good practice to also do this as a caller.

from pypdfium2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.