How to change the depth of an image using imagemagick? - imagemagick

I have tried adding the option -depth 12 to the string
convert transparentPNG.png -resize 500x400 -background white -flatten -depth 12 png_small.jpg
The input file is a transparent png to which I'm adding a background and then changing the depth. But the depth remains the same as 8bits. I verified the same using the -verbose.
I'm not sure what could I be doing wrong here. I'm referring to the site link
The transparent input png file used for my test can be found here
Let me know if you have any questions on the tests i did. Hoping to get some tips.

A JPG can only be 8-bit, so your internal 12-bit image is converted back to 8-bit when you save the result.

Related

Ruby + RMagick + base64 image + RGB conversion from GrayScale doesn't work

I'm uploading base64 encoded image to a RoR application. When I receive the image, it has a rgb color scheme (correct), when I write the image on file to be uploaded with paperclip gem, the image color scheme change from rgb to grayscale.
Here is the code:
source = src.gsub(/^data:image\/(png|jpg|jpeg);base64,/,"")
blob = Base64.decode64(source)
img = Magick::Image.from_blob(blob).first
img.colorspace = Magick::SRGBColorspace
img.add_profile "#{Rails.root.to_s}/lib/color_profiles/RGB.icc"
img.write(url = "#{Rails.root.to_s}/tmp/#{self.id}_logo.png")
image = File.open(url)
the img is correctly a RGB image, if I check the resulted created file:
identify -format "%[colorspace]" #{url}
the color scheme is Gray.
Additional info:
The uploaded image is all black with white text, if I upload same image with red background, the final image is correctly an RGB image.
There seems to be a bug in ImageMagick 6.9.9.27 and 7.0.7.15 when reporting the conversion of a grayscale image to RGB PNG. Identify -verbose is reporting grayscale but the string format %[colorspace] is properly reporting sRGB as are the PNG tags. I have reported this bug. For example:
convert logo: -colorspace gray logo.jpg
convert logo.jpg PNG24:logo.png
convert logo.png -format "%[colorspace]" info:
sRGB
identify -verbose logo.png
...
Colorspace: Gray
...
png:IHDR.color-type-orig: 2
png:IHDR.color_type: 2 (Truecolor)
I do not understand. Is your image a color image or a grayscale only image?
IM 6.7.7.10 was during a time that ImageMagick was changing from non-linear gray to linear gray and back again. And also had RGB and sRGB swapped. So you may have a version where gray was linear (darker than non-linear gray) or where RGB and sRGB were swapped. You can convert back to non-linear using one of the following (I do not recall which to use at this time). The other will convert from linear to non-linear. If I assume your input image was grayscale and not color, then try one of these:
convert input -colorspace RGB -colorspace gray result
or
convert input -colorspace sRGB -colorspace gray result
If it is not grayscale, but color only, then leave off the -colorspace gray in these commands.
I would urge you to upgrade if you can. You are well over 200 versions old.
P.S. It is also possible your profile is causing a problem. I don't know what the RGB.icc profile is. Is that an Adobe RGB profile or an sRGB profile.
Can you reproduce your problem using Command Line ImageMagick? If so, post the command line you used. Sorry I do not know Ruby or RMagick.
P.S. 2 Apart from the lighter/darker issue, if you are trying to convert a grayscale image to color, then you will need to specify the output as PNG24:name.png. That is the only way to force a grayscale image to report colorspace=RGB without inserting color pixels.

ImageMagick: Invalid ICC profile after conversion

I wrote an application which trims and resizes a bunch of images via ImageMagick. The images are also converted to grayscale. But when I try to open a converted image in Photoshop CC, the following warning appears:
The embedded ICC profile cannot be used because the ICC profile is
invalid, ignoring the profile.
Plus, the image profile is in grayscale, but I want it to be in RGB. How can I achieve this with ImageMagick? I played around with the different parameters, but none worked for me.
This command is currently in use:
convert ${src} -type grayscale -set colorspace RGB -background white -gravity center -extent ${longest}x${longest} ${dest}
Also, this one didn't work either:
convert.exe ${src} -set colorspace RGB -set profile RGB.icc ${dest}
A simple way to force the output PNG to be RGB is to replace $(dest) in your command with PNG24:$(dest), or use PNG32:$(dest) if your image has transparency). If you do this, then your existing RGB color profile will be OK.
You can also try PNG8:$(dest) which will be OK if you have fewer than 256 gray levels, and will result in a smaller file size.

