Page 1 of 1

how to remove its shadow and get trimmed

Posted: 2013-07-16T23:03:46-07:00
by salahuddinonline
I have multiple images in a directory
I want to remove in any thing except the image shown for Example If image is for a Desktop computer
[img]/home/sarehman/Desktop/Original.jpg[/img]



and I want to remove its shadow,
Background is white
then trim it edge to edge
then resize its canvas as if width is greater in size height will be same as width, height is greater then width will resize as height,
if height is 700 and width is 500 then width will get change in 700 so output we get will be 700x700
[img]/home/sarehman/Desktop/Maximum.jpg[/img]
I want to apply this to whole directory

Re: how to remove its shadow and get trimmed

Posted: 2013-07-16T23:05:21-07:00
by fmw42
Please post your example image so others can see what you are trying to do.

Re: how to remove its shadow and get trimmed

Posted: 2013-07-16T23:25:06-07:00
by salahuddinonline
Ok
I am unable to upload images from my computer so I am giving examples from web

I try to Explain my point

I have image like this
Image
and
when Shadow is removed I want to get result like this
Image
after that
set things as Background = white
then trim it edge to edge
then resize its canvas as if width is greater in size height will be same as width, height is greater then width will resize as height,
if height is 700 and width is 500 then width will get change in 700 so output we get will be 700x700

I want to apply this to whole directory

Re: how to remove its shadow and get trimmed

Posted: 2013-07-17T20:24:49-07:00
by fmw42
This is not an easy problem. I know of no universal technique for getting rid of arbitrary background or shadows, (without knowledge of a 3D model and lighting conditions, which we obviously do not have).

If the background is mostly white, as in your example, one way is to try using a fuzzy floodfill and see if that works.

convert image -fill white -fuzz XX% -draw "color 0,0 floodfill" result

You will have to use trial and error to see what XX% gives acceptable results if at all. But this technique does not work if the background is not a constant color except for the shadow. see http://www.imagemagick.org/Usage/draw/#color

Perhaps some one else might an some ideas, though I suspect it would likely have to be an image-by-image solution (i.e. different for each image)

You can see what IM can do ( though limited to special easy cases or known backgrounds ) at http://www.imagemagick.org/Usage/masking/#bg_remove

It might be better if we could see a few of your actual images to know what various kinds of backgrounds you need removed. You can post a few of your images to any free image hosting service on the web, such as dropbox. Once there, you can post links to those locations so we can download or view those images.

Re: how to remove its shadow and get trimmed

Posted: 2013-07-17T20:45:40-07:00
by snibgo
It's not just a shadow but also a reflection. In the sample image, it merges with the object. Automatic separation would be very difficult and probably unreliable.

Re: how to remove its shadow and get trimmed

Posted: 2013-07-17T21:05:29-07:00
by salahuddinonline
and what about the sizing
after getting my result
I want to trim it then whatever size comes I want to square it with the higher side
for example
if after Trimming image has size of 750x600 then I want both height and width in the same size 750x750
how could I do it ??/

Re: how to remove its shadow and get trimmed

Posted: 2013-07-17T21:14:34-07:00
by fmw42
NOTE CORRECTED CODE BELOW

convert image -fuzz XX% -trim +repage trimmedimage

dim=`convert trimmedimage -format "%[fx:max(w,h)]" info:`

convert trimmedimage -gravity center -background somecolor -extent ${dim}x${dim} squareimage

Re: how to remove its shadow and get trimmed

Posted: 2013-07-17T21:37:29-07:00
by fmw42
This will do the same thing in one command line, but is an advanced technique. see http://www.imagemagick.org/Usage/distor ... red_square


convert yourimage -trim +repage -background somecolor -virtual-pixel background \
-set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:(max(w,h)-w)/2]-%[fx:(max(w,h)-h)/2]" \
-filter point -distort SRT 0 squareimage

Re: how to remove its shadow and get trimmed

Posted: 2013-07-17T23:35:39-07:00
by salahuddinonline
Fred I want to apply this to whole directory not for a single image
Its working fine for single image for I want it to be processed over whole directory

Re: how to remove its shadow and get trimmed

Posted: 2013-07-18T09:41:08-07:00
by fmw42
I am not sure if you can use this command in mogrify. see http://www.imagemagick.org/Usage/basics/#mogrify. It is not as intelligent as convert, but you can try. Create a new directory for the output so that you don't write over the images.

cd imagedirectory

mogrify -path path2newdirectory -trim +repage -background somecolor -virtual-pixel background \
-set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:(max(w,h)-w)/2]-%[fx:(max(w,h)-h)/2]" \
-filter point -distort SRT 0 *

If you want the output images to be jpg add -format jpg after mogrify. If you want to limit the operation to only jpg then at the end replace * with *.jpg

If that does not work, then you need to write a loop over all the images in the image directory and use the convert command. If the above does not work, then let me know and I will give you the loop commands.

Re: how to remove its shadow and get trimmed

Posted: 2013-07-18T19:27:00-07:00
by salahuddinonline
no dear
Its not working
kindly share loop command

Re: how to remove its shadow and get trimmed

Posted: 2013-07-18T19:45:36-07:00
by fmw42
What error message did you get with mogrify?


try this.

Create a new directory to hold the converted images


cd yourimagedirectory
list=`ls`
for img in $list; do
convert $img -trim +repage -background somecolor -virtual-pixel background \
-set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:(max(w,h)-w)/2]-%[fx:(max(w,h)-h)/2]" \
-filter point -distort SRT 0 path2newdirectory/$img
done