简体   繁体   中英

imagemagick: crop one image to four not equal size parts, but but keep original canvas size and paint background transparent

I have a 1920X1920 ratio image like below: 原始图像

I want to crop this original image so that it is divided into four parts. 在此处输入图像描述 In the end it has the effect of forming four pictures, each of which is the original size. 在此处输入图像描述

I have used such a command, but this intelligence forms two pictures up and down, and cannot form four transparent pictures

convert lena.png -crop 1x2@ +adjoin lena_%02d.miff
for img in *.miff; do
name=$(convert $img -format %t info:)
convert $img -background transparent -flatten $name.png
done
rm -f *.miff

Excuse me, is there a very elegant way to achieve the effect I dream of?

It's hard to answer you properly without actual images and dimensions, but let's try to get you there.

We'll make 4 images and send them to montage to make a 4-image montage, laid out as 2 rows and 2 columns:

magick -size 240x120              \
    xc:red  -write miff:- +delete \
    xc:lime -write miff:- +delete \
    xc:blue -write miff:- +delete \
    xc:black miff:- | magick montage -background yellow -geometry +10+10 miff:- result.png

Hopefully all the different colours and backgrounds will let you see what that is doing.


Now, instead of sending 4 solid blocks of colour, we'll

  • crop out the pieces you want - my numbers will be wrong, but you should see how they work,
  • set the gravity so that they extend in the correct direction,
  • extend them, and
  • send them to a final montage command for assembly - exactly as shown above.

magick YOURIMAGE \
   \( +clone -crop 200x100+10+10   -gravity northwest -background red -extent 300x200 -write miff:- +delete \) \
   \( +clone -crop 200x100+200+10  -gravity northeast -background lime -extent 300x200 -write miff:- +delete \) \
   \( +clone -crop 200x100+10+100  -gravity southwest -background blue -extent 300x200 -write miff:- +delete \) \
             -crop 200x100+200+100 -gravity southeast -background black -extent 300x200 -write miff:- | magick montage ...

Practice cropping out just one section of your image on its own, setting gravity and background and then extending and saving:

magick YOURIMAGE -crop 200x100+20+40 -gravity southeast -background cyan -extend 800x400 result.png

Note: Since the image you're probably working with is not the image you posted, you will need to modify this code to suit your purposes.

#!/bin/bash

width=$(convert lena.png -format "%w" info:)
height=$(convert lena.png -format "%h" info:)

# Get the Percentage of the Width
pct_w() {
  echo $(($1*$width/100))
}

# Get the Percentage of the Height
pct_h() {
  echo $(($1*$height/100))
}

# Top-Left
convert lena.png -crop $(pct_w 33)x$(pct_h 50)+0+0 +adjoin top-left.miff
convert top-left.miff -background transparent -flatten top-left.png

# Top-Right
convert lena.png -crop $(pct_w 66)x$(pct_h 50)+$(pct_w 33)+0 +adjoin top-right.miff
convert top-right.miff -background transparent -flatten top-right.png

# Bottom-Left
convert lena.png -crop $(pct_w 65)x$(pct_h 50)+0+$(pct_h 50) +adjoin bottom-left.miff
convert bottom-left.miff -background transparent -flatten bottom-left.png

# Bottom-Right
convert lena.png -crop $(pct_w 35)x$(pct_h 50)+$(pct_w 65)+$(pct_h 50) +adjoin bottom-right.miff
convert bottom-right.miff -background transparent -flatten bottom-right.png

rm *.miff

There are two helper functions to calculate percentages for Width and Height. In the code $(pct_w 33) this refers to 33% of the width. Change these values accordingly until you get the cuts you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM