Can the `cwebp` tool convert a specific color to transparent pixels in its output? - webp

I have some source images which have a black background, and I would like to convert them into WebP images that have a transparent background.
I don't understand the talk about alpha channels from their documentation, so I am unsure if this is even possible with cwebp. I tried some guesswork with the arguments, but none worked.
The command I use for direct conversion from JPG to WebP is:
cwebp ./input.jpg -o ./output.webp
What would I need to add to this in order to get the black background from the input JPG to be transparent in the output WebP?

I think cwebp's options are mostly focused on compression with some limited other options for manipulating the image like cropping and sharpness.
To accomplish this task I would recommend image magick which is a general purpose image manipulation tool. that can remove the transparency, and then you can send that to cwebp.
imagick convert image.jpg -fuzz 2% -transparent black image.png
cwebp image.png -o image.webp

Related

Compose image with mask image using ImageMagick

Suppose I have some image a.jpg and some other image b.jpg.
The desired output out.jpg should be obtained by copying all the pixels from b.jpg that are not black onto a.jpg, all other pixels shall remain untouched.
I tried using composite but had no success whatsoever.
EDITED TO ADD: A solution here can be quite simple and generic, but going forward, please remember to always include your version of ImageMagick and which OS or platform you're working on. There are some syntax differences that can make that important.
At the very simplest, using ImageMagick v6, you should be able to do something like this...
convert b.jpg -background none -transparent black a.jpg +swap -composite out.jpg
That reads in the B image, changes all the pure black pixels to transparent, then reads in the A image, swaps the images so they're in the right order, then composites the modified B image over the A image and writes the output.
You can add a fuzz value like "-fuzz 5%" ahead of the "-transparent" operation to expand the selection to include near-black pixels, also.
To use with IMv7 change "convert" to "magick".

preventing resized images from becoming blurred

im trying to resize image from 72x72 to 512x512 with following command
convert input.png -resize 512x512 output.png
but the output image (output.png) become blur
how to prevent resized images from becoming blurred
how to prevent resized images from becoming blurred
If you want a pixelated image of the original use -sample
# Create small image.
convert -size 72x72 plasma: 72x72.png
# Magnify the image with pixel subsampling.
convert 72x72.png -sample 512 512x512_sample.png
It's true that you can't restore missing data when upscaling images, but there's a lot of various algorithms to calculate what may be missing.
Try using the -filter option in addition to -resize, and checkout the wonderful usage examples here.
Probably the best you can do is use a sharper -filter such as catrom and then do post processing using -unsharp.
convert input.png -filter catrom -resize 512x512 -unsharp 0xSigma output.png
where sigma is the sharpening value, try sigma=1 or 2 (or as desired)
But it will not maintain the same quality as the input as others have mentioned above.
See -unsharp at http://www.imagemagick.org/script/command-line-options.php#unsharp
You can't. For an equal sharpness, you would need more data in the bigger image. Since you have only the data from a small image the result is blurred.
Look at it the other way, if what you asked was possible, instead of compressing the 512x512 image, we would first scale it down to 72x72, compress that (much smaller file) and sent it over with instructions to scale it up to 512x512.

How to keep transparent background in GraphicsMagick .ai to .png conversion?

I'm using a GraphicsMagick package for image processing
When converting Illustrator (.ai) files to .png files, I end up losing transparency so the background ends up white.
I don't have this problem when using the same options and converting with ImageMagick, but I need a solution to make it work with GraphicsMagick specifically.
I don't have much experience with GraphicsMagick, but I would assume that the behavior of -background is the same as ImageMagick's:
gm convert -background transparent source.ai out.png
Note: This works by defining the background before reading the vector graphic

Convert PNG to TIFF with Transparency and CMYK

I am trying hard to convert PNGs to TIFFs using a Batch File. However, the resulting files lack transparency.
S:\*.*\mogrify -path H:\*.*\blau -format tiff H:\*.*\Raw\*.png
pause
S:\*.*\mogrify -fuzz 20%% -fill blue -opaque black H:\*.*\blau\*.tiff
S:\*.*\mogrify -colorspace cmyk H:\*.*\blau\*.tiff
S:\*.*\mogrify -fuzz 60%% -transparent white H:\*.*\blau\*.tiff
The TIFFs have a white background afterwards which I have verified using PhotoShop. What is wrong here? How can I get the resulting files to not lose transparency?
Although TIFF format, technically, does support transparency, it is a dark matter in general. Even some Adobe applications do not support this correctly, if ever. Perhaps, neither does your application; or it may have support in theory, but messes something upon format-to-format conversion.

How to replace white background color with transparent of an image in ImageMagick?

I have an image in .jpg format with white background color. I want to remove the white background color to transparent in Imagemagick. I tried many ways but still the white background can not be removed. Can some one help me to solve this.
You cannot have transparent background colors in your JPEGs. The JPEG file format doesn't support transparency.
If you need transparent background, you need to convert the JPEG to
either PNG (high quality, filesize possibly larger than JPEG)
or GIF (in case you can tolerate low quality and a range of maximally 255 colors).
Example command:
convert your.jpg -transparent white your.png
First, you need to convert the image format from .jpg to .png format, because JPEG does not support transparency. Then use this command:
convert image1.png -fuzz 20% -transparent white result.png
The -fuzz option allows the specified percentage deviation from the pure white colour to be converted to transparent as well. This is useful, for example, when your image contains noise or subtle gradients.
I just found a very neat thing!
magicwand 1,1 -t 20 -f image -r outside -m overlay -o 0 image.jpg imgOutput.png
It is a Fred Weinhaus bash script that can be downloaded from here (for non commercial use only). Also there has about 250 scripts!! and this one is amazing! it did exactly the trick, to remove all background while keeping the inner image dots untouched!
At his page, there are several images as examples so you pick what you need to put on the command line!
The initial position 1,1 is a general guesser saying all the contour is background.
Pay attention that the output must be ".png"
This is my solution without magicwand (replace magick by convert for im < 7.0):
magick img.png -fuzz 20% -fill none -draw "alpha 1x1 floodfill" result.png
Get the background automatically and remove it :
bg=$(convert input.png -format "%[pixel:p{0,0}]" info:)
convert input.png -fuzz 20% -transparent "$bg" output.png

Resources