Detect background color and create square image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
crc863
Posts: 12
Joined: 2013-11-21T10:06:21-07:00
Authentication code: 6789

Detect background color and create square image

Post by crc863 »

Hi everyone!

I may be asking too much here but let me explain what I'm trying to do.

1.) User uploads photo
2.) Detect if the image has a solid background (most likely a logo) or if not (most likely a photograph)
3.) If it's a logo with a solid background, determine the background color
4.) if the image is not a perfect square, and a "logo" make the image a square using the script below with the determined background color.

Code: Select all

<?php
exec('convert '.ROOT.UPLOADS_IMGS.'2014/12/18/9360011418937327.jpg \
        \( +clone -rotate 90 +clone -mosaic +level-colors white \) \
         +swap -gravity center -composite '.ROOT.UPLOADS_IMGS.'2014/12/18/9360011418937327-bg.jpg');
?>
Is this possible with imagemagick?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Detect background color and create square image

Post by snibgo »

crc863 wrote:2.) Detect if the image has a solid background (most likely a logo) or if not (most likely a photograph)
If the input is JPG, it won't have a solid background. It might have a nearly solid background. You might detect this by flood-filling from (0,0) with a fuzz factor, and counting the proportion of pixels that have changed, or seeing if all border pixels have changed, or some other test.
snibgo's IM pages: im.snibgo.com
Post Reply