I am using this command to flatten 3 images in a folder.
for pos1 in 1*.png; do for pos3 in 3*.png; do for pos6 in 6*.png; do convert -gravity center $pos1 $pos3 $pos6 -layers flatten $pos1$pos3$pos6 ;done ;done ;done
this is how my folder looks:
For some reason, imagemagick ignores the 1*.png images and creates a composition of just $pos3 and $pos6. What could be the reason for this behaviour and how do I fix it? The images i am manipulating are the product of some older image magick manipulation.
UPDATE:
After some more testing it looks like that the error comes from the fact that the input images are the output of come previous convert / composite command (cant remember which one I used). Still, I don't know how to fix this problem.
identify command output (no histogram):
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 3843x3402+0+0
Units: Undefined
Type: TrueColorAlpha
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 1-bit
Channel statistics:
Pixels: 13073886
Red:
min: 0 (0)
max: 255 (1)
mean: 246.072 (0.96499)
standard deviation: 44.204 (0.173349)
kurtosis: 23.2206
skewness: -4.96363
entropy: 0.0669139
Green:
min: 0 (0)
max: 255 (1)
mean: 246.072 (0.96499)
standard deviation: 44.2038 (0.173348)
kurtosis: 23.2205
skewness: -4.96362
entropy: 0.0669169
Blue:
min: 0 (0)
max: 255 (1)
mean: 246.072 (0.96499)
standard deviation: 44.2038 (0.173348)
kurtosis: 23.2207
skewness: -4.96364
entropy: 0.066917
Alpha:
min: 255 (1)
max: 255 (1)
mean: 255 (1)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
entropy: 0
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 184.554 (0.723742)
standard deviation: 38.2817 (0.150124)
kurtosis: 150.659
skewness: -25.8099
entropy: 0.050187
Rendering intent: Perceptual
Gamma: 0.45455
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgba(223,223,223,1)
Matte color: grey74
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 3843x3402+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2017-04-05T20:18:59+02:00
date:modify: 2017-04-05T10:36:08+02:00
png:bKGD: chunk was found (see Background color, above)
png:cHRM: chunk was found (see Chromaticity, above)
png:gAMA: gamma=0.45454544 (See Gamma, above)
png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 6
png:IHDR.color_type: 6 (RGBA)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 3843, 3402
png:sRGB: intent=0 (Perceptual Intent)
png:text: 2 tEXt/zTXt/iTXt chunks were found
png:tIME: 2017-04-03T18:20:37Z
signature: ea0ce8b483b92bda18d999d203a5e1329d48fb63059d6702051b74f7930286fc
Artifacts:
filename: /Users/mainuser/Desktop/final/HALFS/to/1_1ND1_1.png
verbose: true
Tainted: False
Filesize: 710KB
Number pixels: 13.07M
Pixels per second: 54.47MB
User time: 0.230u
Elapsed time: 0:01.240
Version: ImageMagick 6.9.7-3 Q16 x86_64 2017-01-07 http://www.imagemagick.org
Updated Answer
As we are having some problems and you can't share your images, let's make some of our own:
convert -size 600x200 xc:none -fill red -draw "circle 100,100 100,200" -bordercolor black -border 1 1.png
convert -size 600x200 xc:none -fill lime -draw "circle 300,100 400,100" -bordercolor black -border 1 2.png
convert -size 600x200 xc:none -fill blue -draw "circle 500,100 600,100" -bordercolor black -border 1 3.png
Now let's flatten them onto a transparent background:
convert [123].png -background none -flatten result.png
Maybe you can create these images and try with your version of ImageMagick.
Original Answer
It depends on how your files are in terms of size and transparency, but try this in the middle of your loop:
convert -gravity center "$pos1" "$pos3" -composite "$pos6" -composite "${pos1}${pos3}${pos6}.png"
This assumes that "$pos1" is the largest file in terms of pixel dimensions.
Note that you should always double quote shell variables when expanding them in case your filenames happen to have spaces in them.
This is not the answer to the original question but I found it because I had the same problem that images were just ignored when using convert.
My solution was allocate higher ressources in resources.xml as described in this solution.
Maybe this is useful to someone at some point.
Related
I have an image created in a Affinity Designer, which has the following channels:
R, G and B:
Alpha:
When I use this image in other places (Unreal Engine), I get small black artifacts on the edge of the shape due to the black part of the RGB channels. The only important information I need is the alpha channel.
How can I use ImageMagick to replace the RGB channels with white, while retaining the alpha channel?
Here is a simple way to do the in ImageMagick:
First, since you did not provide the actual image, I will reconstruct it from the rgb.png and alpha.png
convert rgb.png alpha.png -alpha off -compose copy_opacity -composite image.png
Now, to make the rgb part white, simply do:
convert image.png -fill white -colorize 100 result.png
Now to see that the rgb parts are fully white, we do:
identify -verbose result.png
.
.
.
Channel statistics:
Pixels: 512
Gray:
min: 255 (1)
max: 255 (1)
mean: 255 (1)
standard deviation: 0 (0)
kurtosis: -4.9152e+52
skewness: 9.375e+35
entropy: 0
Alpha:
min: 0 (0)
max: 255 (1)
mean: 187.152 (0.733931)
standard deviation: 109.252 (0.42844)
kurtosis: -0.822401
skewness: -1.05578
entropy: 0.336307
So the rgb components are a constant 255 (white) and the image is now white with an alpha component.
Try the following...
magick \( input.png -fill white -draw 'color 0,0 reset' \) \
\( input.png -alpha extract \) \
-compose Copy_Alpha -composite \
output.png
I'm sure there's better ways to do the above. Reading the input.png twice, and extracting the alpha channel is bit redundant.
The first part replaces all RGB channels with white, but also clears the alpha channel.
input.png -fill white -draw 'color 0,0 reset'
The second part grabs the alpha channel.
input.png -alpha extract
Finally, we copy the extracted alpha values back over the white image's alpha channel.
-compose Copy_Alpha -composite
If your using ImageMagick-6, then replace magick with convert, and Copy_Alpha with Copy_Opacity.
Best of Luck!
I want to convert a png with a transparent background to one with a background with 1% opacity.
I want to do this because files within a .dmg window are only selectable/draggable, if there is a background to the file image.
I would like to do this using the cli convert function from Imagemagick.
You question is not very clear to me.
If the following is not what you want, then please explain in more detail and/or post your input image.
Lets assume you have only binary transparency, i.e, part fully transparent and part fully opaque, such as the following where the white area has been made transparent:
Here are the alpha value summaries of the original.
Alpha:
min: 0 (0)
max: 255 (1)
mean: 42.2975 (0.165872)
standard deviation: 94.8515 (0.371967)
kurtosis: 1.22756
skewness: 1.79654
entropy: 0.648175
To make the fully transparent part 1% opaque and leave the fully opaque part unchanged, you could do the following in ImageMagick, namely, add 1% to the alpha channel. This makes black (0) go to 1% (3) and clips white so that it stays at 255.
convert logot.png -channel a -evaluate add 1% +channel logot2.png
You will have to extract the images to see the transparency.
Here are the alpha value summaries afterwards, where min of 0 is now min of 3. The (0.0117647) is the value in the range 0 to 1 or equivalently 1.17647%.
Alpha:
min: 3 (0.0117647)
max: 255 (1)
mean: 44.7998 (0.175686)
standard deviation: 93.7356 (0.367591)
kurtosis: 1.22756
skewness: 1.79654
entropy: 0.648175
The source is an RGBA PNG image (color_type=6 and bit_depth=8).
I need an image with indexed color and 2 palette items (color_type=3, bit_depth=1).
I tried with ImageMagick, but was able to reach only 1bit grayscale image (color_type=0, bit_depth=1) or 2bit indexed color image (color_type=3, bit_depth=2) in which only 2 colors are in use.
According to the PNG specification such image is possible, but how to create it?
Here is an image I am trying to convert:
The result of "convert input.png -type palette -depth 1 output.png":
The result of "convert input.png -type palette -depth 1 -colors 2 output.png"
Both results have bit_depth=2, but the second one uses only 2 colors of 4 possible.
Use one of the following 3, ordered from least to most ancillary tags in the output file:
convert -colors 2 -define png:include-chunk=none -verbose input.png output.png
convert -colors 2 -define png:exclude-chunk=bkgd -verbose input.png output.png
convert -colors 2 -background #000000 -verbose input.png output.png
The root of this problem is that ImageMagick by default tags the image with a white background color (bKGD) PNG chunk (unnecessarily I'd say), and then adds this color to the palette if it's not already there, even if the image has no pixels of that color. Your particular image doesn't have white after converting to 2-color, so the unneeded background color tag becomes a 3rd color and it can no longer be saved as a 1-bit indexed color image. See also this from the author of IM.
The reason others have failed to reproduce the problem is probably that they've tested with images where one of the 2 colors happened to be white.
The 1st option with -define png:include-chunk=none avoids the problem by not outputting any ancillary PNG chunks at all (e.g. bKGD, gAMA, cHRM, tEXt, tIME). Like pngcrush -rem alla. I'd prefer this for a less cluttered file (without it IM will add a few of these even if they weren't in the input file). Note: There's also the simple -strip option which should avoid most of these, but as of v6.9.3 it won't cut bKGD due to a bug.
The 2nd with -define png:exclude-chunk=bkgd removes only the offending background chunk.
The 3rd option with -background #000000 retains all ancillary PNG chunks including bKGD but sets it black, one of the 2 colors present in your image, so it can still be 1-bit. Adjust the color for an image with neither white nor black in it.
Note that for all these I'm including the -verbose switch. Not overly verbose; just goes from zero to two lines of status output where you'll notice a "2c" if the image stayed 2-color or a "3c" if not. It will also tell you if the palette reduction was lossy or not. It also outputs "8-bit sRGB" for most images, even paletted ones with fewer than 8 bits-per-pixel; that's not a bug as in your comment to another answer #johnfound, this refers not to the bits-per-pixel but to the bits-per-color component. It can be more than 8-bit for (rare) deep color images.
convert input.png -background white -type palette -depth 1 -colors 2 output.png
works for me.
(Why -depth=1 is not enough? No idea.)
BTW, the tweakpng tool is useful for checking this kind of things.
Update: if the images have transparency, you might play safer by removing it explicitly:
convert input.png -background white -flatten -type palette -depth 1 -colors 2 output.png
(You can replace white for your preference)
I think the command you need is this:
convert input.png -type palette -depth 1 output.png
If not, please post your input image and say how/where you find the color_type and bit_depth fields you refer to.
Exiftool tells me this:
ExifTool Version Number : 9.76
File Name : output.png
Directory : .
File Size : 2.3 kB
File Modification Date/Time : 2015:01:10 19:20:33+00:00
File Access Date/Time : 2015:01:10 19:21:46+00:00
File Inode Change Date/Time : 2015:01:10 19:20:33+00:00
File Permissions : rw-r--r--
File Type : PNG
MIME Type : image/png
Image Width : 640
Image Height : 427
Bit Depth : 1
Color Type : Grayscale
Compression : Deflate/Inflate
Filter : Adaptive
Interlace : Noninterlaced
Gamma : 2.2
Background Color : 1
Datecreate : 2015-01-10T10:21:30+00:00
Datemodify : 2015-01-10T10:21:30+00:00
Image Size : 640x427
ImageMagick identify tells me this:
Image: output.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: PseudoClass
Geometry: 640x427+0+0
Units: Undefined
Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: Gray
Depth: 8/1-bit
Channel depth:
gray: 1-bit
Channel statistics:
Pixels: 273280
Gray:
min: 0 (0)
max: 255 (1)
mean: 1.90168 (0.00745755)
standard deviation: 21.9388 (0.0860345)
kurtosis: 129.1
skewness: 11.4499
Colors: 2
Histogram:
271242: ( 0, 0, 0) #000000 gray(0)
2038: (255,255,255) #FFFFFF gray(255)
Colormap entries: 2
Colormap:
0: ( 0, 0, 0) #000000 gray(0)
1: (255,255,255) #FFFFFF gray(255)
Rendering intent: Undefined
Gamma: 0.45455
Background color: gray(255)
Border color: gray(223)
Matte color: gray(189)
Transparent color: gray(0)
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 640x427+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2015-01-10T19:20:33+00:00
date:modify: 2015-01-10T19:20:33+00:00
png:bKGD: chunk was found (see Background color, above)
png:gAMA: gamma=0.45455 (See Gamma, above)
png:IHDR.bit-depth-orig: 1
png:IHDR.bit_depth: 1
png:IHDR.color-type-orig: 0
png:IHDR.color_type: 0 (Grayscale)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 640, 427
png:text: 2 tEXt/zTXt/iTXt chunks were found
signature: 3e08d7fea7bc7aeb0659ac2e2696084083d35ce30b0e3075dc561ad94259eaec
Artifacts:
filename: output.png
verbose: true
Tainted: True
Filesize: 2.33KB
Number pixels: 273K
Pixels per second: 27.33MB
User time: 0.000u
Elapsed time: 0:01.009
Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-11-10 http://www.imagemagick.org
I need to convert from png to png.
I am using ImageMagick 6.8.7-7 on Mac OS X to decrease the dimension of my png.
Here is an exact example : start.png : 20866 × 8957 = 6.6 MB
start.png is a detailed floor plan just black and white
convert start.png -resize 16384x7033 out.png
Result is out.png 16384 × 7033 = 36.9 MB
(even convert start.png -resize 16384x7033 out.jpg => out.jpg 22.7MB)
If I apply "pngquant" to out.png I can reduce to 15MB
Why does this increase happen?
I am looking for a option on convert which will not increase the size of out.png?
EDIT
Maybe it helps if I provide more infos...
I executed "identify -verbose" on both files and got following output:
Image: start.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 20866x8957+0+0
Resolution: 98.43x98.43
Print size: 211.988x90.9987
Units: PixelsPerCentimeter
Type: Palette
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2014-01-19T09:06:05+01:00
date:modify: 2014-01-17T11:48:05+01:00
png:iCCP: chunk was found
png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 2
png:IHDR.color_type: 2 (Truecolor)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 20866, 8957
png:pHYs: x_res=9843, y_res=9843, units=1`
Profiles:
Profile-icc: 2576 bytes
Artifacts:
filename: start.png
verbose: true
Tainted: False
Filesize: 6.603MB
Number pixels: 186.9M
Pixels per second: 41.63MB
User time: 4.480u
Elapsed time: 0:05.490
**OUT.png
Image: out.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 16384x7033+0+0
Resolution: 98.43x98.43
Print size: 166.453x71.4518
Units: PixelsPerCentimeter
Type: TrueColor
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2014-01-19T09:55:41+01:00
date:modify: 2014-01-19T09:55:41+01:00
png:bKGD: chunk was found (see Background color, above)
png:cHRM: chunk was found (see Chromaticity, above)
png:iCCP: chunk was found
png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 2
png:IHDR.color_type: 2 (Truecolor)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 16384, 7033
png:pHYs: x_res=9843, y_res=9843, units=1
png:text: 3 tEXt/zTXt/iTXt chunks were found
Profiles:
Profile-icc: 2576 bytes
Artifacts:
filename: out.png
verbose: true
Tainted: False
Filesize: 36.89MB
Number pixels: 115.2M
Pixels per second: 38.54MB
User time: 2.980u
Elapsed time: 0:03.990
Version: ImageMagick 6.8.7-7 Q16 x86_6*
The reason the filesize increases is that your resize operation increases the number of colors. You could replace "resize WxH" with "-sample WxH" to only use the existing (black and white) colors.
If you're using ImageMagick, you might as well remove the ICC profile too, with "-define png:exclude-chunk=iCCP", because it's not of much use in a black-and-white image. This option isn't yet available in GraphicsMagick.
We are using the open source image manipulation tool ImageMagick to take thumbnails of various files. We thought things were running pretty smoothly with all this, until we realized that some browsers (namely Internet Explorer 8 and Chrome) were unable to display the JPEG files.
I can only assume that the output of the ImageMagick conversions are not web safe JPEGs. Why is this the case, and can it be fixed at all?
Incidentally, we are using the command line tool convert and we are using these parameters:
convert -thumbnail 150x fileToThumb outputPath
EDIT:
Image: 50afd2b1-e42c-4e90-9244-9c5a00c1933d.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Class: DirectClass
Geometry: 150x212+0+0
Resolution: 72x72
Print size: 2.08333x2.94444
Units: PixelsPerInch
Type: ColorSeparation
Endianess: Undefined
Colorspace: CMYK
Depth: 8-bit
Channel depth:
cyan: 8-bit
magenta: 8-bit
yellow: 8-bit
black: 8-bit
Channel statistics:
cyan:
min: 0 (0)
max: 255 (1)
mean: 28.492 (0.111734)
standard deviation: 61.879 (0.242663)
kurtosis: 5.32422
skewness: 2.47138
magenta:
min: 0 (0)
max: 255 (1)
mean: 43.5579 (0.170815)
standard deviation: 72.7733 (0.285386)
kurtosis: 1.31682
skewness: 1.57362
yellow:
min: 0 (0)
max: 255 (1)
mean: 53.0706 (0.20812)
standard deviation: 85.3198 (0.334587)
kurtosis: -0.0841614
skewness: 1.2581
black:
min: 0 (0)
max: 52 (0.203922)
mean: 0.149434 (0.000586016)
standard deviation: 1.78161 (0.00698672)
kurtosis: 364.996
skewness: 17.91
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 25.054 (0.098251)
standard deviation: 61.3102 (0.240432)
kurtosis: 5.28177
skewness: 2.5304
Total ink density: 300%
Rendering intent: Undefined
Interlace: None
Background color: white
Border color: cmyk(223,223,223,0)
Matte color: grey74
Transparent color: black
Page geometry: 150x212+0+0
Dispose: Undefined
Iterations: 0
Compression: JPEG
Quality: 92
Orientation: Undefined
Properties:
create-date: 2009-08-17T11:38:16+01:00
jpeg:colorspace: 4
jpeg:sampling-factor: 1x1,1x1,1x1,1x1
modify-date: 2009-08-17T11:37:48+01:00
signature: f5e85add196c10f1d73f416482e779245595a644877696fffb2637b5b97f6b9c
Artifacts:
verbose: true
Tainted: False
Filesize: 20.5kb
Number pixels: 31.1kb
Version: ImageMagick 6.5.3-10 2009-06-19 Q16 OpenMP http://www.imagemagick.org
Here is the identify output (also noticed that this JPEG image displays as a solid black image on the Mac):
[Where is this image ???]
Check that the colorspace of the JPEG is RGB, browsers won't like any other colorspace.
To check it, use the ImageMagick identify command
identify -verbose path/to/jpeg.jpg
The output should start off like this (it's about 50 lines long typically)
Format: JPEG (Joint Photographic Experts Group JFIF format)
Class: DirectClass
Geometry: 467x330+0+0
Type: TrueColor
Endianess: Undefined
Colorspace: RGB <----you are looking for this
Depth: 8-bit
...
If you've got a different colorspace (e.g. CMYK), you can use -colorspace RGB in your convert command line to force the RGB colorspace to be used.
If that does not help, you might want to paste the entire output of the identify command into your question as that will greatly aid diagnosis.
You can use the following command to convert cmyk to rgb
http://imagemagick.org/Usage/formats/#color_profile
convert cmyk_image.jpg -colorspace
rgb rgb_image.jpg
g.b.1981 answer is OK, but I have to add: for me it works reliably only when adding -type truecolor:
convert cmyk.jpg -colorspace rgb -type truecolor rgb.jpg