ImageMagick convert adds several extra "border" colors from tiff to jpeg?

I created an 8-bit .tiff image ("test.tiff") containing a grid of 30 different color patches in the RGB color space using ImageMagick -convert.
When I convert this image into a jpeg (which is what I need) using:
convert -quality 100 -colorspace RGB -depth 8 test.tiff test.jpg
The identify -verbose command reveals that the resulting jpeg has several additional colors in the color table, each only taking up a few (1-4) pixels and residing very near the desired colors in RGB space. My assumption is that some kind of border bleeding is happening; maybe due to compression?
I don't understand why this border bleeding has occurred, especially given that it does not occur when I convert the tiff image to either a bmp or pcx image.
Thank you
By definition, JPEG is a lossy compression. The effects your experiencing are expected with the JPEG format. Setting the -quality of 100 will not have a 1-to-1 image result as tiff.
See additional answers:
Should I use JPG or TIFF for high-quality prints?
[...] because every time [JPEG] would save it it would generate some changes.
Is Jpeg lossless when quality is set to 100?
At [quality] 100, you just get the LEAST loss possible.
I don't know how you created your 30 colour swatch, or how your histogram looks, but you might try adding -dither None and -colors 30 options to your convert commands:
convert test.tiff -dither None -colors 30 ...

imagemagick convert CMYK pdf to RGB jpeg or PNG and preseerve colors

I have a cmyk pdf that I am trying to convert to a RGB jpeg or png file but have the colors stay pretty close to what the CMYK version is (compared to how photoshop does it)
I am trying the following command but the colors change drastically from a red color to almost bright neon red and so on.
Here is the command
convert cmykpdf.pdf +profile icc -profile AdobeRGB1998.icc -colorspace sRGB jpegtesting.jpg
Any ideas? or thoughts on how to do this. I tried saving it as a PNG also and same issue occurs and have tried changing sRGB to just RGB
NOTE: It doesnt necessarily need to be RGB jpeg it can even be CMYK jpeg but i just need it to be displayed in the browser correctly and I know safari does not display cmyk jpegs correctly
My goal is to just display a img in the browser that shows the correct color and correct resolution nothing pixilated
The solution is fairly easy, there's nothing voodoo or special about Photoshop's CMYK to RGB nowadays. Imagemagick uses LCMS color engine, which does its job just fine.
But first you'll need to edit delegates.xml file inside IM's directory. Find the line with delegate decode="ps:cmyk" and insert -dUseCIEColor=false near the end, so it looks like that:
<delegate decode="ps:cmyk" restrain="True" command=""#PSDelegate#" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dEPSCrop -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pamcmyk32" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" -dUseCIEColor=false "-f%s" "-f%s""/>
It's necessary because otherwise Ghostscript (before returning pam image to ImageMagick) will perform CMYK to CMYK convertion (assuming DeviceCMYK to be CIEbased CMYK), and you probably don't want that, as colors will shift considerably.
Then try this command:
convert -density 144 cmyk.pdf -profile USWebCoatedSWOP.icc -resample 72 -profile "sRGB Color Space Profile.icm" -quality 100 out.jpg
Here we take cmyk.pdf (rather, temporary pam image that GS returns to IM), assign CMYK profile (just as you do in Photoshop, when you open a file or do it explicitly - therefore choose profile that describes you input CMYK best), convert it to sRGB profile (because I don't think you want AdobeRGB as color space of images for Internet) and save to jpeg. Reduce quality parameter as needed.
One more trick here is additional manual anti-aliasing -- note intermediate resolution of 144 dpi and final 72 dpi. Because I don't think that Ghostscript's anti-aliasing with -dGraphicsAlphaBits=4 is en par with Photoshop's anti-aliasing.
The result of this command looks exactly the same as converted in Photoshop.
You could try this:
convert -negate -colorspace RGB srcfile.jpg outputfile.jpg
Let me know if it works!
Based on previous answers, I finally managed to keep natural colors from a CMYK pdf to a RGB png simply using:
convert -colorspace sRGB cmyk.pdf rbg.png

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