imagemagick convert png 16 bit to raw - imagemagick

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

Related

Converting limited range YUV to sRGB using ImageMagick

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

png imagemagick error: Extra compressed data

android version upgrade to 5.x, I got a png image through screenshot.
when I use imagemagick to resize it, I got an error like this
identify: Extra compressed data. `a.png' # warning/png.c/MagickPNGWarningHandler/1777.
identify: Extra compression data. `a.png' # warning/png.c/MagickPNGWarningHandler/1777.
convert: Extra compressed data. `a.png' # warning/png.c/MagickPNGWarningHandler/1777.
convert: Extra compression data. `a.png' # warning/png.c/MagickPNGWarningHandler/1777.
use pngcheck info:
File: a.png (251221 bytes)
chunk IHDR at offset 0x0000c, length 13
1440 x 2560 image, 24-bit RGB, non-interlaced
chunk sBIT at offset 0x00025, length 3
red = 8 = 0x08, green = 8 = 0x08, blue = 8 = 0x08
chunk IDAT at offset 0x00034, length 251149
zlib: deflated, 32K window, maximum compression
chunk IEND at offset 0x3d54d, length 0
No errors detected in a.png (4 chunks, 97.7% compression).
My soft version:
ImageMagick 6.7.9-0
PNG* PNG rw- Portable Network Graphics (libpng 1.2.52)
See http://www.libpng.org/ for details about the PNG format.
PNG24* PNG rw- opaque 24-bit RGB (zlib 1.2.8,1.2.1.2)
I tryed ImageMagick-6.9.1-6 and libpng-1.6.17 zlib-1.2.8, all are newest version, but got error yet
please try this png image , and help me...
http://pan.baidu.com/s/1mg3mkCo
thanks
Add the option -quiet to your convert or identify command so you don't get an error generated.
"Extra compressed data" is just a warning. You may ignore it.
The result of your "convert" operation should be OK; ImageMagick won't propagate the extra data.
Note that "pngcheck" doesn't decompress the IDAT chunks so it won't report about the extra data that is evidently due to a bug in the screenshot application.

"-type truecolorAlpha" does not work

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

ImageMagick memory usage

I have 100 PNG-files and each of them is 8250x4090 big. I need to append them with Imagemagick to one big PNG-file (82500 x 40900) so that I have 10 rows and 10 columns . I know how the code must look like but I get the errors: convert.exe: unable to extend cache
`C:\Row_345.png': No space left on device # error/cache.c/OpenPixelCache/3689.
convert.exe: Memory allocation failed `C:\Row_345.png' # error/png.c/WriteOnePNGImage/8725.
First question: How much space is needed (approximately)? I have 8 GB of Ram and 30 GB free SSD and it wasn't enough. The pictures have polygons and lines in up to 5 different colors. The biggest PNG is 300 KB)
Second question: Is there a way how to make it more clever so that it won't use that much space?
ImageMagick needs 8 bytes per pixel if you are using a Q16 build. A Q8 build only needs 4 bytes per pixel.
82500 * 40900 * 8 = about 27Gbytes
82500 * 40900 * 4 = about 13.5 Gbytes
The size of the PNG is irrelevant; ImageMagick stores them uncompressed.
Possibly ImageMagick is trying to hold two copies -- your 100 small images plus the large result. It may be that you'll have enough memory plus disk to run your conversion with ImageMagick-Q8.
Try doing just a single row of 10 at a time, ten times - so you get 10 rows of 10. Then do row1 plus row2. Then rows 1&2 plus row 3.
convert 1.png 2.png 3.png ... +append row1.png
convert 11.png 12.png 13.png ... +append row2.png
...
convert 91.png 92.png 93.png ... +append row10.png
Then
convert row1.png row2.png -append row1and2.png
You can add -debug cache to your ImageMagick convert command like this:
convert -debug cache 1.png 2.png 3.png ... +append row1.png
You can also look at your resource settings as to what is available to ImageMagick like this:
identify -list resource
File Area Memory Map Disk Thread Time
-------------------------------------------------------------------------------
768 1.0386GB 3.8692GiB 7.7384GiB unlimited 4 unlimited
And increase resources like this:
convert -limit memory 32MiB ...

Chop image into tiles using VIPS command-line

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...

Resources