For a project I'm currently working on, I'm trying to convert a bunch of PNG images to HEIF/HEIC. These images will be used in Xcode's .xcassets, which will then be "compiled" into a .car file.
Compiling the PNGs (~150 total files) results in ~40 MB of Assets.car, which is why I'm trying to convert them to HEIF/HEIC in the first place. I've tried various solutions, such as ImageMagick, "Export as" in GIMP, biodranik/HEIF, libheif's heif-enc, exporting a PNG as 8-bit or 16-bit in Photoshop and doing everything all over again. But everything results in the .heic file being "broken" on iOS. The first image shows the best output I've got so far, but still fringes around the edges. The white rounded rectangle on the right is iOS' Face ID padlock.
The second image is (I think) a 16-bit PNG converted to HEIC using libheif#1.8.0, upgraded through Homebrew. Lossless quality preset, 10-bit output. heif-enc complained about the color space being converted from RGB to YCbCr, stating even though you specified lossless compression, there will be differences because of the color conversion
Is there any way to properly convert PNG files to HEIF/HEIC without such quality loss? Please don't suggest online services to convert files, as I'd like to keep total control of my files.
Note: To get lossless encoding, you need this set of options. Try :-
-L switch encoder to lossless mode
-p chroma=444 switch off color subsampling
--matrix_coefficients=0 encode in RGB color-space
I am trying to compress and resize DSLR camera photos. But my observation so far is that webp has noticeable degraded quality when webp file size is about 30% smaller than JPEG.
Command used to generate webp using imagemagick:
convert 1.JPG -strip -quality 80 -resize 800 -define
webp:method=6 1.webp
My goal is to get webp format that is at least 20% samller file size than JPEG while having virtually no difference in quality between JPEG and webp.
Is this achievable or again there is no such thing as free lunch?
Update Since the time of the original posting, I've discovered that using Google's cwebp compressor shows dramatically improved compression over ImageMagik 6.7.8, which is what powered my initial tests. This is especially true for images with transparency when compared to PNGs. Files using the mac version of the webp command (which uses cwebp under the hood) are about 1/4 of the size of the same file compressed with ImageMagik, and do provide a significant performance boost.
According to Google, "WebP typically acheives an average of 30% more compression than JPG" (source) with similar visual quality to a JPG. However, as you suggest, there in never such a thing as a free lunch.
Quality
Quality is largely a subjective measure, but keep in mind that you're comparing a file compressed at quality 80 with a file that doesn't have that level of compression (at least, this is what I understand from your question). Just running the default conversion without specifying a lower quality may give you slightly smaller files without loss of visible quality. 20% smaller might be a bit much of an ask, though, but it may be achievable for certain images.
convert 1.JPG 1.webp # do not specify quality
Size
In practice, it depends a lot on your settings and your source images. For example, I recently ran this command on all jpg images in a folder on a website "in the wild":
convert filename.jpg -quality 80 -strip -define webp:lossless=false -define webp:method=6 filename.webp
The convert command on this particular server is powered by ImageMagick 6.7.8. Some files were dramatically smaller compared to the original JPGs, while others were actually larger. Overall, after running that command, the total file sizes of all JPG images was 49MB, while the total file size of all WebP images was 29MB. That's a pretty good savings, however, when I ran ImageMagick's JPG compression, it was even better:
convert filename.jpg -sampling-factor 4:2:0 -strip -quality 80 -interlace JPEG filename-new.jpg;
The size of all new jpgs in the directory was 21MB. Both are set to compress at quality 80, but the jpg compression appears to be better. This may have to do with some compression already on my set of test images and how that interacts with the WebP conversion process.
As I found, file sizes can even grow, usually if you are converting between lossy and lossless images. On the FAQ page linked above, Google claims: "this is mainly due to the colorspace difference (YUV420 vs ARGB) and the conversion between these."
tl;dr: In the wild, it may or may not improve file size depending on the type of images, if/how the source file was compressed, and what quality you set for the WebP. Visible degradation is harder thing to measure, but try setting a higher quality, or without specifying a quality at all.
Just to add a side-comment to #Pwpwpw's excellent answer, webp is a good PNG replacement, but not great as a JPG replacement.
It beats PNG because it has better lossless compression algorithm than libpng, and it has the great feature of allowing different compression settings for different channels. You can do lossless on the alpha but lossy on the RGB, for example, which is fantastic for overlays.
Against JPG it does less well. It uses the VP8 codec for lossy, which is only somewhat better than JPG.
I would take a look at HEIC. It uses the much more modern h.265 for lossy compression and typically beats JPG by a factor of two at the same quality. It's the format Apple are using by default on iOS now.
libheif have a nice demo here:
https://strukturag.github.io/libheif/
That's libheif, compiled to javascript and running in your browser. You can upload .heic to it and download as jpg. libheif have some basic command-line tools to encode and decode images.
It does sadly have some patent issues, you might need to be a little cautious.
update Looking further out, the current hope is AVIF: it's the same container format as HEIC (called HEIF, confusingly), but swaps the problematic h.265 compressor for AV1. AV1 is roughly equivalent to h.265, but is patent-free.
update for 2022 AVIF seems to be too slow to be practical, so now hope rests on JXL. It's fast, patent free, supports HDR, transparency, etc., and compression is as good as HEIC. Chrome has support, though it's behind a flag for now.
You are re-encoding lossy compressed jpeg; if you have access to the original raw files you should use them as master. Currently you are compressing noise (and other artefacts) introduced by the jpeg encoder so the WebP codec does not have access to the same information that was already lost at the jpeg encoding. Of course it will look worse.
In other words; you should encode the original image data, if possible. I'm aware that some cameras and equipment only outputs HEIC / JPEG and not RAW so you might be out of options - the best thing to do is to keep the JPEGs as JPEGs.
Just did a quick test with a high-res JPG from my DSLR camera converting to a 410x800 background picture for mobile using Photoshop CC 2020 (and the WebPShop plugin).
Lossless:
PNG 100%: 680 KB
JPG 12 (max): 428 KB
WEBP 100%: 537 KB
50% quality:
JPG 6 (50%): 119KB
WEBP 50%: 45KB
At 50% JPG has more detail, but quite comparable quality.
0 quality:
JPG 0 (min): 51KB
WEBP 0%: 6KB
At 0% WEBP is horrible. But imagine the lowest quality JPG, and that's bad image quality and WEBP definitely beats it at 50%, while still being smaller in file size. So for me.. that's free lunch.
I have certain png files. They are of size approx 1MB. I tried several command but they didn't work for me. Any suggestions. One is as below :
"C:\\Program Files\\ImageMagick-6.9.9-Q16\\mogrify.exe" -depth 8 -format png -define PNG:compression-strategy=2 -define PNG:compression-filter=0 test.png
Thanks,
As pointed out by #fmw42 in comments, your image may already be optimized. Also, #Mark's comment regarding reducing colors is true.
But apart from this, the important thing to know is that "there is no ideal command". You will have to figure out bit depth in your color channels and reduce them. There will always be a trade-off between reducing colors and quality you wish to pick.
Apart from that, there can also be other methods that you can use:
If opacity of PNG is fully opaque, you can strip alpha channel as it
makes no sense in that case. This can give you some file size savings.
If the image is visibly grayscale and still color type is
true-color, true-color-alpha or indexed-color, you can make significant savings by saving the image with a grayscale color space.
Retry optimizing PNG files using adaptive delta filtering and LZ77 Optimizations. This can be done easily using "optipng". But if the image is already optimized enough, this won't provide significant file sizes reduction. Moreover, choice of filtering depends upon png bit depths, so you would have to look up and understand PNG compression from various documentation available online regarding PNG compression.
How can I convert a 256x256 JPEG to a 512x512 JPEG without losing any quality? (I can use jpegtran, but that uses SmartScale, which isn't widely supported.)
It must be converted to JPEG specifically. If it cannot be a literally lossless conversion, can it get very close? Virtually lossless? Thanks.
EDIT:
If it's not possible with any existing tool, please tell me if and how it might be possible in theory.
EDIT 2:
The reason I'm asking is because I want to losslessly combine 512x512 image tiles from the internet in order to re-assemble a panoramic image. The problem is that some of these tiles (along the top and bottom rows of the image) are 256x256 resolution, and require stretching before being combined with the other tiles. So in short, most of the tiles are 512x512 and thus wouldn't require reencoding, but some 256x256 tiles would.
If the 256x256 JPEGs became 512x512 JPEGs, I could combine all the tiles with jpegtran without reencoding, and they would form a simple equirectangular image.
I don't want to encode these panoramas to a different format like PNG, if I can help it, but I know I may need to. So I'm wondering if these few half-sized tiles can be scaled up essentially losslessly.
I am using GraphicsMagick to reduce the file size of PNG images. Some PNG files have their alpha values conveyed in a tRNS chunk to optimize their file size. The problem is GM is writing these files out as RGBA PNG's which is radically increasing their file size, as much more space is needed to store the alpha channel.
Does anyone know how to specify to GraphicsMagick or even ImageMagick to use the original alpha encoding rather than defaulting to RGBA?
I have found out from the maintainers of GraphicsMagick that this functionality has not yet been implemented in ImageMagick or GraphicsMagick. However, the feature is being implemented right now so it may be available in the near future.