Newbie seeking help!

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
vintagepaperads
Posts: 3
Joined: 2019-03-09T11:21:35-07:00
Authentication code: 1152

Newbie seeking help!

Post by vintagepaperads »

I'm fairly new to the ImageMagick environment. I'm hoping someone can help me. I am looking to prepare images to sell on Amazon. The problem is, most of my images are taken on a copy stand, with a gray background. Because of lighting inconsistencies... the background in the image does not end up being a solid gray. Amazon requires pictures to be on a solid white background.

What I would like to do is come up with a batch process using ImageMagick that will:

A) Crop the image as close as possible, to the vintage advertisement.
B) Replace any remaining background with a solid white color.

OR... if that's not possible... crop the advertisement as close as possible, with no background. As an added challenge... I also have some larger photos that are taken at an angle on a table-top. I'd like those to be cropped and if possible transformed to compensate for the angle... making it look more like a straight on shot.

Here's a link to some sample photos:

https://wlu.box.com/v/imagemagick

Any help that could point me in the right direction would be greatly appreciated! Thank you for your assistance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie seeking help!

Post by fmw42 »

With the images you have, I do not know of any process that will remove the background automatically. Too much similar grayshaded in the image to the background and shadows make this very hard. You can draw a bounding box manually about the image to crop it. You can improve the brightness and contrast automatically. You can correct the perspective by picking the four corners of the page and using -distort perspective. Further help can be provided once you provide information below about your IM version and platform.

Please, always provide your IM version and platform when asking questions, since syntax may differ.

Also provide your exact command line and your images, if possible.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

If using Imagemagick 7, then see http://imagemagick.org/script/porting.php#cli


For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown
https://imagemagick.org/script/porting.php#cli
vintagepaperads
Posts: 3
Joined: 2019-03-09T11:21:35-07:00
Authentication code: 1152

Re: Newbie seeking help!

Post by vintagepaperads »

Thanks for your response! I had a feeling this might be a hard task.

I'm on an Apple computer (OSX 10.13.6). I'm using Terminal for the command line. My ImageMagick version is 6.9.9-40.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie seeking help!

Post by fmw42 »

Here are a couple of results from your first image that seem to work. The fuzz value may need adjusting. You can try these on your other images, but some may not work so well.

Input:
Image

The technique is to stretch the image to full dynamic range or even more, then do a fuzzy flood fill from the upper left corner to replace the background with white.

Code: Select all

convert EX0030.JPG -auto-level -fuzz 25% -fill white -draw "color 0,0 floodfill" -alpha off EX0030_result1.jpg
Image

Code: Select all

convert EX0030.JPG -colorspace gray -auto-level -fuzz 25% -fill white -draw "color 0,0 floodfill" -alpha off EX0030_result2.jpg
Image

Code: Select all

convert EX0030.JPG -colorspace gray -contrast-stretch 0,10% -fuzz 25% -fill white -draw "color 0,0 floodfill" -alpha off EX0030_result3.jpg
Image


You might find some of my unix shell scripts useful. See my link below.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie seeking help!

Post by fmw42 »

Here is an example of doing the perspective correction by picking points on the corners of the page and then specifying output rectangular corners equivalent to the width of the bottom of the page and height of right side of the page.

Image

Code: Select all

convert EX1069.JPG -contrast-stretch 0,20% -define distort:viewport=830x446+0+0 +distort perspective "113,42 0,0  780,38 830,0  852,484 830,446  21,476 0,446" EX1069_perspective.jpg
Image

See https://imagemagick.org/Usage/distorts/#perspective
vintagepaperads
Posts: 3
Joined: 2019-03-09T11:21:35-07:00
Authentication code: 1152

Re: Newbie seeking help!

Post by vintagepaperads »

fmw42 thanks for the help! That's definitely getting me on the right track! With the first post you made... once I've replaced the background with white... is there a way to automatically crop closer to remove as much of the white as possible? Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie seeking help!

Post by fmw42 »

vintagepaperads wrote: 2019-03-09T12:34:17-07:00 fmw42 thanks for the help! That's definitely getting me on the right track! With the first post you made... once I've replaced the background with white... is there a way to automatically crop closer to remove as much of the white as possible? Thanks!
Yes, but the white background is not fully white. It has dark tiny spots, which prevent the trim from working. So I will have to employ more complicated methods.

I will try to do so and post back later today.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie seeking help!

Post by fmw42 »

This is one way. I process the image the same saving the image as tmp.png, but add -morphology close diamond:1 afterwards to remove the noise. However, this will cause damage to the image. So I get the trim bounds (cropbox) using the string format "%@", which I save in a variable. Once I have that trim (cropbox) variable, I go back and crop the tmp.png image, which can then be deleted.

Code: Select all

cropbox=$(convert EX0030.JPG -colorspace gray \
-contrast-stretch 0,10% \
-fuzz 25% -fill white -draw "color 0,0 floodfill" -alpha off \
+write tmp.png \
-morphology close diamond:1 \
-threshold 50% \
-format "%@" info:)
convert tmp.png -crop $cropbox EX0030_result5.jpg
Image

See
https://imagemagick.org/Usage/morphology/#close
https://imagemagick.org/script/escape.php
https://imagemagick.org/Usage/basics/#parenthesis
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie seeking help!

Post by fmw42 »

This is one way. I process the image the same saving the image as tmp.png, but add -morphology close diamond:1 afterwards to remove the noise. However, this will cause damage to the image. So I get the trim bounds (cropbox) using the string format "%@", which I save in a variable. Once I have that trim (cropbox) variable, I go back and crop the tmp.png image, which can then be deleted.

Code: Select all

cropbox=$(convert EX0030.JPG -colorspace gray \
-contrast-stretch 0,10% \
-fuzz 25% -fill white -draw "color 0,0 floodfill" -alpha off \
+write tmp.png \
-morphology close diamond:1 \
-threshold 50% \
-format "%@" info:)
convert tmp.png -crop $cropbox EX0030_result5.jpg
Image

See
https://imagemagick.org/Usage/morphology/#close
https://imagemagick.org/script/escape.php
https://imagemagick.org/Usage/basics/#parenthesis
Post Reply