I have a set of source PNG images and I want to use parts of them to assemble a final PNG image. The parts are rectangular and never overlap on the destination but are of different sizes. Sometimes it is the whole of a source image and sometimes just a subsection. I want to edit the sources many times and re-assemble the final image each time, so I tried to write a script using sh and Imagemagick to do it.
I tried this
convert \
-size 512x512 null:\
-page +96+32 source_a.png\
-page +96+0 source_b.png[32x32+16+16] \
-background transparent\
-layers merge\
destination.png
(just with two source images for illustration)
I want all of source_a.png and a piece of source_b.png. The first is OK, but using the 'inline crop' syntax on source_b.png gives me an error:
convert: geometry does not contain image `source_b.png' # warning/transform.c/CropImage/666.
The image is big enough:
$ identify source_b.png
source_b.png PNG 64x48 64x48+0+0 8-bit sRGB 3.7KB 0.000u 0:00.000
What's the best way to do this? I am using ImageMagick 6.9.7-0 Q16 on MacOS 10.12
An alternative might be to use -geometry and -composte to achieve the same effect:
convert -size 512x512 xc:white \
source_a.png -geometry +96+32 -composite \
source_b.png[32x32+16+16] -geometry +96+0 -composite \
result.png
PNG's will preserve the paging from inline cropping, so the addition page will through the ROI out of bounds. I imaging it'll be simpler to -repage the inline crop then attempting to clear previous paging & setting new page.
convert -size 512x512 null: \
-page +96+32 source_a.png \
\( source_b.png[32x32+16+16] -repage +96+0 \) \
-background transparent\
-layers merge\
destination.png
Related
I'n very new to imagemagick and i need to learn how could i combined these two commands to make it work.
I need to add background image to a transparent image. I tried to combine these two commands but was not successful.
magick mogrify -path Output_Path -trim -filter Triangle -define filter:support=2 -thumbnail 450x450 -gravity center -extent 500x500 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB -strip Transparent_Image_Path
magick Background_Image.png Transparent_Image.png -composite output.jpg
Result Should Be Like This :
Image Reference
Thanks in advance!
There are several possible syntaxes, depending on how your brain likes to work ;-)
What you need to bear in mind when trying to understand this is that all processing operations, e.g. -crop or -resize apply to all loaded images. So, you either need to load images that need processing and process them before loading images you don't want affected, or you need to restrict processing to certain images only... using parenthesised "aside" operations.
You can proceed like this:
load transparent foreground image first and process it before loading the background image, then
load background image, then
exchange the order and composite
That looks like this:
magick FOREGROUND.PNG -resize ... -crop ... \
BACKGROUND.PNG \
+swap -composite RESULT.JPG
Alternatively, you could:
load the background image, then
load the foreground image and process it "aside" within parentheses so that the background image is not affected
composite
That looks like this:
magick BACKGROUND.PNG \
\( FOREGROUND.PNG -resize ... -crop ... \) \
-composite RESULT.JPG
If you need to independently process both background and foreground images prior to compositing, use:
magick \
\( BACKGROUND.PNG -resize ... -crop ... \) \
\( FOREGROUND.PNG -resize ... -crop ... \) \
-composite RESULT.JPG
Note that if you use Windows:
the backslashes at line-ends become carets (^) and may not have trailing spaces after them,
the backslashes preceding opening and closing parentheses must be omitted,
most single quotes probably need replacing with double quotes - probably not applicable to this specific question.
Note that your command is probably unnecessarily complicated, I would recommend omitting quite a lot of it and seeing how you get on. For example, all the define png:XXX=YYY settings are irrelevant if you aren't creating a PNG as output.
You can carry forward the JPEG-related parameters (quality, interlace, upsampling) and put them in anywhere you like, probably at the start like this, but put the colorspace and strip at the end to ensure both input files are stripped:
magick -quality 82 -define jpeg:fancy-upsampling=off \
BACKGROUND.PNG ... \
FOREGROUND.PNG ... \
-composite -colorspace sRGB -strip RESULT.JPG
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
I'm assuming ImageMagick is the best option for this, but please let me know if you have other recommendations that can be scripted.
I am trying to replace all the 32x32 tiles of an image with a single tile. This is an example for the original image:
This is the tile that I want to use to replace all tiles on the original image:
And this is what I want the output to be:
I've figured out from other posts on Stack Overflow that I can use ImageMagick's composite option to overlay the tile onto the original image:
$ convert original.png tile.png -composite overlay.png
Resulting in the following:
And I assume by knowing the original images dimensions I can overlay the tile manually multiple times. But is there a way to automate the process. In the example pictures I have given, I need to overlay the tile 8 times on the original 64x128 image.
How can I do this with ImageMagick or another software? And if ImageMagick, would the montage or composite command be a better option?
Edit: As an additional question, would it be possible to skip tiles that are completely transparent?
Input example:
Output example:
It isn't really important to be able to do this part, but would be nice.
If the tile image fits evenly into the dimensions of the original, a command like this should do most of what you want...
convert original.png tile.png -background none -virtual-pixel tile \
-set option:distort:viewport %[fx:u.w]x%[fx:u.h] -distort SRT 0 +swap \
-compose copyopacity -composite overlay.png
That reads in both images. Then it creates another canvas the size of the original and filled with multiple copies of the tile image. Then it uses the original as a transparency mask to create a copy of the new tiled image with the same transparent cells as the original.
I don't know why you would need to overlay the 8 tiles on the original. Just create it from scratch and name the output the same as your original
You could use Imagemagick montage to do that (unix syntax):
nx=`convert original.png -format "%[fx:w/32]" info:`
ny=`convert original.png -format "%[fx:h/32]" info:`
num=$((nx*ny-1))
montage tile.png -duplicate $num -tile ${nx}x${ny} -geometry +0+0 result.png
Here I use convert to duplicated the tile, but it uses a relatively current -duplicate feature. If you do not have a current enough version of Imagemagick, then just repeat the tile in montage as follows:
montage Ro1Lp.png Ro1Lp.png Ro1Lp.png Ro1Lp.png Ro1Lp.png Ro1Lp.png Ro1Lp.png Ro1Lp.png -tile 2x8 -geometry +0+0 result.png
As Fred (fmw42) says, "why don't you just create the whole image from scratch?".
Maybe your description isn't complete, so here are a couple more pieces that might help you work it out.
Given bluetiles.png and singlered.png:
you can position red ones as you wish like this:
convert bluetiles.png \
singlered.png -geometry +0+32 -composite \
singlered.png -geometry +32+96 -composite result.png
Given bluewithtransparent.png:
you can copy its transparency to the newly-created image like this:
convert bluetiles.png \
singlered.png -geometry +0+32 -composite \
singlered.png -geometry +32+96 -composite \
\( bluewithtransparent.png -alpha extract \) -compose copyopacity -composite result.png
Here's the original image I'm trying to remove background from:
I am trying to use imagemagick to remove the background from an image. When the image has a white product, my script doesn't work well. It removes the white from inside the product also. In brief I'm trying to do the following
create a mask image (replace background pixels with white with fuzz and threshold)
apply the mask over the original image to generate the output
If I use a fuzz factor of 0, like shown below, i get the background removed, but it creates a nasty halo around it. What can be done here?
I would take advantage of HSL colorspace, and create an alpha mask from the lightness channel.
convert tshirt.jpg \( \
+clone -colorspace HSL -separate \
-delete 0,1 -fx 'u>0.975?0:1' \) \
-compose CopyOpacity -composite \
out.png
I would go for a simple threshold to pick out the white and then some sort of filtration to remove the noise/ragged edges. So, for example
convert shirt.jpg -threshold 99.99% -negate result.jpg
which gives this:
Then apply some median filtering to smooth it:
convert shirt.jpg -threshold 99.99% -median 5 -negate result.jpg
or maybe a bigger filter:
convert shirt.jpg -threshold 99.99% -median 11 -negate result.jpg
which gives this
Alternatively, you may get on better with an erosion and a dilation...
convert shirt.jpg -threshold 99.99% -negate \
-morphology erode diamond:3 \
-morphology dilate diamond:3 result.jpg
You may like to use Anthony Thyssen's flicker_compare to flicker between the input and result image to see what you have got, see here.
./flickercompare -o flick.gif shirt.jpg result.jpg
We have the following PSD
https://app.box.com/s/rf514j3wnic1xkt6y1q3b5qnk0zds2rl
This is a transparent PSD with one base layer for the ring metal.
And additional layer for each individual stones.
Something like https://app.box.com/s/i8lhshbl27pvjmmczjq2bhla4mzw9cwn
and this
We want to be able to apply tint to each individual stone layer based of user input and export the final image as PNG file. To provide users the ability to design the ring to their liking.
Now I am trying to figure out what is the best way to achieve this functionality ?
We would like the image generated server side in .net so it can be shared.
What is the best way to approach this. I am thinking of using an image library like imagemagick to convert the image.
But I am unable to locate any examples where you can alter multiple layer with in a PSD file before converting it to another file format.
Any example or suggestion on methods to achieve this will of great help.
Editing layered PSD files is not going to be easy. I would suggest you maybe save the ring and each of the gemstone layers as a separate PNG file. Then you can do something along these lines with ImageMagick:
#!/bin/bash
convert ring.png \
\( layer-1.png \( +clone +level-colors red \) -compose Multiply -composite \) -compose overlay -composite \
\( layer-2.png \( +clone +level-colors green \) -compose VividLight -composite \) -compose overlay -composite \
\( layer-3.png \( +clone +level-colors blue \) -compose LinearBurn -composite \) -compose overlay -composite \
\( layer-4.png \( +clone +level-colors "#ffff00" \) -compose Saturate -composite \) -compose overlay -composite \
result.png
Giving you a PNG file like this:
You may like to experiment with the blending modes, I just tried a few that looked vaguely pleasing to my eye. If you want a list of all available blending modes, you can do:
identify -list compose
Atop
Blend
Blur
Bumpmap
ChangeMask
Clear
ColorBurn
ColorDodge
Colorize
CopyBlack
CopyBlue
CopyCyan
CopyGreen
Copy
CopyMagenta
CopyOpacity
CopyRed
CopyYellow
Darken
DarkenIntensity
DivideDst
DivideSrc
Dst
Difference
Displace
Dissolve
Distort
DstAtop
DstIn
DstOut
DstOver
Exclusion
HardLight
HardMix
Hue
In
Lighten
LightenIntensity
LinearBurn
LinearDodge
LinearLight
Luminize
Mathematics
MinusDst
MinusSrc
Modulate
ModulusAdd
ModulusSubtract
Multiply
None
Out
Overlay
Over
PegtopLight
PinLight
Plus
Replace
Saturate
Screen
SoftLight
Src
SrcAtop
SrcIn
SrcOut
SrcOver
VividLight
Xor
I also specified some colours by name and one by hex, so you can see how to do it that way if you prefer.
P.S. If you keep all your artwork as PSD files, you can always use Adobe's ExtendScript to script the export of the various layers as separate PNG files with a single keypress...
P.P.S. It is possible for ImageMagick to extract the layers from a PSD file itself, but I extracted the layers from your file and they are not all the same size as the background image and I cannot find their correct positioning relative to it. If you know something about how the layers were created and whether they could be made identical size and aligned with the background, you could replace layer-n.png in my examples with PhotoshopFile.psd[n]
P.P.P.S. If you want to do this server side in .NET you should take a look at the .NET library for ImageMagick that can be found here: https://magick.codeplex.com/. If you need help translating the commands above to C# you could ask a question on the discussions page there.