I've written an imagecache action that crops based on entropy, which produces more pleasing results when cropping to a fixed aspect (for example it can help prevent cutting of people's heads when cropping a portrait to a square). Simpletests are included.

Would this fit in imagecache_actions? The code can be downloaded here:
http://github.com/dylantack/codename_cornbaby

CommentFileSizeAuthor
#4 imagecache_actions-smartcrop.jpg475.2 KBdman

Comments

grendzy’s picture

BTW if you're at drupalcon this week feel free to look me up :-)

dman’s picture

This sounds like an awesome feature!
What a freaky algorithm (code looks really tidy, I won't try to understand it)
I've got to try this out.

No drupalcon for me this time. Paying jobs happening this month :-]

I'm actually interested in the idea that you can write simpletests for image manipulation. I thought hashing on a binary file would be doomed as a method of comparison in cases of different image compression or toolkit libraries. I've needed a testsuite for my imagecache_actions library for a while, but so far have been pumping out a test sheet for visual comparison instead.

grendzy’s picture

You are right that hashing images for comparison doesn't usually work, you would need a more "fuzzy" metric for image similarity.

In this case, I was able to use hashing to compare cropped images - because there is no interpolation from scaling, and PNGs can be compressed losslessly.

I should also add I can't take credit for the algorithm, it was adapted it from http://code.google.com/p/sorl-thumbnail/

dman’s picture

StatusFileSize
new475.2 KB

Well, I've had a big big play with it.
I'm certainly getting above-average results from it, although it's not quite as magic as I wished to to be.
Running it on images that have the same proportions as the desired result does nothing, because the source just fits directly into the target size. But it does help a tiny bit if cropping a landscape into a square.

I did find that lining up a few optimizations in a row produced an acceptable zoom on the image features though! smartcrop 75%x100% then 100%x75% was pretty good.
It's certainly an improvement - most of the time.
Here's the testsheet I looked at. Made up of a disparate handful of not-very-randomly selected images I had handy. The smartcrop columns were - generally - an improvement on the default or center crop.

I like it.

grendzy’s picture

Cool. Thanks for the thorough test-drive. Please let me know if there's any changes you'd like to the way the code is structured. (The "smartcrop" name can be changed, or folded into canvas actions, etc). I wrote this as a standalone module, before it occurred to me to suggest inclusion here.

Also, I just committed a small change to the git repo adding attribution for the authors of sorl-thumbnail.

dman’s picture

Well, I don't know how far out this is, but I was hoping that I could say "Smartcrop 25%x25%" and get the most interesting quarter of the picture. or 200x200 and get an interesting detail shot.

When I tried that, it didn't really happen like that, which is why I ended up doing the vertical then horizontal slices - which approximates the same result. Is it at all possible to focus on a smaller region like that? Or is it just intended to be a fullsize scale&crop that avoids cropping the important bit?

I think the code works well as a stand-alone, so I'd like to fold it in to imagecache_actions indeed. I've not yet reviewed the code at all, so I'll put it through the code style checker etc first - unless you'd like to?

All it really needs adding is a bit more descriptive help on the settings page. I'd like to know a little more about the process. You've described it a bit on this thread, but I want to capture it in the user docs.

grendzy’s picture

Status: Needs review » Needs work

I hadn't considered that use case, but I like it. I usually get some time on Fridays to work on this.

grendzy’s picture

Status: Needs work » Needs review

Some new changes in github:

  • A second action, that only crops (no scaling, so you can do 25%x25%)
  • A performance tweak that makes the entropy calculation about 80% faster
  • Better tests, I got rid of the hashing and instead compare images based on subtraction. This testing method would probably work for the other imagecache actions, since it tolerates some amount of noise.

Also, if you'd like I'd be happy to co-maintain this module moving forward. Thanks!

grendzy’s picture

ping, wondering if you've had a change to review the new version?

Thanks for all the helpful feedback.

achton’s picture

subscribe!

grendzy’s picture

Status: Needs review » Closed (won't fix)

This issue seemed stalled so smartcrop has it's own home now:
http://drupal.org/project/smartcrop

dman, I hope there's no hard feelings - I do very much appreciate the feedback and testing you contributed!

dman’s picture

Yeah, that's best.
I personally don't have quite enough need or love for it (or additional time) to maintain it under my umbrella, and it runs just fine as a stand-alone feature, so go for it. I'll stick a cross-link on my project page here though, just to support the family of actions.
:-)