Since I'm a CS theory person, I can offer some theoretical improvements on the running time and make the problem even more general...<p>Instead of minimize the linear difference of partition, we might want to minimize the standard deviation, or basically any convex function, and still do it in the same time bound.<p>One can reduce this problem to find a k-edge path of minimum weight on a complete DAG. The naive algorithm will run in O(kn^2), but we can improve the running time to O(kn) by realize the weight on this DAG has the Monge property. This is very practical to implement.<p>I posed it as a problem on daily haskell exercise <a href="http://dailyhaskellexercise.tumblr.com/post/58060450750/the-minimum-weight-path-of-length-k-in-a-dag-with" rel="nofollow">http://dailyhaskellexercise.tumblr.com/post/58060450750/the-...</a>.<p>In this application, k is very large. n is just a constant multiple of k. We can use a <i>theoretically</i> better algorithm that takes n2^O(sqrt(log n loglog n)) time. (this is almost O(n^(3/2))). I doubt it will ever be implemented with speed comparible to the O(kn) solution. See <a href="http://www.cs.ust.hk/mjg_lib/bibs/DPSu/DPSu.Files/sdarticle_204.pdf" rel="nofollow">http://www.cs.ust.hk/mjg_lib/bibs/DPSu/DPSu.Files/sdarticle_...</a><p>I shall post a solution tomorrow since I'm currently touring NYC with my gf...
WordPress recently added 'Tiled' galleries via their JetPack plugin. It seems like a similar approach. You can see an example gallery here:<p><a href="http://ma.tt/2013/07/jay-z-picasso/" rel="nofollow">http://ma.tt/2013/07/jay-z-picasso/</a><p>Additional info:
<a href="http://jetpack.me/support/tiled-galleries/" rel="nofollow">http://jetpack.me/support/tiled-galleries/</a><p>Code:
<a href="https://github.com/crowdfavorite-mirrors/wp-jetpack/tree/master/modules/tiled-gallery" rel="nofollow">https://github.com/crowdfavorite-mirrors/wp-jetpack/tree/mas...</a>
It like how it looks, but much like Flickr I dislike how it's biased towards landscape pictures. Some of my best photos are portrait, but they get overshadowed by the landscape ones.<p>I imagine the algorithm to cater to both is much harder, because you wouldn't be able to treat each row in isolation, can it actually be solved to a good standard?
Side-topic. Shooting Holi [1] without putting your camera in an airtight sealed enclosure is a good way to ruin your equipment.<p><a href="http://en.wikipedia.org/wiki/Holi" rel="nofollow">http://en.wikipedia.org/wiki/Holi</a><p><a href="http://www.lensrentals.com/blog/2013/05/how-to-ruin-your-gear-in-5-minutes-without-water" rel="nofollow">http://www.lensrentals.com/blog/2013/05/how-to-ruin-your-gea...</a>
I just implemented pretty much the same thing from scratch for my wedding gallery¹.<p>For each row, it tries 3-10 images, sums their aspect ratios, divides the total row width by the sum to get a candidate height, then picks the height that's closest to the average of the existing rows.<p>To make things look a bit nicer, it rejects candidate rows with the same number of images as the last row.<p>I might release the code on Github if I can make it modular enough. Currently it depends on jQuery but that's really only for element creation.<p>¹ <a href="http://mattandsophiegetmarried.com" rel="nofollow">http://mattandsophiegetmarried.com</a>, I'm the guy with the waistcoat, purple cravat and massive grin.
What's just as impressive is the full size view of the image. The background brings the ambient colour of the photo to make it more immersive. Very nice!
It looks nice (much like Google Image results) but IMO it's not "equally distributed". For example, an image that has dimensions 800x531 gets ~2.3x the space as one with dimensions 531x800. That arbitrarily incentivizes/rewards landscape photos w.r.t. portraits. "Equally-distributed" would probably require an algorithm that works mosaically (without row constraints) and couldn't achieve "taking up all the space available [in a rectangular region]".<p>Still, looks nice.
Maybe I'm missing the point but why is this an achievement? I've seen Google images do this for years (from the backend, sends the users viewport dimensions and then automatically calculates the optimum filling). For years (Warning NSFW!) vusker.com has been doing this client-side in their thumbnail and gallery view. Plus look at the vertically stacked posts from Pinterest.<p>It seems more a marketing ploy to get attention to the great service chromatic.io is providing?
he writes an excellent example of why "all that computer sciency stuff" is usefull in day to day programming, yet still wants to take pride in his ignorance, like this is some strange sort of fluke.
The demo [0] is pretty impressive. Photo sites like Instagram and 500px give a fixed ratio, while flikr does nothing quite appealing as this. Nice work.<p>[0] <a href="http://www.chromatic.io/FQrLQsb" rel="nofollow">http://www.chromatic.io/FQrLQsb</a>
This works well when the photos are not time sequential. I find it harder to follow the story when the photos are ordered by the partition fit instead of the time taken. It would be neat to optionally incorporate time into the algorithm.
This looks like a pretty slick, mobile-compatible photo gallery, but without the code it isn't very useful. They talk about Chromatic like a real product, but "free web service" basically means "demo".
"Remember the days in college when you learned all about the big Oh!'s and re-implemented all these sort-algorithms for the hundredth time? If you are a web developer like me, chances are you never had to touch a single one of these algorithms ever again." +1<p>It's a good start, but when the window is shrunk, the result isn't as impressive.
This is why I always preferred vertical masonry. Sites such as VKontakte, Google Image Search, and the recent Flickr app tile things horizontally, but this sometimes means you have to crop the images to fit into your masonry. Not so with vertical masonry, which you can just resize to have constant width.
Why not use Masonry or Isotope?<p><a href="http://masonry.desandro.com/" rel="nofollow">http://masonry.desandro.com/</a><p><a href="http://isotope.metafizzy.co/" rel="nofollow">http://isotope.metafizzy.co/</a>
Given that clear textbook explanation and he decided to port a Python implementation instead of writing it from scratch? What a waste of an opportunity for a fun day at work...
It is hard not to mention series of articles [0] on the same topic by Christopher 'vjeux' Chedeau [1] in this context.<p>[1] <a href="http://blog.vjeux.com/?s=%22Image+Layout+Algorithm%22" rel="nofollow">http://blog.vjeux.com/?s=%22Image+Layout+Algorithm%22</a>
[0] <a href="https://news.ycombinator.com/user?id=vjeux" rel="nofollow">https://news.ycombinator.com/user?id=vjeux</a>
I too made something similar recently. I decided to use dynamic programming. This way it's easy to ensure that you don't end up with a half row at the end. It also doesn't require changes to the ordering of the images.
<a href="http://fangel.github.io/packing-images-in-a-grid/" rel="nofollow">http://fangel.github.io/packing-images-in-a-grid/</a>
Great post with gorgeous results. The service is really very nice too! Fantastic work.<p>Question: will you ever implement profiles or permanent galleries?
I made something similar a while back to generate backgrounds on a music website. I took a different approach, and it's not as clean. If anyone is interested to see a different attempt, take a look at the JS.<p><a href="http://wwwstu.tcu.edu/baerbradford/tiles.html" rel="nofollow">http://wwwstu.tcu.edu/baerbradford/tiles.html</a>
Interestingly when I load up their demo gallery at: <a href="http://www.chromatic.io/FQrLQsb" rel="nofollow">http://www.chromatic.io/FQrLQsb</a> and click on one of the images to enlarge it, then click again to drop back into the gallery view, the gallery breaks horribly (about 1/3 of the images vanish).<p>FF 22 on Windows 8
I've studied this a great deal, and developed a custom masonry type layout to mitigate the row/column bias. I believe it is a much more balanced layout than the article shows. Using it in Imagist, a paid app. Open to a few beta testers for iOS 7 as well, if you want to judge for yourself.
Since when it is ok to suppose visitors know to click the escape key to go back. And where is the link to go up to the gallery. I had to mess with the url or I was gone for a long back button session. Morevoer it is very slow.<p>Just to add some negativeness to the generaly positive comments here.
I really like this, made a quick one with snaps from Angkor Wat in Cambodia: <a href="http://www.chromatic.io/lTOfsdP" rel="nofollow">http://www.chromatic.io/lTOfsdP</a>
Can sombody tell me why this uses the photos as background images for divs? It seems to me like this gallery could also use img tags (which is semantic, crawlable, yadda..)
Anyone else in this thread think that this blog post covers a painfully trivial calculation problem? What's next - someone's going to write a full blog post to explain to me how to perfectly toast my toast using these visual sensor things that seem to be built into my head (eyes)?
i made a library for this:<p><a href="https://github.com/jonathanong/horizontal-grid-packing" rel="nofollow">https://github.com/jonathanong/horizontal-grid-packing</a>