I am trying to convert a set of raw video frames from YUV444 to sRGB using ImageMagick.
Input format: Raw YUV444 limited range, BT.709 in planar data order.
Required output format: sRGB (set of PNG images).
Main issue: ImageMagick conversion always applies JPEG conversion formula.
Remark about "limited range" YUV format:
In 8 bits limited range YUV format, the range of Y is [16, 235] and the range of U, V is [16, 240]. (limited range BT.709 is used in HTDV systems).
JPEG uses "full range" YUV format, where Y,U,V range is [0, 255].
sRGB is used in PC systems, and the range of R,G,B is full range [0, 255].
YUV and YCbCr are interchangeable.
For testing, I used the following sample image:
Sample image in sRGB format (rgb_input.png):
I converted the sample to YUV444 format using FFmpeg:
ffmpeg -y -colorspace bt709 -i rgb_input.png -pix_fmt yuv444p yuv_input.yuv
Following image illustrates the YUV444 output (in planar data order):
Input image for ImageMagick in YUV444 format (yuv_input.yuv planar data order illustration):
I converted yuv_input.yuv to PNG using ImageMagick converter (version 7.0.8-51):
magick -depth 8 -interlace plane -size 128x96 -colorspace Rec709YCbCr -sampling-factor 4:4:4 yuv:yuv_input.yuv rgb_output_magick.png
Result of ImageMagick (rgb_output_magick.png):
If you look carefully you see that the image is different than rgb_input.png.
Same conversion using FFmpeg (used as reference):
ffmpeg -y -s 128x96 -colorspace bt709 -pix_fmt yuv444p -i yuv_input.yuv -pix_fmt rgb24 rgb_output_ffmpeg.png
Result of FFmpeg (rgb_output_ffmpeg.png):
Note: The true format of my raw input video frames prevents me from using FFmpeg.
Conversion formula from 8 bits limited range YUV BT.709 to sRGB:
R = 1.1644*Y + 0.00000*U + 1.79270*V - 248.10
G = 1.1644*Y - 0.21325*U - 0.53291*V + 76.878
B = 1.1644*Y + 2.11240*U + 0.00000*V - 289.02
How can I do the above conversion using ImageMagick converter?
As you requested, this would be the ImageMagick color-matrix command for your transform, assuming it makes sense. ImageMagick color-matrix values are normalized to the range 0 to 1, including the offset (right-most) term. I assume your offsets are in the range 0 to 255 (8-bit). So I am not sure if this even makes sense. Nevertheless, here is the way it would look.
magick input -color-matrix \
"1.1644 0.00000 1.79270 0, 0, -0.9729 \
1.1644 -0.21325 -0.53291 0, 0, 0.30148 \
1.1644 2.11240 0.00000 0, 0, -1.1334 \
0 0 0 1, 0, 0 \
0 0 0 0, 1, 0 \
0 0 0 0, 0, 1" output
See http://www.adobetutorialz.com/articles/1987/1/Color-Matrix
Related
I want to convert 16 bit (HALF) exr image into 32 bit exr image. I am trying to do it using ImageMagick built with openEXR, But I am not able to do the the same.
I have Build ImageMagick-7.0.8-23-Q8-windows-x64-static with HDRI flag enabled. I am using 16 bit RGBA EXR file . When I run below command:
identify -verbose "Desk.exr"
It gives Channel depth as 8 bit.
Format: EXR (High Dynamic-range (HDR))
Class: DirectClass
Geometry: 644x874+0+0
Units: Undefined
Colorspace: RGB
Type: TrueColorAlpha
Base type: Undefined
Endianess: Undefined
Depth: 8-bit
Channel depth:
Red: 8-bit
Green: 8-bit
Blue: 8-bit
Alpha: 1-bit
I tried to change it by using convert tool by specifying -depth 16 and 32, but it is still showing channel depth as 8 bit.
int main()
{
Magick::InitializeMagick("");
Magick::Image image;
Magick::Image image2;
Magick::Image image3;
try {
// Read a file into image object
image.read("D:\\IR\\EXR_Support\\Images\\Desk.exr");
//Set bit to 32
image.depth(32);
image.channelDepth(MagickCore::RGBChannels,32);
image.write("D:\\IR\\EXR_Support\\Images\\Desk_32Bits.exr");
}
catch (Magick::Exception &error_)
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
I checked channel depth of output file , that I have set 32 bit, but it is also showing :
Depth: 8-bit
Channel depth:
Red: 8-bit
Green: 8-bit
Blue: 8-bit
Alpha: 1-bit
I have built imagemagick with Quantum 8 bit and I am expecting it should work for 16 and 32 bit.
The quantum depth, a.k.a. Q setting, is specified at compile time. You can't increase it at run-time.
If you want to process 32-bit images, you will need to re-compile with 32-bit quantum depth.
I have some images whose type is PaletteAlpha and which have to be TrueColorAlpha. I am trying to use the command
convert testein -type TrueColorAlpha testeou
to do this conversion but the result is an image which still has Type: PaletteAlpha. Is this behaviour expected? How can I transform from one type to the other?
I have also tried things such as PNG:testeou but with no results.
My IM version is 6.7.8-9.
The PNG32: prefix forces the output PNG to be RGBA:
convert logo: -transparent white png32:logot32
identify -verbose logot32 | grep Type
Type: PaletteAlpha
pngcheck logot32
OK: logot32 (640x480, 32-bit RGB+alpha, non-interlaced, 94.1%).
"identify" reports the "Type" of the image after it has been read in. In this case, it is an image with transparency and fewer than 256 colors, so it's reported as PaletteAlpha.
"pngcheck" reports what's actually stored in the PNG file, which is RGBA.
In fact, "identify" also gives more information about the PNG file. Near the end of the "identify -verbose" output can be found:
Properties:
...
png:IHDR.bit_depth : 8
png:IHDR.color_type : 6 (RGBA)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height : 640, 480
...
Version: ImageMagick 6.7.8-9 2015-06-01 Q16 http://www.imagemagick.org
I'm trying to convert a 16 bit greyscale PNG to a raw file. The image size is 640*480.
First, identify:
$ identify image.png
image.png PNG 640x480 640x480+0+0 16-bit PseudoClass 65536c 299KB 0.000u 0:00.000
I'm expecting the result file to be 640*480*2 bytes in size.
Attempt 1:
$ convert image.png -depth 16 image.raw
This gives a file size of 330805 bytes. Its first 16 bytes look like:
0x00000000: 89504E47 0D0A1A0A 0000000D 49484452 .PNG........IHDR
Attempt 2:
$ convert image.png -depth 16 image.rgb
This gives a file size of 1843200 bytes, which is 640*480*2*3.
I'm running imagemagick version 6.7.7-10 on Ubuntu 14.04.
Any ideas?
Updated Answer
It occurred to me since answering you, that there is a simpler method of doing what you want, that takes advantage of ImageMagick's little-used stream tool, to stream raw pixel data around.
In effect, you can use this command
stream -map r -storage-type short image.png image.raw
which will read the Red channel (-map r), which is the same as the Green and Blue channels if your image is greyscale, and write it out as unsigned 16-bit shorts (-storage-type short) to the output file image.raw.
This is cleaner than my original answer - though should give identical results.
Original Answer
If you write an RGB raw file, you will get 3 channels - R, G and B. Try writing a PGM (Portable Greymap) like this...
convert image.png -depth 16 pgm:-
P5
640 480
65535
<binary data> < binary data>
The PGM format is detailed here, but suffice to say that there is header with a P followed by a digit describing the actual subtype, then a width and height and then a MAX VALUE that describes the range of the pixel intensities. In your case, the MAX VALUE is 65535 rather than 255 because your data are 16-bit.
You can the strip the header like this:
convert image.png -depth 16 pgm:- | tail -c 614400 > file.raw
If you are converting lots of files of different sizes and dislike the hard-coded 614400, and are using bash, you can get ImageMagick to tell you the size (height * width * 2 bytes/pixel) and use that like this:
bytes=$(identify -format "%[fx:h*w*2]" image.png)
convert image.png -depth 16 pgm:- | tail -c $bytes > file.raw
gray might be the format you want:
convert image.png -depth 16 image.gray
This command stores each pixel in 2 bytes and nothing else in the file.
Here I provide a minimal synthetic example: https://superuser.com/questions/294270/how-to-view-raw-binary-data-as-an-image-with-given-width-and-height/978432#978432
.raw is not really a "pixel only" format: it does contain some metadata: https://en.wikipedia.org/wiki/Raw_image_format#File_contents
Is there a simple way to extract EXIF data as text from an image file using ImageMagick. Something like the way to do it for IPTC data:
convert input.jpeg data.iptc <= binary data
convert input.jpeg data.iptctext <= textual data
Sadly this doesn't work for EXIF data:
convert input.jpeg data.exif <= binary data
convert input.jpeg data.exiftext <= not working
I know that i can use
identify -verbose input.jpeg
but then i would have to parse the result in order to search for all the EXIF and IPTC data.
So is there a simple way to do it with pure ImageMagick?
Like this:
identify -format '%[EXIF:*]' image.jpg
Output:
exif:ApertureValue=4845/1918
exif:BrightnessValue=4991/792
exif:ColorSpace=1
exif:ComponentsConfiguration=1, 2, 3, 0
exif:Compression=6
exif:DateTime=2014:08:31 14:18:07
exif:DateTimeDigitized=2014:08:31 14:18:07
exif:DateTimeOriginal=2014:08:31 14:18:07
exif:ExifImageLength=2448
exif:ExifImageWidth=3264
exif:ExifOffset=204
exif:ExifVersion=48, 50, 50, 49
...
...
I have a large Tiff image that I want to chop into 512x512 tiles and write to disk.
In the past I've used ImageMagick like so:
convert -crop 512x512 +repage image_in.tif image_out_%d.tif
But recently this hasn't been working, processes running out of memory, etc.
Is there a similar command in VIPS? I know there's a CLI but I can't find an example or useful explanation in the documentation, and I'm still trying to figure out the nip2 GUI thing. Any help appreciated. :)
libvips has a operator which can do this for you very quickly. Try:
$ vips dzsave wtc.tif outdir --depth one --tile-size 512 --overlap 0 --suffix .tif
That's the DeepZoom writer making a depth 1 pyramid of tif tiles. Look in outdir_files/0 for the output tiles. There's a chapter in the docs talking about how to use dzsave.
It's a lot quicker than IM for me:
$ time convert -crop 512x512 +repage huge.tif x/image_out_%d.tif
real 0m5.623s
user 0m2.060s
sys 0m2.148s
$ time vips dzsave huge.tif x --depth one --tile-size 512 --overlap 0 --suffix .tif
real 0m1.643s
user 0m1.668s
sys 0m1.000s
Where huge.tif is a 10,000 by 10,000 pixel uncompressed RGB image. Plus it'll process any size image in only a small amount of memory.
I am running into the same issue. It seems that VIPS does not have a built-in command like the one from imagemagick above, but you can do this with some scripting (Python-code snippet):
for x in xrange(0, tiles_per_row):
xoffset = x * tile_size
for y in xrange(0, tiles_per_row):
yoffset = y * tile_size
filename = "%d_%d_%d.png" % (zoom, x, y)
command = "vips im_extract_area %s %s %d %d %d %d" % (base_image_name, filename, xoffset, yoffset, tile_size, tile_size)
os.system(command)
However you won't get the same speed as with imagemagick cropping...