How do I crop an image from a blank area

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
disburden
Posts: 2
Joined: 2019-08-08T11:12:46-07:00
Authentication code: 1152

How do I crop an image from a blank area

Post by disburden »

Image

In A,The shaded portion of the picture represents an image,the blue dotted box indicates the area I want to crop.
I want a picture like B,the white space is transparent,But I can only get a picture of what C looks like,image pixels are always filled from the top.

The command I'm using looks something like this:
convert 32.jpg -crop 356x768+436+263 -background transparent -extent 356x768 output.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I crop an image from a blank area

Post by snibgo »

I think you want the output to contain part of the input, plus some extra pixels that are transparent. You have a positive y-offset "+263" but your diagrams suggest you want a negative offset. I would use virtual pixels and a viewport, like this:

Code: Select all

magick in.png -set option:distort:viewport 400x400+100-100 -virtual-pixel None -distort SRT 1,0 +repage out.png
snibgo's IM pages: im.snibgo.com
disburden
Posts: 2
Joined: 2019-08-08T11:12:46-07:00
Authentication code: 1152

Re: How do I crop an image from a blank area

Post by disburden »

Yeah, I'm using a negative y-offset,posted in a hurry,so a random copy of the previous test command.
I tried your command and it worked fine.Thank you very much!!

I would like to ask you another question.
I use the following command to round the corners of the picture.

Code: Select all

convert cut3.png -alpha set -virtual-pixel transparent -channel A -blur 0x32  -threshold 50% +channel cut4.png
If the picture is like C, i hope that only the upper left corner is rounded, but now all the four corners of the shaded part will be rounded.
How to don't ignore alpha channel when rounding corners?
Thanks again!
(In fact, I don't really understand those parameters,catch up with project schedule,you know...)
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How do I crop an image from a blank area

Post by GeeMack »

disburden wrote: 2019-08-08T11:19:44-07:00In A,The shaded portion of the picture represents an image,the blue dotted box indicates the area I want to crop.
I want a picture like B,the white space is transparent,But I can only get a picture of what C looks like,image pixels are always filled from the top.
A great method was provided already by snibgo. You can also use "-extent" to get essentially the same result. You can cut out a portion of an input image and place it in a particular location on a certain size transparent canvas with a command like this...

Code: Select all

convert input.png -background none -extent 720x720+320-90 result.png
The geometry for the "-extent" operation first defines a width and height for the output. Then it sets the horizontal distance from the left edge to crop the input image like "+320", and the vertial distance from the top to crop the input image, like "-90". You can use negative numbers to specifiy the geometry of the crop.
Post Reply