How to split an image with a grid and preserve transparency bounding box - image-processing

I have some png images that I want to split it into parts, like by grid or size.
But each part should have the same bounding box (transparency) as original image.
Example:
Splitting image into 2 parts.
Original: 200 × 89
Output:
part_1.png, 200 × 89
part_2.png, 200 × 89
Can ImageMagick do this? Or any other app or method.
My actual goal is to split into 100+ slices images.
EDIT:
Another goal to have an indents for each slice. Say indent = 10px.
Example:
Input: 200 x 100
Output:
part_1.png, 200 x 100
part_2.png, 200 x 100
And just as example, to visually compare input and output: combined output images in Photoshop as layer added one onto another
200 x 100 :
Also this is showing input image added onto combined(so it's better to see what was cropped and how):

In ImageMagick, you can split an image into many parts with the -crop command. For your example above with two parts, you can do that with the following commands. ImageMagick will append -0, -1 ... to the output file names.
ImageMagick 6:
dim=`convert image.png -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( image.png -crop 50x100% \) -layers composite result.png
ImageMagick 7:
magick \( image.png -set option:dim "%wx%h" -crop 50x100% \) null: \( -size "%[dim]" xc:none \) -reverse -layers composite result.png
The results are:
See
http://www.imagemagick.org/Usage/crop/#crop
http://www.imagemagick.org/Usage/crop/#crop_percent
http://www.imagemagick.org/Usage/crop/#crop_tile
http://www.imagemagick.org/Usage/crop/#crop_quad
http://www.imagemagick.org/Usage/crop/#crop_equal
http://www.imagemagick.org/script/command-line-options.php#layers
Note that -crop keeps the virtual canvas information if you do not add +repage afterwards. So to put the individual images back into their original placement, you have to composite them onto a transparent background the size of the input. That is done in one command using -layers composite using the null: separator.

Here is another way to add transparent areas between parts of a crop in ImageMagick. Crop the image into pieces, chop off the parts you want to remove, then pipe to montage to add the spacing back.
Input:
Here I make this into a 4x4 grid of images with 10 pixel spacing:
convert lena.png -crop 25%x25% +repage -gravity east -chop 10x0 -gravity south -chop 0x10 +repage miff:- | montage - -background none -tile 4x4 -geometry +5+5 result.png

To answer your new question, you can do that with a script loop. On a Unix-like platform, assuming your images do not have spaces, you can do the following:
cd path/to/current_folder
list=`ls *.png`
for img in $list; do
name=`convert $img -format "%t" info:`
dim=`convert $img -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( $img -crop 50x100% \) -layers composite -scene 1 path/to/new_folder/${name}_%d.png
done
If you want leading 0s in the output, say 3, use path/to/new_folder/${name}_%03d.png.
Note that to start with 1 rather than 0, I have added -scene 1.
Sorry, I do not know how to script for Windows.
Please always provide your ImageMagick version and platform.

In ImageMagick, the best way to put transparent areas into your image is with a binary mask that is put into the alpha channel of your image.
convert input.png \( -size 200x89 xc:white -size 10x89 xc:black -gravity center -composite \) -alpha off -compose copy_opacity -composite result.png
You can add as many blank areas as you want by adding more white areas to the mask or by tiling out one region of black and one region of white to create the mask with regular spacing of black and white.

Edited to add this ImageMagick 6 example which splits the input image into 4 pieces, 25% of the original width and 100% of its height, then creates a transparent canvas for each piece the same dimensions of the input image, and locates the pieces at their original offsets on those canvases.
convert input.png -set option:distort:viewport %[w]x%[h] -crop 25x100% \
-virtual-pixel none -distort affine "0,0 %[fx:s.page.x],%[fx:s.page.y]" out%03d.png
The output file names will be numbered starting from zero like "out000.png", etc.
Original message...
Here's a simple command using ImageMagick 7 that can crop an image into any number of pieces, and output all the pieces at their original offsets on transparent backgrounds of the original input dimensions...
magick input.png -crop 100x1# -background none \
-extent "%[fx:s.page.width]x%[fx:s.page.height]-%[fx:s.page.x]-%[fx:s.page.y]" out%03d.png
That "-crop 100x1#" tells it to split the image into a grid 100 pieces wide by 1 piece high. You could just as well specify the crop sizes as percents or numbers of pixels.
Edited again to add:
This following command will split the input image into the individual pieces specified with the "-crop" operator, then shave 5 pixels from every side of each piece, then apply a 5 pixel transparent border to every side of each piece. It will still remember the original locations of the pieces within the input canvas, so the "-distort affine ..." can extend the canvases and place the pieces where they were in the input image.
convert input.png -set option:distort:viewport %[w]x%[h] \
-bordercolor none -background none -virtual-pixel none \
-crop 25x100% -shave 5x5 -border 5x5 \
-distort affine "0,0 %[fx:s.page.x],%[fx:s.page.y]" out%03d.png
To use this command with IM7 you need to change "convert" to "magick".

Given the changes of requirements provided by Kamikaze, here is one way to achieve the split with indent in ImageMagick, assuming I understand correctly.
dim=`convert image.png -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( image.png -crop 50x100% -shave 5x5 \) -geometry +5+5 -layers composite result.png
To check, I flatten over a blue background:
convert result-0.png result-1.png -background blue -flatten result.png

Related

Overlay PNGs with ImageMagick while keeping transparency

I have two images:
Image 1
In this image, the white region plus the white + pink region are transparent.
Image 2
GOAL
I want to merge both images (Image 1 in front, Image 2 behind) by:
Keeping the transparent region from Image 1 so that Image 2 can be
seen through the white mask.
Having the chance to locate Image 2 by vertically centering the photo in the middle of the white region.
Then, I'd like to obtain a result like this:
HOWEVER
I am using the following command in ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 in Ubuntu 16.04:
convert \( Image1.png -resize 447x640 \) \( -compose Overlay Image2.png \) -gravity north -composite Image3.png
I've tried countless times but the best result I can get (by using command above) is Image 3. Can anyone help me? Thank you.
Image 3
I think this is what you want using Imagemagick in Unix syntax:
Img:
Mask:
convert \( mask.png -alpha off \) img.jpg \( mask.png -alpha extract -negate \) -compose over -composite result.png
or more simply:
convert mask.png img.jpg -compose dstover -composite result.png

How to find out the average color (rgb) of masked area of an Image

I would like to get the average color in rgb of the mask area of a png image.
the following just outputs the average color of the whole image
convert demo.png -mask demo_mask.png -resize 1x1 txt:-
In ImageMagick, -scale 1x1! will ignore the transparent pixels and give you the average of the opaque pixels. So if you want the average of the masked region, you can put the mask into the alpha channel and then use -scale 1x1! to average it down to one pixel. The mask should be white where you want to get the average and black where it should be transparent and ignore those pixels in the average. So this should do it.
convert image.png mask.png -alpha off -compose copy_opacity -composite -scale 1x1! -alpha off -format "%[pixel:u.p{0,0}]" info:
For example if I make the logo: image transparent where it is white and then get the average, I get
convert logo: -transparent white -scale 1x1! -alpha off -format "%[pixel:u.p{0,0}]" info:
srgb(100,81,99)
You can show that this works, by doing it the long way. Multiply the mask by the image, then get the average of each channel of the product. Then get the average of the mask. Then compute the ratios scaled to the range 0 to 255.
convert logo: -transparent white logot.png
convert logot.png -alpha extract mask.png
declare `convert \( logot.png -alpha off \) mask.png -compose multiply -composite -format "IR=%[fx:mean.r]\nIG=%[fx:mean.g]\nIB=%[fx:mean.b]\n" info:`
echo "IR=$IR; IG=$IG; IB=$IB"
IR=0.0651798; IG=0.0529989; IB=0.0641607
MM=`convert mask.png -format "%[fx:mean]\n" info:`
echo "MM=$MM"
MM=0.165872
convert xc: -format "srgb(%[fx:round(255*$IR/$MM)],%[fx:round(255*$IG/$MM)],%[fx:round(255*$IB/$MM)])\n" info:
srgb(100,81,99)
Result is the same as above.
ASIDE: Note that
convert \( logot.png -alpha off \) mask.png -compose multiply -composite ...
in this case is the same as just
convert logot.png -alpha remove ...
But I show it the long way, if the user has a separate mask and image with no transparency.
You should be able to get very nearly what you want with a command something like this...
convert image.png mask.png -compose copyopacity -composite -resize 1x1! txt:-
To output only the color information you can try something like this...
convert image.png mask.png \
-compose copyopacity -composite -resize 1x1! -format "%[pixel:p]" info:
I haven't tried it, but you may have to "-negate" your mask image depending on which version of IM you're using because there have been changes in the way the alpha channel is handled.
You'll get a very slightly different result if you "-trim" before the "-resize".
If you don't want to output the alpha channel information, you can add "-alpha off" after the "-resize".
Perhaps...
convert demo.png -mask demo_mask.png -trim -fx mean -extent 1x1 txt:- |\
tail -1 | cut -d ' ' -f 4
This would work because -trim will reduce the masked image down to the MBR of the ROI. Fx operator -fx mean will convert all pixels into the overall average. And finally the -extent 1x1 will isolate the first pixel in the image. The rest is basic unix utilities.
Another option with better performance...
MEAN=$(convert demo.png -mask demo_mask.png -trim -format '%[fx:mean]' info:-)
convert null: -depth 8 -format "%[pixel:$MEAN]" info:-
Or from the Quantization documentation...
convert demo.png -mask demo_mask.png -trim -scale 1x1\! '%[pixel:s]' info:-

How to blend + translate simultaneously with Imagemagick (or some other scriptable software like Photoshop)?

How can I blend AND translate at the same time ?
Something like this : http://www.imagemagick.org/Usage/layers/#flatten but in such a way that the images are transparent.
I was trying :
composite -blend 90 -page +0+0 input01.jpg -page +500+0 input02.jpg -resize x400 outputSimpleMosaicBlend01.
but this did not work.
So if I have two input images:
Then how can I get an image that looks like the composite image below ?
Any suggestions how to do this programatically (not manually) with ImageMagick ? Or some other tools ?
I would like to create several thousands of composite images like that (for an animation) and I would like to automate the process.
The problem is that I can find examples that overlay images and that translate images but I cannot find examples that do these two operations simultaneously.
This is the main goal of this question, to give such code/script examples, how to do that with image manipulation tools like ImageMagick programmatically.
EDIT:
Things that I tried and did not work:
convert a.jpg -geometry +100+0 b.jpg -compose blend -composite result.jpg
gives:
I tried
convert -background none a.jpg -geometry +100+0 b.jpg -compose blend -composite result.jpg
too which gives the same result.
I got this :
with this
convert -background none input01.jpg input02.jpg -geometry +1200+0 -compose blend -define compose:args=50 -composite result.jpg
command.
It's getting close ! Thanks Mark!
A slightly different way of doing this is to set the width of the output image using -extent and then to overlay the right hand image using -gravity East to align it to the right edge - seems a fraction more intuitive to me - but go with whatever works for you!
convert a.jpg -background white -extent 2800x \
\( b.jpg -resize 150% -alpha on -channel A -evaluate set 50% +channel \) \
-gravity east -composite result.jpg
Thanks to Snigbo, the following command :
convert input02.jpg \( input01.jpg -resize 150% -alpha Opaque -channel A -evaluate Multiply 0.5 +channel -set page +1200+30 \) -background White -layers merge a.jpg
produces:

How to square an image and pad with transparency from the commandline (imagemagick)

The section entitled Square Padding or Cropping describes a method to generate a square image--for a file whose dimensions are unknown--and pad the background with a color.
How do I perform the same operation, but create a transparent background.
Let's make a red off-square image first, that is 300x200:
convert -size 300x200 xc:red image.png
Now let's put make a square image of it, but using a yellow background so you can see it:
convert -background yellow -gravity center image.png -resize 400x400 -extent 400x400 result.png
Now we can do the same thing again, but make the background transparent:
convert -background none -gravity center image.png -resize 400x400 -extent 400x400 result.png
and, just check to make sure it has worked:
identify result.png
result.png PNG 400x400 400x400+0+0 8-bit sRGB 418B 0.000u 0:00.000
These modified methods from Anthony's examples both work for me:
convert thumbnail.gif \
\( +clone -rotate 90 +clone -mosaic +level-colors grey -transparent grey \) \
+swap -gravity center -composite square_padded.gif
convert thumbnail.gif -virtual-pixel none -set option:distort:viewport \
"%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:max((h-w)/2,0)]-%[fx:max((w-h)/2,0)]" \
-filter point -distort SRT 0 +repage square_external.gif
A pure imagemagick command would be preferable, but here's a script that uses the 'file' unix/linux command to extract the dimensions of the file which can then be used on a resize to a square of the max dimension.
#!/usr/bin/env ruby
require 'shellwords'
def dims(image_escaped)
size_data = `file #{image_escaped}`
size_data[/, (\d+ x \d+),/, 1].split(' x ').map(&:to_i)
end
def square(image, pad_color='transparent')
image_esc = Shellwords.escape(image)
maxdim = dims(image_esc).max
geometry = "#{maxdim}x#{maxdim}"
# could use convert if don't want to clobber the image
system "mogrify -resize #{geometry} -background #{pad_color} -gravity center -extent #{geometry} -format png #{image_esc}"
end
ARGV.each do |image|
square(image)
end

Image magick to put images in row and then generate reflection

I was wondering if I can use imagemagick to produce a result image like this
My initial image will be like this
I am able to create the dark image to be put in the background like this -
convert -brightness-contrast -20x10 GARDENS-ILLUSTRATED_JAN-14.jpg out_lighter2.jpg
But I do not know how to put them in line like that and then generate a reflection.
I have got something close, but have no more time, so maybe you can fiddle with it and get it to what you want:
#!/bin/bash
input=input.png
# Calculate width and height
w=$(convert $input -ping -format "%w" info:)
h=$(convert $input -ping -format "%h" info:)
# Calculate how much to chop off bottom and height of reflection
chop=$(echo $h*60/100|bc)
refl=$(echo $h*20/100|bc)
convert $input -alpha on \
\( +clone -flip -size ${w}x${refl} gradient:gray40-black -alpha off -compose CopyOpacity -composite \) \
-append -background white -compose Over -flatten -gravity South -chop 0x${chop} output1.png
# Darken and reduce for second layer
convert output1.png -brightness-contrast -25x8 -resize 90% output2.png
# Darken and reduce for third layer
convert output2.png -brightness-contrast -25x8 -resize 90% output3.png
# Make big new output canvas
neww=$(echo $w*1.6|bc)
newh=$(echo $h*1.6|bc)
# Splat images onto it with offsets
convert -size ${neww}x${newh} xc:transparent -page +0+80 output3.png \
-page +40+40 output2.png \
-page +80+0 output1.png -flatten output.png
My basic idea is to generate the bright, frontmost image first in output1.png, then darken and reduce it into output2.png and again into output3.png. Then composite them all together into output.png with a little offset.

Resources