sampling-factor always similar

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
lahabana
Posts: 8
Joined: 2013-04-26T06:41:57-07:00
Authentication code: 6789

sampling-factor always similar

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: sampling-factor always similar

Post by anthony »

More than likely the image is greyscale, which means the image does not need sub-sampling of color channels.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
lahabana
Posts: 8
Joined: 2013-04-26T06:41:57-07:00
Authentication code: 6789

Re: sampling-factor always similar

Post 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
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: sampling-factor always similar

Post 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.
lahabana
Posts: 8
Joined: 2013-04-26T06:41:57-07:00
Authentication code: 6789

Re: sampling-factor always similar

Post 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!
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: sampling-factor always similar

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: sampling-factor always similar

Post by snibgo »

Can you supply the input file, 4.jpg? "-type trueColor" works for me.
snibgo's IM pages: im.snibgo.com
lahabana
Posts: 8
Joined: 2013-04-26T06:41:57-07:00
Authentication code: 6789

Re: sampling-factor always similar

Post by lahabana »

For sure here it is:
Image
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: sampling-factor always similar

Post 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)
lahabana
Posts: 8
Joined: 2013-04-26T06:41:57-07:00
Authentication code: 6789

Re: sampling-factor always similar

Post by lahabana »

So this is a bug isn't it? Should I report it or you've already did it?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: sampling-factor always similar

Post by magick »

Its a bug and its already fixed. Look for a patch in ImageMagick 6.8.5-7 by sometime tomorrow. Thanks.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: sampling-factor always similar

Post by GreenKoopa »

Congrats lahabana on finding a bug. Thanks magick for fixing it within minutes of our coming up with simple & reproducible test cases.
lahabana
Posts: 8
Joined: 2013-04-26T06:41:57-07:00
Authentication code: 6789

Re: sampling-factor always similar

Post by lahabana »

Perfect thank you very much!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: sampling-factor always similar

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: sampling-factor always similar

Post by GreenKoopa »

snibgo, insightful observation and clever work-around.
Post Reply