save first 209 pixels, remove everthing below

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
dhamlett

save first 209 pixels, remove everthing below

Post by dhamlett »

I think this should be a simple function but I have failed to figure out how to achieve it.

I have multiple images of varying heights but all have the same width. I want to keep the top 209 pixels and delete everything below, regardless of the overall height.

Does this make sense, and can someone help?

thanks....
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: save first 209 pixels, remove everthing below

Post by fmw42 »

Do you mean the top 209 rows of pixels? If you mean 209 pixels, then what do you do with the remaining pixels in the row containing the 209th pixel, if your images are not all 209 pixels wide.

You can always crop to keep rows that are at or above your 209th pixel, but you have to keep any pixels in the row containing the 209th pixel. The only meaningful way of doing this is to make the rest transparent after the 209th pixel.

The best thing would be to crop off all pixels below the row containing the 209th pixel. This should be easy as you have all the same width, so you can compute how many rows you have until you get to the 209th pixel.

Then make a b/w mask image that is the same number of rows and columns as the cropped image, which is white everywhere, but black after the 209th pixel. This mask can then be used for all your images.

This will make an appropriate mask for an image 20x20, for example:

convert -size 20x20 xc: -fx "(j*20+i)<=209?white:black" tmp.png

(but I have not computed where to crop, yet. It is just to show how to use -fx to find the first 209 pixels and make white and the rest make black)


Please clarify about the issues above with more details and I can help further.

Are you keeping only the top 209 rows or only the first 209 pixels starting in the upper left and working along the columns and then rows until you count 209?

What width are all your images?

If all you want to do is crop to keep the top 209 rows, then for each image one-by-one

convert image[Wx209+0+0] result

where W is your actual constant width in each the images.

or for a full directory of files use mogrify with -crop Wx209+0+0 +repage

cd /fullpathtoreaddirectory
mogrify -path /fullpathtosavedirectory -crop Wx209+0+0 +repage *

(this will save the file to a new directory if created ahead of time, so you don't walk over your input images by mistake with an error)

or be more selective about the resulting format, by including -format .jpg (or png or whatever) and the * wildcard can be more specific as well.

see http://www.imagemagick.org/Usage/basics/#mogrify
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: save first 209 pixels, remove everthing below

Post by anthony »

Code: Select all

convert image txt:- | head -n 210  > pixels.txt
If you don't want the header strip the first line too!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: save first 209 pixels, remove everthing below

Post by fmw42 »

anthony wrote:

Code: Select all

convert image txt:- | head -n 210  > pixels.txt
If you don't want the header strip the first line too!

Neat! This works to make the first 209 white and the rest black (could use none for transparency)

convert -size 20x20 xc:white txt:- | head -n 210 | convert -background black - tmp.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: save first 209 pixels, remove everthing below

Post by anthony »

I have a feeling however that the user does not want 209 pixel but 209 rows. Which is a simple -crop.

Fred you do not need to know the width of the image, just use a '0' for the width.

Code: Select all

     convert image   -crop  0x209+0+0  +repage   output
Basically unless dhamlett tells us what he (or she) is wanting it for, we can't help further.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: save first 209 pixels, remove everthing below

Post by fmw42 »

But this does not set the remaining pixels after the 209th to black. Am I doing it wrong?

convert -background black -size 20x20 xc:white -crop 0x209+0+0 +repage tmp.png

I have tried changing the -background position in the command line and replacing it with -fill, but all I get is a totally white image.

What I did above produces a white image for the first 209 pixels and black for all others.

I think he wants rows, too. But I was not sure and I thought this was interesting to see if one could do it for 209 pixels.
Post Reply