I am trying to convert png images into webP with sampling factor. I am imagemagick tool for conversion, as they mentioned in docs -quality is used for sampling but it did not work.
This command works fine to covert kodak png to JPEG.
convert test/images/kodim$i.png -quality $(($j*5)) -sampling-factor 4:2:0 test/jpeg/kodim$i/`printf "%02d" $j`.jpg
Converting PnG to WebP
convert test/images/kodim$i.png -quality $(($j*5)) test/webP/kodim$i/`printf "%02d" $j`.webp
I want to follow same sampling as mentioned in JPEG.
This work fine on kodak images.
Just install webP encoder
sudo apt-get install webp
Code
for i in {01..24..1}; do
echo WebP Encoding test/images/kodim$i.png
mkdir -p test/webP/kodim$i
for j in {0..20..1}; do
cwebp test/images/kodim$i.png -q $(($j*5)) -o test/webP/kodim$i/`printf "%02d" $j`.webp
done
done
Important
This command creates a WebP lossy image at different qualities.
WebP lossy is always with YUV420 sampling, equivalent to imagemagick's -sampling-factor 2x2 (also -sampling-factor 4:2:0) for jpeg.
Related
I have a DNG image and a cropped monochromatic version of the image. Both are generating the same file as the thumbnail when I run any of the below commands:
magick.exe C:\sample1crop.dng -resize 500x375 C:\crop-T.JPG
or
magick.exe dng:C:\sample1crop.dng -intent relative -sample 500x375> -strip -auto-orient -density 72 C:\crop-T.JPG
or
magick.exe convert dng:C:\sample1original.dng -thumbnail 500x375 -filter -auto-orient -density 72 C:\orig-T.JPG
As I am not allowed to upload the DNG files, thus I uploaded the images in hightail, sharing the link below: https://spaces.hightail.com/space/ThDEDYZVey
The generated thumbnail for both the cases:
I tried to get the thumbnail with exiftool as well:
exiftool -b -PreviewImage C:\86854\SLS\Issues\ART-73712\crop-T.JPG > C:\86854\SLS\Issues\ART-73712\thumbnail.jpg
exiftool -b -ThumbnailImage C:\86854\SLS\Issues\ART-73712\crop-T.JPG > C:\86854\SLS\Issues\ART-73712\thumbnail.jpg
but the resulting file seems corrupted. When I extract the exiftool metadata I see:
"ThumbnailTIFF": "(Binary data 42194 bytes, use -b option to extract)"
My requirement here is to get a generic cmd that provides a cropped monochromatic thumbnail similar to the original image.
Using this exiftool command, I was able to extract four images from those files.
You don't mention what OS or shell you're using but if you're using Windows PowerShell, it is known to corrupt binary data when piping or redirecting. Use CMD and you should be able to extract the images properly.
I am currently struggling to understand why recompressing an uncompressed JPEG image differs its original.
It's clear, that JPEG is a lossy compression, but what if the image to compress is already uncompressed, which means all sampling losses are already included? In other words: Downsampling and DCT should be inversable at this point without loosing data.
To make sure losses are not effected by the color space conversion, this step is skipped and YUV images are used.
Compress YUV image to JPEG (image.yuv --> image.yuv.jpg)
Uncompress JPEG image to YUV image (image.yuv.jpg --> image.yuv.jpg.yuv)
Compress YUV image to JPEG (image.yuv.jpg.yuv --> image.yuv.jpg.yuv.jpg)
Uncompress JPEG image to YUV image (image.yuv.jpg.yuv.jpg --> image.yuv.jpg.yuv.jpg.yuv)
Step 1 includes a lossy compression, so we will not deal with this step anymore. For me, intresting is what happens afterwards:
Uncompressing the JPEG image back to YUV (step 2) leads to an image which perfectly fits all sampling steps if compressed again (step 3). So the JPEG image after step 3 should (from my understanding) be exactly the same as after step 1. Also the YUV images after step 4 and step 2 should equal each other.
Looking at the steps for one 8x8 block the following simplified sequence should illustrate what I am trying to descibe. Lets start with the original YUV image, which can only be decompressed loosing all decimal places:
[ 1.123, 2.345, 3.456, ... ] (YUV)
DTC + Quantization
[ -26, -3, -6, ... ] (Quantized frequency space)
Inverse DTC + Quantization
[ 1, 2, 3, ... ] (YUV)
Doing this with input, which already matches all steps, which may lead to loss of data afterwards (using round numbers in my example), the decompressed image should match its original:
[ 1, 2, 3, ... ] (YUV)
DTC + Quantization
[ -26, -3, -6, ... ] (Quantized frequency space)
Inverse DTC + Quantization
[ 1, 2, 3, ... ] (YUV)
There are also some sources and discussions, which are confirming my idea:
need help creating Jpeg Generational Degradation code
What factors cause or prevent “generational loss” when JPEGs are recompressed multiple times?
Lossless Chroma Subampling
So much for theory. In praxis, I've runned these steps using ffmpeg and Nvidias jpeg samples (using NvJPEGEncoder).
ffmpeg:
#Create YUV image
ffmpeg -y -i image.jpg -s 1920x1080 -pix_fmt yuv420p image.yuv
#YUV to JPEG
ffmpeg -y -s 1920x1080 -pix_fmt yuv420p -i image.yuv image.yuv.jpg
#JPEG TO YUV
ffmpeg -y -i image.yuv.jpg -s 1920x1080 -pix_fmt yuv420p image.yuv.jpg.yuv
#YUV to JPEG
ffmpeg -y -s 1920x1080 -pix_fmt yuv420p -i image.yuv.jpg.yuv image.yuv.jpg.yuv.jpg
#JPEG TO YUV
ffmpeg -y -i image.yuv.jpg.yuv.jpg -s 1920x1080 -pix_fmt yuv420p image.yuv.jpg.yuv.jpg.yuv
#YUV to JPEG
ffmpeg -y -s 1920x1080 -pix_fmt yuv420p -i image.yuv.jpg.yuv.jpg.yuv image.yuv.jpg.yuv.jpg.yuv.jpg
Nvidia:
#Create YUV image
./jpeg_decode num_files 1 image.jpg image.yuv
#YUV to JPEG
./jpeg_encode image.yuv 1920 1080 image.yuv.jpg
#JPEG TO YUV
./jpeg_decode num_files 1 image.yuv.jpg image.yuv.jpg.yuv
#YUV to JPEG
./jpeg_encode image.yuv.jpg.yuv 1920 1080 image.yuv.jpg.yuv.jpg
#JPEG TO YUV
./jpeg_decode num_files 1 image.yuv.jpg.yuv.jpg image.yuv.jpg.yuv.jpg.yuv
#YUV to JPEG
./jpeg_encode image.yuv.jpg.yuv.jpg.yuv 1920 1080 image.yuv.jpg.yuv.jpg.yuv.jpg
But a comparison of the images
image.yuv.jpg.yuv and image.yuv.jpg.yuv.jpg.yuv
image.yuv.jpg.yuv.jpg and image.yuv.jpg.yuv.jpg.yuv.jpg
showing differences in the files. That brings me to my question why and where the difference gets happen, since from my understanding the files should be equal.
I have an animated PNG image but after I convert it to webP - I get the static image without animation.
I've tried cwebp
$ cwebp -q 100 1.png -o 1.webp
$ cwebp -version
1.0.0
and the same with imagemagick
$ magick 1.png -quality 100 -define webp:lossless=true 1.webp
$ magick --version
Version: ImageMagick 7.0.10-29 Q16 x64 2020-09-05 http://www.imagemagick.org
How can I convert to save the animation?
As discovered, cwebp does not support conversion with animation. Possible solution is to extract the individual frames (using ffmpeg or imagemagick for instance) and use img2webp to generate the animation.
I tried to resize a very big image (457 MB and 21600x21600) with the following command
-i test.png -vf scale=320:-1 out.png
but it throws exception saying "Picture size 21600x21600 is invalid". How can I find out the biggest supported resolution by ffmpeg? Is there a way to resize this high resolution image with ffmpeg?
If you want to use ImageMagick it is included in most Linux distros and is available for macOS and Windows.
Your command becomes:
convert test.png -resize 320x result.png
If you are running v7 or newer, use:
magick test.png -resize 320x result.png
If you have lots to do, and you want all the resized images written in a directory called thumbs you can use:
mkdir thumbs
magick mogrify -path thumbs -resize 320x *.png
Alternatively, you may find vips is a lighter-weight installation and does a faster conversion using less memory:
mkdir thumbs
vipsthumbnail -s 320 -o "thumbs/%s.png" image.png
I'm trying to convert color image from .raw format into .jpg or .png format, I used ImageMagick with the following command prompt code:
convert -size 768X576 -depth 8 rgb:my_image.raw my_image.jpeg
It is work successfully to convert the image into jpeg format, but with some problems which are:
1- The resulted image is gray and not colored.
2- The resulted image is subdivided into 9 images as a grid of small images repeated.
When I change rgb into gray, return me 3 separated gray image with different in lighting conditions from darker to lighter.
I'm necessary need to convert the image format, can anyone please help me how can I edit the code or also any other software that able to open the image, I tried very software but they are usefulness, I use windows 10.
I tested it with my own raw input image, and it's working fine.
You may be using a wrong version of ImageMagick.
I downloaded the version: ImageMagick-7.0.8-49-Q8-x64-static.exe from https://imagemagick.org/script/download.php.
In the version I downloaded, the convert command is magick.exe and not convert.
The following command is working:
magick.exe -size 768x576 -depth 8 rgb:my_image.raw my_image.jpeg.
I prefer using FFmpeg for format conversion.
You can download it from https://ffmpeg.zeranoe.com/builds/
Select:
Version: stable (current version is 4.1.3).
Architecture: Windows 64-bit
Linking: static.
Extract the zip file, you only need ffmpeg.exe.
For converting the raw file to jpeg using FFmpeg you can use the following command:
ffmpeg -y -video_size 768x576 -pix_fmt rgb24 -i my_image.raw -pix_fmt yuvj444p my_image.jpeg
Assuming your raw file format is "chunky" RGB (ordered: r,g,b,r,g,b,r,g,b...), make sure the file size is 768*576*3 = 1,327,104 Bytes.
Just to make sure, the problem is not in your input file...
You can create an synthetic input raw image using FFmpeg and convert the result to jpeg:
Create synthetic input:
ffmpeg -y -f lavfi -i testsrc=duration=1:size=768x576:rate=1 -pix_fmt rgb24 -f image2 test_image.raw
Convert synthetic input:
ffmpeg -y -video_size 768x576 -pix_fmt rgb24 -i test_image.raw -pix_fmt yuvj444p test_image.jpeg
Result (test_image.jpeg):
I found the solution:
Apparently the raw image format is "planar" RGB:
RRRRR
RRRRR
RRRRR
GGGGG
GGGGG
GGGGG
BBBBB
BBBBB
BBBBB
Converting it to jpeg (using FFmpeg) is a complicated problem:
ffmpeg -y -video_size 768x576 -pix_fmt gbrp -i m-001-1.raw -filter_complex "extractplanes=g+b+r[g][b][r],[r][g][b]mergeplanes=0x001020:gbrp[v]" -map "[v]" m-001-1.jpeg
FFmpeg has no support for raw "planar" RGB format.
The only "planar" that is close to RGB is GBR (green plane is first).
I used extractplanes and mergeplanes for reordering the color channels.
ImageMagick has a much more simple solution:
magick.exe -depth 8 -interlace plane -size 768x576 rgb:image.raw image.jpeg
Example:
Since stackoverflow not allowing upload of raw files, I uploaded a png image in grayscale format that simulates the "planar" RGB:
For testing the solution use the following steps:
Download the image (imgur named it: D5IUp.png).
Convert from png to raw as "planar" RGB" (.y extension):
ffmpeg -y -i D5IUp.png -c:v rawvideo -pix_fmt gray D5IUp.y
Convert from raw to jpeg:
ffmpeg -y -video_size 128x96 -pix_fmt gbrp -i D5IUp.y -filter_complex "extractplanes=g+b+r[g][b][r],[r][g][b]mergeplanes=0x001020:gbrp[v]" -map "[v]" D5IUp.jpeg
Result:
Example for batch conversion using ImageMagick:
for %%f in (*.raw) do (magick.exe -depth 8 -interlace plane -size 768x576 rgb:"%%f" "%%~nf.jpeg")