Nj Subedi
Nj Subedi

Reputation: 317

How do I fill certain percentage by height of an image using ImageMagick commands?

I have an image with around 20% of its bottom filled with white color. But the image has some dots of other colors than white. I have 100s of such images which I would like to remove those dots from.

I have been using ImageMagick commands, and bash scripts to automate several tasks but I cannot find any command to fill certain percentage of an image from bottom by a solid color. Sample image that I need to remove dots from The dots are marked by arrow in the screenshot. A sample command or a hint would be great!

Upvotes: 2

Views: 340

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207670

You can do this in a single step like this:

convert in.png                                                \
   \( +clone -gravity south -crop x10% -evaluate set 100% \)  \
   -composite out.png

Essentially, we read in the image and then do some "aside processing" - a delightful term coined by ImageMagick guru Kurt Pfeifle. The "aside processing" starts with (\ and ends with \). In there, we clone the image (i.e. create a copy of it) and chop off the bottom 10%, which we then fill with white. At the end of the "aside processing", this white image is still in our image stack so we tell ImageMagick to composite that on top of the original.

Result:

enter image description here

Upvotes: 1

Nj Subedi
Nj Subedi

Reputation: 317

I achieved the goal by calculating the height, taking percentage (approximate) of the image's height and filling a white rectangle.

# A tool to fill up 10% of the bottom of given image
# by white color. Useful to remove unnecessary colors
# at the bottom of image.
# Usage: this_script.sh required_image.jpg

#!/bin/bash
image=$1
right=`identify -format %w $image`;
bottom=`identify -format %h $image`;
top=`expr $bottom \* 9 / 10 | bc`;
left=0

convert $image -fill white -draw "rectangle ${left},${top},${right},${bottom}" $image

This can be automated for several images in a folder like:

for img in *.jpg; do bash <script>.sh $img; done

Upvotes: 2

Related Questions