Page 1 of 2
sampling-factor always similar
Posted: 2013-05-15T06:26:04-07:00
by lahabana
Hi,
I'm trying to convert a bunch of images so that they are all exactly similar (size, jpeg format...)
What I use is:
convert 4.jpg -background '#000000' -resize 640x480 -gravity center -extent 640x480 -strip -compress JPEG -quality 80 -sampling-factor 2x2,1x1,1x1 -type TrueColor 4_b.jpg
This works most of the times even with cross format. But I'm having an issue with one image which has as sampling factor:
Properties:
jpeg:colorspace: 1
jpeg:sampling-factor: 1x1
Artifacts:
filename: 3.jpg
When I use convert on this image (with the command specified before) the output sampling is the same:
Properties:
jpeg:colorspace: 1
jpeg:sampling-factor: 1x1
Artifacts:
filename: 3.jpg
I think I am probably missing something but I have been reading quite a bit without any success... So I'm turning to you for some help
Thx
Re: sampling-factor always similar
Posted: 2013-05-15T18:38:13-07:00
by anthony
More than likely the image is greyscale, which means the image does not need sub-sampling of color channels.
Re: sampling-factor always similar
Posted: 2013-05-16T03:24:25-07:00
by lahabana
Yes indeed I have just realized that the image is grayscale.
However, I thought the option -type TrueColor was supposed to force grayscale image to be written as normal images. The doc says:
"-type type the image type.
Choose from: Bilevel, Grayscale, GrayscaleMatte, Palette, PaletteMatte, TrueColor, TrueColorMatte, ColorSeparation, or ColorSeparationMatte.
Normally, when a format supports different subformats such as grayscale and truecolor, the encoder will try to choose an efficient subformat. The -type option can be used to override this behavior. For example, to prevent a JPEG from being written in grayscale format even though only gray pixels are present, use. convert bird.png -type TrueColor bird.jpg"
Is there a way to force it then?
I use avconv with the images after and it is with the images you give in. This is why I want these grayscale to be encoded as color image. Thx
Re: sampling-factor always similar
Posted: 2013-05-16T10:34:26-07:00
by GreenKoopa
Are you sure the grayscale image isn't being saved as TrueColor (other than your perceptive sampling factor observation)? Internally jpeg uses the Y'CbCr colorspace, so Anthony is right to observe that Cb and Cr channels will be constant for a grayscale image, even one represented as TrueColor.
Re: sampling-factor always similar
Posted: 2013-05-17T08:43:33-07:00
by lahabana
Hi,
I've just checked and the image is not in true colors (even after applying my command line). Here is the reduced identify -verbose:
Code: Select all
Image: testFiles/640x480_5_multipleformat/3.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Class: PseudoClass
Geometry: 207x300+0+0
Resolution: 96x96
Print size: 2.15625x3.125
Units: PixelsPerInch
Type: Grayscale
Base type: Grayscale
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 8-bit
Channel statistics:
Gray:
min: 0 (0)
max: 255 (1)
mean: 151.852 (0.595499)
standard deviation: 57.2143 (0.22437)
kurtosis: -0.670763
skewness: -0.579322
Colors: 256
Histogram:
...
Rendering intent: Undefined
Gamma: 1
Interlace: JPEG
Background color: gray(255,255,255)
Border color: gray(223,223,223)
Matte color: gray(189,189,189)
Transparent color: gray(0,0,0)
Compose: Over
Page geometry: 207x300+0+0
Dispose: Undefined
Iterations: 0
Compression: JPEG
Quality: 89
Orientation: Undefined
Properties:
comment: File written by Adobe Photoshop? 4.0
date:create: 2013-05-15T13:09:09+00:00
date:modify: 2013-05-15T13:09:09+00:00
jpeg:colorspace: 1
jpeg:sampling-factor: 1x1
signature: 16a3c8829457dbb68c6fa26a1cd9906a4916f07f039ac7e903c929b009d72c58
Profiles:
Profile-8bim: 4218 bytes
Artifacts:
filename: testFiles/640x480_5_multipleformat/3.jpg
verbose: true
Tainted: False
Filesize: 16.6KB
Number pixels: 62.1K
Pixels per second: 0B
User time: 0.000u
Elapsed time: 0:01.000
Version: ImageMagick 6.7.7-10 2013-02-25 Q16 http://www.imagemagick.org
Is there a way to make sure that all images converted will be forced to be in TrueColor and to have the same sampling factor. Even if it makes grayscale images store unnecessary information?
Thx for your help so far!
Re: sampling-factor always similar
Posted: 2013-05-17T10:23:39-07:00
by GreenKoopa
lahabana wrote:Is there a way to make sure that all images converted will be forced to be in TrueColor and to have the same sampling factor. Even if it makes grayscale images store unnecessary information?
This is a reasonable need, and I also think that
-type TrueColor should have been your solution. I think your sampling factor problem is simply a side effect of the GrayScale / TrueColor problem. If nobody else has an answer for you soon, I'll do some testing. Maybe you found a bug.
Re: sampling-factor always similar
Posted: 2013-05-17T10:45:02-07:00
by snibgo
Can you supply the input file, 4.jpg? "-type trueColor" works for me.
Re: sampling-factor always similar
Posted: 2013-05-17T10:53:09-07:00
by lahabana
For sure here it is:
Re: sampling-factor always similar
Posted: 2013-05-17T10:55:21-07:00
by GreenKoopa
I get these results with two recent versions, as well as both jpg and png.
convert -size 16x64 gradient:#000-#FFF -type GrayScale gray.jpg
convert gray.jpg -format "%r %[channels] %[colorspace]" info:-
PseudoClass Gray gray Gray <= Case 1: pass
convert -size 16x64 gradient:#000-#FFF -type TrueColor rgb.jpg
convert rgb.jpg -format "%r %[channels] %[colorspace]" info:-
DirectClass sRGB srgb sRGB <= Case 2: pass
convert -size 16x64 gradient:#000-#FFF default.jpg
convert default.jpg -format "%r %[channels] %[colorspace]" info:-
DirectClass sRGB srgb sRGB <= Case 3: not as documented
convert rgb.jpg -type GrayScale rgb_to_gray.jpg
convert rgb_to_gray.jpg -format "%r %[channels] %[colorspace]" info:-
PseudoClass Gray gray Gray <= Case 4: pass
convert gray.jpg -type TrueColor gray_to_rgb.jpg
convert gray_to_rgb.jpg -format "%r %[channels] %[colorspace]" info:-
PseudoClass Gray gray Gray <= Case 5: fail (the case lahabana needed)
Re: sampling-factor always similar
Posted: 2013-05-17T11:26:22-07:00
by lahabana
So this is a bug isn't it? Should I report it or you've already did it?
Re: sampling-factor always similar
Posted: 2013-05-17T11:58:50-07:00
by magick
Its a bug and its already fixed. Look for a patch in ImageMagick 6.8.5-7 by sometime tomorrow. Thanks.
Re: sampling-factor always similar
Posted: 2013-05-17T12:12:03-07:00
by GreenKoopa
Congrats lahabana on finding a bug. Thanks magick for fixing it within minutes of our coming up with simple & reproducible test cases.
Re: sampling-factor always similar
Posted: 2013-05-17T12:12:32-07:00
by lahabana
Perfect thank you very much!
Re: sampling-factor always similar
Posted: 2013-05-17T12:27:59-07:00
by snibgo
Bug fixed, so this is superfluous, but might be of interest.
I think the situation is this:
When IM is saving an image, it checks whether it has any colour. The checking comes from knowledge of the input file (was it greyscale?) and any operations performed. If is definitely greyscale (because the input was grey, and nothing has changed that), IM saves as greyscale, irrespective of "-type".
But if the image might contain a colour, "-type" does have an effect.
For example (Windows 7, IM 6.8.5):
Code: Select all
convert ^
3.jpg ^
( +clone -fill Red -colorize 100% ) ^
+swap -composite ^
-sampling-factor 2x2,1x1,1x1 -type Truecolor r.jpg
Composing the original over a red image doesn't change it, but IM isn't clever enough to realise this, so "-type" is effective. This trick also works for GreenKoopa's last example.
I would like "-type truecolor" to always be effective, even when the image has no colour.
Re: sampling-factor always similar
Posted: 2013-05-17T13:39:11-07:00
by GreenKoopa
snibgo, insightful observation and clever work-around.