Git Product home page Git Product logo

Comments (3)

respondcreate avatar respondcreate commented on August 16, 2024

Hey @davidtgq !

This is totally possible to do with a filter! I know you requested this kind of behavior happen without a filter but, if you're up for it, I'd recommend the following:

  1. Write a custom filter that does what you're requesting and be sure to register it with the filter registry.
  2. Add that filter to a repo you control (or in a gist if that's easier) and send me a link so I can review it.
  3. If the filter you write is performant and solves a real need in this app I'd be more than willing to merge it in at a more 'base' level than a filter.

If that filter doesn't merit merging as a piece of core functionality than at least you'd have a working filter you can use on your own projects. Additionally, if you took that filter and wrapped it into a pip-installable app I'd be more than happy to link to it in the docs so others could take advantage of it.

from django-versatileimagefield.

davidtgq avatar davidtgq commented on August 16, 2024

Hi @respondcreate, thanks for the response.

I think I found a typo:
http://django-versatileimagefield.readthedocs.org/en/latest/writing_custom_sizers_and_filters.html#writing-a-custom-filter

All Filters should subclass versatileimagefield.datastructures.filteredimage.FilteredImage and only need to define a process_filter method with following arguments:

Should it be process_image?

Also in the docs it talks about StringIO, but it now uses BytesIO in your code. Is BytesIO more efficient/native? I don't know what these are (my guess is BytesIO is binary and StringIO is Base64), digging through io.py it says BytesIO is a simple stream of in-memory bytes, and the only docs I found is:

This is a fake file object for binary data. In Python 2, it’s an alias for StringIO.StringIO, but in Python 3, it’s an alias for io.BytesIO.

I haven't tried this yet, please let me know if I've got the right idea or if I'm way off, particularly the part where I skipped ImageOps. I'd expect this to only compress the image to the jpeg quality percentage set in the settings:

from versatileimagefield.datastructures.filteredimage import FilteredImage
from versatileimagefield.registry import versatileimagefield_registry
from django.utils.six import BytesIO

class CompressImage(FilteredImage):
    def process_image(self, image, image_format, save_kwargs={}):
        imagefile = BytesIO()
        image.save(
            imagefile,
            **save_kwargs
        )
        return imagefile

versatileimagefield_registry.register_filter('compress', CompressImage)

Just to confirm my assumption, can I put this anywhere and it'll work, such as in any app's models.py?

Further down the docs it talks about preprocessing, it looks like this is where the actual compression takes place, so the filter should really be called a BypassFilter or something.

Is there a post-processor, where something like jpegoptim would plug in, or could that be done in a filter? Just an idea borrowed from here: http://easy-thumbnails.readthedocs.org/en/2.1/ref/optimize/

from django-versatileimagefield.

respondcreate avatar respondcreate commented on August 16, 2024

Hey @davidtgq !

First off, sorry for the extreme delay of my response. Yup, you definitely found a typo there (just fixed it in commit d92a13e)...thanks for pointing that out!

You could put that code in your models.py but, by convention, versatileimagefield looks for new sizers and filters in versatileimagefield.py files at the top level of any app on settings.INSTALLED_APPS (check out the 2nd paragraph under the Registering Sizers and Filters heading here.

Image compression is only for JPEGs (the only file type that understands that as a 0-100 int) that get sized and is set during the preprocessing phase (source link) and is applied to the image directly before saving (in the save_image method here.

Why only for just sized images? Well, I didn't want the image compression to operate on full sized images (as filtered images are) since compressing during a filtering pass and then again on a chained sized operation could lead to overly-compressed/grainy images.

Does that make sense?

from django-versatileimagefield.

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.