Trim whitespace from top and bottom of image and distort

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
tomchambers
Posts: 5
Joined: 2019-03-31T11:26:29-07:00
Authentication code: 1152

Trim whitespace from top and bottom of image and distort

Post by tomchambers »

What I have - image with whitespace at top and bottom
Image

What I want - image with no whitespace and content is distorted to fit space. This was done in photoshop
Image

My method for achieving this is
1. Crop image into 1x256px slices
2. Trim the whitespace/transparent pixels from top and bottom
3. Resize the image to be 1x256 so the content is stretched
4. Montage

Code: Select all

convert original.png +repage -crop 1x -trim -resize 1x256\! out.png
montage out-*.png -geometry 1x -tile x1 montage.png
Seems to be working ok, but I have a bunch of odd artefacts at regular intervals through the image. Can't work out whether this is a problem with the montaging or the cropping!

Actual imagemagick image
Image
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Trim whitespace from top and bottom of image and distort

Post by GeeMack »

tomchambers wrote: 2019-03-31T11:38:47-07:00

Code: Select all

convert original.png +repage -crop 1x -trim -resize 1x256\! out.png
montage out-*.png -geometry 1x -tile x1 montage.png
Seems to be working ok, but I have a bunch of odd artefacts at regular intervals through the image. Can't work out whether this is a problem with the montaging or the cropping!
I don't know about artifacts. You might have to post a close up or point out a specific area that shows the problem. But I would suggest you run that as a single command. Just "+append" the cropped and resized strips like this...

Code: Select all

convert original.png +repage -crop 1x -trim -resize 1x256\! +append out.png
The result "out.png" should be your final image.
tomchambers
Posts: 5
Joined: 2019-03-31T11:26:29-07:00
Authentication code: 1152

Re: Trim whitespace from top and bottom of image and distort

Post by tomchambers »

The artefacts I mean are the 1px columns that appear every 20px or so and don't fit in the sequence.

But +append has solved it, thank you! I have the full image now.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim whitespace from top and bottom of image and distort

Post by fmw42 »

The default separation for montage is 2 pixels so use -geometry +0+0 to make them align. But append is probably a better option.
Post Reply