respondcreate / django-versatileimagefield

A drop-in replacement for django's ImageField that provides a flexible, intuitive and easily-extensible interface for quickly creating new images from the one assigned to the field.
http://django-versatileimagefield.readthedocs.io/
MIT License
531 stars 88 forks source link

Smart-crop support #60

Open fabiocaccamo opened 8 years ago

fabiocaccamo commented 8 years ago

It would be very nice to have smart-crop support for thumbnail generation.

There is already a python lib to do that: smartcrop.py (based on smartcrop.js).

fabiocaccamo commented 7 years ago

Here some smart-crop services: http://cloudinary.com/blog/smart_automatic_image_cropping_maybe_you_can_always_get_what_you_want http://thumbor.org/

Any feedback about this issue? It would be appreciated...

respondcreate commented 7 years ago

Hey @fabiocaccamo !

Thanks for passing along these links, I'm a bit slammed this week but I'll try to carve out some time soon to do some investigation. Do you envision smart crop being an additional 'sizer' in addition to the existing crop sizer that uses PPOI to crop {{ instance.image.smart_crop.200x200 }}? Or a boolean setting (i.e. SMART_CROP = True) that the existing crop sizer would use if an image's PPOI was set to the default of 0.5x0.5?

fabiocaccamo commented 7 years ago

Hi @respondcreate, exactly, I imagine an additional sizer that uses a PPOI (determined by an algorithm) to crop.

Thanks for the quick feedback :).

respondcreate commented 7 years ago

Hey @fabiocaccamo –

That simplifies things greatly! OK, I'll carve out some time soon to investigate what it would take the implement this! :)

fabiocaccamo commented 7 years ago

@respondcreate thanks!

drigan commented 6 years ago

Maybe I'm oversimplifying here, but wouldn't it be possible to create a much better crop by combining original image dimensions, the crop function and your thumbnail function?

For example: original image is 2560x1440 We want a 200x150

First, figure out the dimension constraining our crop: width_multiplier = 2560/200 # 12.8 height_multiplier = 1440/150 # 9.6 constraining_factor = min(width_multiplier, height_multiplier) constraining_factor = 9.6

Since height_multiplier is relatively smaller, 9.6 is the constraining factor.

So now we take the constraining factor and find the max size of a crop with the ratio we want:

temp_crop_height = desired_height (200) constraining_factor (9.6) # 1920 temp_crop_width = desired_width (150) constraining_factor (9.6) # 1440

Now we just take a crop of that: temp_image = original_image.crop.1920x1440

And get a thumbnail of that image desired_image =temp_image.thumbnail.200x150

Effectively we take a thumbnail of a crop. I think this would give really good results. Do you think you could do it? Would it work?

This isn't as good as the smart crop stuff, but it would be a really nice compromise.

Weaknesses I see: It takes more image processing. We probably want the original crop to use 100% image quality . . . otherwise we might cause image quality loss. (This isn't my area of expertise, so I'm just guessing)

fabiocaccamo commented 6 years ago

@drigan this issue is not about cropping geometry, but cropping smartly using as center the "image gravity point" (it is different for each image), please check the links above.

drigan commented 6 years ago

Maybe I misunderstood how you intend to use the PPOI, but I was assuming that you would continue to use the PPOI when you did crop for the temp_image. This would result in an image that captures as much of the original image as possible, but doesn't waste bandwidth/loading time.

fabiocaccamo commented 6 years ago

If you check the links below it's pretty clear. The PPOI should be detected automatically by the library based on the image, so it will be different for each image.

fabiocaccamo commented 6 years ago

The objective is to crop images based on "automatically detected PPOI" to provide better thumbnails.

drigan commented 6 years ago

Got it, my apologies, I guess I'm requesting a new feature. Something like crop_thumbnail? I'll start a new thread for that.

respondcreate commented 6 years ago

Hey @drigan !

Is what you're looking to do is crop an image (leaving and then size it down to a specific size? If so, that's exactly what the current crop sizer does, from the docs:

The crop Sizer will first scale an image down to its longest side and then crop/trim inwards, centered on the Primary Point of Interest (PPOI, for short).

drigan commented 6 years ago

@respondcreate Yes it is! But when I use the crop Sizer it's not performing that way: it's cutting parts off the image that should remain. For instance: I take the 2560x1440 image from here:https://pxhere.com/en/photo/1107871 And make a crop of it with .crop.1200x200 it cuts off at the whale shark's pelvic fin rather than continuing to the edge. (It does the same at 1200x400, but interestingly behaves as expected at 1200x500) I thought the cropper was simply finding a natural 1200x200 section centered at the PPOI and using that . . . but now that I'm looking closer at it, I don't know what it's doing.

respondcreate commented 6 years ago

Hey @drigan !

Is there any chance you could share some of the images that you referenced above that were created from the whale shark image (the 1200x200 crop, 1200x400 crop and 1200x500 crop) that showcases how the sizer's behavior differs from your expectations? It's hard to know exactly what's happening and whether or not that's the expected behavior of the sizer or if it's a bug.

fabiocaccamo commented 6 years ago

@drigan invalid image link

drigan commented 6 years ago

@fabiocaccamo you might need to log in first. It's a free site, so don't be afraid to create an account.

drigan commented 6 years ago

@respondcreate There must have been something strange going on my end: I had changed the image to something else. (The site is a test site, and is changing frequently.) After putting the image back to the whale shark image, it acts exactly the way that I would expect it to for all three of those image sizes. I have no idea what changed. :( I'm sorry!

fabiocaccamo commented 6 years ago

@drigan try to click your link...

drigan commented 6 years ago

@fabiocaccamo Weird. I see what you mean. Fixed it.