ImageMagick: Zoom an image by integral zoom factor without any smoothing? - imagemagick

I have a 256×256 PNG image in a.png that just consists of black and white pixels.
What I want is just to zoom it by a factor of 4, so that each pixel becomes a 4×4 block of the same color. So what I did is:
> convert a.png -geometry 1024x1024 a4.png
The problem is that the resulting image a4.png is blurred and is no more 2-colored black-and-white.
What does not work:
With -colors 2 I am getting an output with only 2 colors, but they are grey-ish, not black + white. And the image looks dithered even with -dither none.
-dither none has no effect.
-quantize-colorspace has no effect.
-solarize 128 has no effect either.
-depth 1 does not work (shreds the image).
Thanks in advance for any hints.

Try:
convert input.png -scale 1024x1024 result.png

Related

imagemagick check if image is of almost one single color

Friends,
I have a stack of color-scanned images. Some are from regular white paper with text or images, others were scanned from colored paper (blank pages, same green colored paper used.)
I'd like to identify these colored paper images. Problems:
paper's color ("background") is not scanned very uniformly, often has a wavy or structured pattern
green tone is quite different depending on the scanner used
scanner does not catch the full sheet resulting in a white or shadowed "border" around green area
My idea was to see if say 90% of the image is some sort of green and tried using a sorted histogram. But because of (1) and esp. (2) I have a hard time picking a working color value from the histogram data.
Any help appreciated!
Edit:
Here are three sample images, scanned from the same sheet of paper.
Have a look at HSV colourspace on Wikipedia - specifically this diagram.
It should be a better place to find the colour of your images, regardless of scanner and calibration.
Now, let's create a lime-green, yellow and cyan block and derive its colour using ImageMagick:
magick -size 100x100 xc:lime -colorspace HSV -channel 0 -separate -format "%[fx:mean*360]" info:
120
magick -size 100x100 xc:yellow -colorspace HSV -channel 0 -separate -format "%[fx:mean*360]" info:
60
magick -size 100x100 xc:magenta -colorspace HSV -channel 0 -separate -format "%[fx:mean*360]" info:
300
magick -size 100x100 xc:cyan -colorspace HSV -channel 0 -separate -format "%[fx:mean*360]" info:
180
Hopefully you can see we are correctly calculating the Hue angle. Now to your image. I have added an artificial frame so you can see how to remove the edges:
We can remove the frame like this:
magick YOURSCAN.jpg -gravity center -crop 80% cropped.jpg
So, my complete suggestion would be to crop and convert to HSV and check the mean Hue. You could also test if the image is fairly saturated so it doesn't pick up grey-ish, uncoloured images. You could also test the variance in the Hue channel to see if there are many different colours - or the spread of the hues is large and reject ones where it is large.
magick YOURSCAN.jpg -gravity center -crop 80% -colorspace HSV -channel 0 -separate -format "%[fx:mean*360]" info:
Just for reference, your 3 images come up with the following Hue angles on a scale of 0..360:
79, 68, 73
I would suggest you test a few more samples to establish a reasonable range.

Extract watermark from image

I've got 2 versions of 1 image
Is it possible to get a transparent png file representing this watermark? Can ImageMagick do this?
SilverMonkey has the basic solution using Imagemagick. But the request was for a transparent PNG. So I will add a little bit more to his code to make it transparent by adding -alpha copy.
convert kitty2.jpg kitty1.jpg -compose minus -composite -auto-level -alpha copy watermark1.png
Here is another approach that makes a binary mask for the watermark by thresholding. But it leaves a lot of noise. So I use some morphology open to remove the noise and then some morpholgy close to try to fill in where the text is broken up. Then I add -alpha copy to make the image transparent. But the text is white and the original watermark was light gray. So I turn alpha off, multiply by 0.75 to reduce the brightness of the white letters to gray without affecting the alpha channel. Then turn the alpha channel back on.
convert kitty2.jpg kitty1.jpg -compose minus -composite -threshold 0.6% -morphology open diamond:1 -morphology close octagon:1 -alpha copy -alpha off -evaluate multiply 0.75 -alpha on watermark2.png
For more on morphology, see https://imagemagick.org/Usage/morphology/
You can achieve your goal by calculating the difference between both images (subtract the pixels of both images and calculate the absolute value). This will result in:
ImageMagick seems to be capable of image subtraction, look here:
The code:
convert image2 image1 -compose minus -composite result

Convert the background of an image to transparent

I want to convert the background of a image to transparent. I have used the below script for converting. Also i am converting the image to png.
convert -resample 300x300 -depth 8 "%1"[0] -fuzz 10% -transparent white -flatten -resize 1260x1260 -quality 80 "%2".
I also tried below command:
convert -resample 300x300 -depth 8 "%1"[0] -background none -flatten -resize 1260x1260 -quality 80 "%2"
but using the above script background is not converted to transparent.
Can you please let me know the Imagemagick script for converting the background.
Thanks in advance.
Updated Answer
You really need to show your image, but if the background his white as you say, this should work:
convert start.png -fuzz 40% -transparent white result.png
If that doesn't work, I really cannot help till you show the image.
Original Answer
You need to be clearer as to what colour the background currently is, and how it can be identified - or show your image.
If we assume this is your starting image:
then we can make the blue pixels transparent like this:
convert start.png -fuzz 10% -transparent blue result.png
If we take this as your start image:
and set the fuzz to "within 10% of black", we will do this:
convert start.png -fuzz 10% -transparent black result.png
whereas if we make pixels within 30% of black become transparent:
convert start.png -fuzz 30% -transparent black result.png
we will get this:

Add overlap or duplicate pixels on texture bounds after cropping in imagemagick

I have huge texture Nx1024 pixels. I split it into several 1024x1024 textures with the following command with Imagemagick:
magick image.png -quality 100 -background none -resize x1024 -crop 1024x1024 -extent 1024x1024 output%02d.png
How can I add 2 pixels overlap on left and right sides of output images or duplicate pixels with imagemagick?
PS:
I found the following case (-crop 3x1+2#) crop with equal size but it doesn't suit because I have an unknown width of the input image (it could be multiple of N for 1024)
If on Unix-based OS, then you could try my script, overlapcrop at http://www.fmwconcepts.com/imagemagick/index.php

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