how to remove its shadow and get trimmed

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
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

how to remove its shadow and get trimmed

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to remove its shadow and get trimmed

Post by fmw42 »

Please post your example image so others can see what you are trying to do.
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Re: how to remove its shadow and get trimmed

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to remove its shadow and get trimmed

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to remove its shadow and get trimmed

Post 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.
snibgo's IM pages: im.snibgo.com
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Re: how to remove its shadow and get trimmed

Post 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 ??/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to remove its shadow and get trimmed

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to remove its shadow and get trimmed

Post 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
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Re: how to remove its shadow and get trimmed

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to remove its shadow and get trimmed

Post 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.
salahuddinonline
Posts: 24
Joined: 2013-07-14T03:36:12-07:00
Authentication code: 6789

Re: how to remove its shadow and get trimmed

Post by salahuddinonline »

no dear
Its not working
kindly share loop command
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to remove its shadow and get trimmed

Post 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
Post Reply