Giter Site home page Giter Site logo

Comments (2)

Minnozz avatar Minnozz commented on August 26, 2024

Pillow (an existing dependency) supports detecting image types by opening the image and looking at the format property: https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.format

Both places in the code where this functionality is used (that I could find), the purpose is to generate a random filename with an extension that matches the encoding of the image:

try:
image_content, extension = get_image(url)
except: # pylint: disable=bare-except
return None
if not image_content:
return None
image_name = str(uuid4()) + "." + extension

image_content, extension = get_image(url)
if not image_content:
return None
image_name = f"{uuid4()}.{extension}"
return [image_name, image_content]

Using Pillow this way would parse the entire image instead of just the header, so that makes it less efficient.

from bookwyrm.

alcarithemad avatar alcarithemad commented on August 26, 2024

PEP 594 was updated to include recommendations to replace imghdr.

puremagic looks like it should be a suitable replacement.

get_image already reads the entire file, so overhead compared to imghdr should be minimal (puremagic may try to examine a few more kilobytes than imghdr's 32).

from bookwyrm.

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.