Is it possible using the PHP5 imagick wrapper? - imagick

I don't have possibility to run convert via cmdline and the only way to use Imagick is via the PHP5 wrapper. I would need to run following command:
convert in.jpg -channel RGB -auto-level +level-colors red,white out.jpg
Unfortunately I haven't found in documentation any method that would perform any of these options. Can somebody with better knowledge of the php wrapper help?
I found only Imagick::levelImage() but couldn't simulate -auto-level and level-colors is nowhere to be found.
Thanks for help

In the end I got help on imagemagick forum, so for anybody with similar problem, here is what worked for me.
$img = new Imagick('in.jpg');
// grayscale image
$img->modulateImage(100, 0, 100);
// instead of auto-level option, where 65535 = Imagick::getQuantumRange()
$img->contrastStretchImage (0, 65535, Imagick::CHANNEL_ALL);
// instead of missing level-colors option, apply clut with the 2 desired colors
$clut = new Imagick();
$clut->newPseudoImage(1, 2, "gradient:red-white");
$img->clutImage($clut);
$img->writeImage('out.jpg');
For more info you can read the imagemagick thread

Related

imagemagick -auto-level in ffmpeg

I've been looking for a solution to perform the equivalent of magick -auto-level in ffmpeg but am unable to find anything. There are some references stating I should first manually discover the levels using other software like GIMP, however, I'm looking for an automated and simpler solution. Any ideas how to address this?
I've tried the following - the first enhanced the image which was initially prettry dark, but the second over-exposed it, causing it to become mostly white:
convert img.jpg -auto-level img2.jpg
ffmpeg -i img.jpg -vf "normalize" -y img2.jpg
Note: I apologize I cannot share the image as it is restricted by privacy policy

imagemagick convert -contrast-stretch doesn't work after update to 6.9.x and up

I use convert to process my scans. I use convert as follows:
convert in.tiff +dither -colors 2 -colorspace gray -contrast-stretch 0 out.tiff
The input file is about 8.5MBytes. In V6.8.9-9 the output size is about 1.1MBytes. In 7.0.8-14 the output size stays at 8.5MBytes.
I have searched for problems with -contrast-stretchbut I couldn't find info on my topic. The same problem occurs with the -threshold option. With the old version size gets smaller with the new version size doesn't decrease.
This is on ubuntu 18.04 libtiff-tools is installed. Old Version is on ubuntu 16.04.
Am I missing something?
Regards
Thommy
I found a solution. With identify -verbose out.tif I saw that depth differed. With the new version it is 8/1-bit in the old version it is 1-bit. Searching on that topic I found that -depth 1 could be the solution. And indeed adding -depth 1 to the command line solved my problem.
Still I don't know why the old version worked w/o it. But I am back to what I want.
Thommy

How to identify if an image have an embed sRGB icc profile?

I would like to remove from my images their sRGB profile if they have one. the problem i don't know how to identify that an image have a sRGB profile. What the good way to do ?
In Imagemagick, use
convert image.suffix -format "%[profiles]\n" info:
or
convert image.suffix -format "%[profile:icc]\n" info:
unless your Imagemagick version is ancient.
For example:
convert logo.jpg -format "%[profiles]\n" info:
icc
convert logo.jpg -format "%[profile:icc]\n" info:
sRGB built-in
Please do not accept as the answer as Fred has already shown you the way, but here's how you can get the profile with PHP Imagick:
$img = new Imagick('image.jpg');
$img->setOption('format','%[profiles]');
$img->setImageFilename('info:');
$fd = fopen('php://memory','rwb');
$img->writeImageFile($fd);
fseek($fd, 0);
$info = fread($fd, 1024);
fclose($fd);
var_dump($info);
Sample Output
string(18) "app12,exif,icc,xmp"

How to read jpeg image with Adobe RGB colorspace in OpenCV?

I am trying to read and write jpegs wth Adobe RGB colorspace in OpenCV. OpenCV assumes the jpeg has sRGB colorspace and when displaying or writing to file, the image loses some of its color intensity. I found this intensity loss was due to colorspace difference by answers given to my previous question.
Is there anyway I can make OpenCV to read Adobe RGB colorspace without casting it to sRGB?
Some information that is hopefully useful for anyone looking for a work-around for dealing with ICC and other profiles...
You can see what profiles are present in an image using ImageMagick which is installed on most Linux distros and is available for macOS and Windows. In the Terminal, or Command Prompt on Windows, run:
magick identify -verbose frog.jpg | grep 'Profile-.*bytes'
Profile-icc: 578 bytes
That tells you this image has a 578 byte ICC profile embedded.
If you are on Windows and don't have grep, you can equally use the following, though you may need to double up the percent sign, or prefix it with a caret (^) or somehow escape it:
magick identify -format "%[profiles]" frog.jpg
icc
You can extract that profile from the image, using this command:
magick frog.jpg frog.icc
And, you'll get a 578 byte ICC profile:
ls -l *icc
-rw-r--r-- 1 mark staff 578 24 Apr 10:36 frog.icc
You can check that the profile looks correct using the file command:
file *icc
frog.icc: ColorSync color profile 2.1, type ADBE, RGB/XYZ-mntr device by ADBE, 560 bytes, 11-8-2000 19:51:59 "Adobe RGB (1998)"
You can apply that profile to some other file like this:
magick other.jpg -profile "icc:frog.icc" otherWithProfile.jpg
Once you have extracted the profile using the above method, you can apply it to an image that you plan to use with OpenCV using PIL/Pillow's ImageCMS Module.
For that, I think you need to use these steps or something very similar, though I have not tested it:
from PIL import Image, ImageCMS
import numpy as np
# Open frog with PIL/Pillow
im = Image.open('frog.jpg')
iccp = PIL.ImageCms.getOpenProfile("profile.icc")
rgbp = ImageCms.createProfile("sRGB")
icc2rgb = ImageCms.buildTransformFromOpenProfiles(rgbp, iccp, "RGB", "RGB")
result = ImageCms.applyTransform(im, icc2rgb)
You should then be able to convert the resulting image to a Numpy array that OpenCV can work with using:
OpenCVim = np.array(result)
and remember to then convert from RGB ordering to BGR with cv2.cvtColor().
Rather than detect and extract the ICC profile with ImageMagick, you could equally use PIL/Pillow like this:
from PIL import Image
im = Image.open('frog.jpg')
# Now look at "im.info"
{'jfif': 257,
'jfif_version': (1, 1),
'dpi': (72, 72),
'jfif_unit': 1,
'jfif_density': (72, 72),
'icc_profile': b'\x00\x00\x020ADBE\x02\x10\x00\x00mntrRGB XYZ \x07\xd0\x00\x08\x00\x0b\x00\x13\x003\x00;acspAPPL\x00\x00\x00\x00none\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf6\xd6\x00\x01\x00\x00\x00\x00\xd3-ADBE\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ncprt\x00\x00\x00\xfc\x00\x00\x002desc\x00\x00\x010\x00\x00\x00kwtpt\x00\x00\x01\x9c\x00\x00\x00\x14bkpt\x00\x00\x01\xb0\x00\x00\x00\x14rTRC\x00\x00\x01\xc4\x00\x00\x00\x0egTRC\x00\x00\x01\xd4\x00\x00\x00\x0ebTRC\x00\x00\x01\xe4\x00\x00\x00\x0erXYZ\x00\x00\x01\xf4\x00\x00\x00\x14gXYZ\x00\x00\x02\x08\x00\x00\x00\x14bXYZ\x00\x00\x02\x1c\x00\x00\x00\x14text\x00\x00\x00\x00Copyright 2000 Adobe Systems Incorporated\x00\x00\x00desc\x00\x00\x00\x00\x00\x00\x00\x11Adobe RGB (1998)\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00XYZ \x00\x00\x00\x00\x00\x00\xf3Q\x00\x01\x00\x00\x00\x01\x16\xccXYZ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00curv\x00\x00\x00\x00\x00\x00\x00\x01\x023\x00\x00curv\x00\x00\x00\x00\x00\x00\x00\x01\x023\x00\x00curv\x00\x00\x00\x00\x00\x00\x00\x01\x023\x00\x00XYZ \x00\x00\x00\x00\x00\x00\x9c\x18\x00\x00O\xa5\x00\x00\x04\xfcXYZ \x00\x00\x00\x00\x00\x004\x8d\x00\x00\xa0,\x00\x00\x0f\x95XYZ \x00\x00\x00\x00\x00\x00&1\x00\x00\x10/\x00\x00\xbe\x9c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'}
Here's the frog.jpg image:
Keywords: Python, ImageMagick, image, image processing, profile, ICC profile, extract, insert, apply, transform, PIL, Pillow, OpenCV, CMS, pyCMS.

jpg won't optimize (jpegtran, jpegoptim)

I have an image and it's a jpg.
I tried running through jpegtran with the following command:
$ jpegtran -copy none -optimize image.jpg > out.jpg
The file outputs, but the image seems un-modified (no size change)
I tried jpegoptim:
$ jpegoptim image.jpg
image.jpg 4475x2984 24bit P JFIF [OK] 1679488 --> 1679488 bytes (0.00%), skipped.
I get the same results when I use --force with jpegoptim except it reports that it's optimized but there is no change in file size
Here is the image in question: http://i.imgur.com/NAuigj0.jpg
But I can't seem to get it to work with any other jpegs I have either (only tried a couple though).
Am I doing something wrong?
I downloaded your image from imgur, but the size is 189,056 bytes. Is it possible that imgur did something to your image?
Anyway, I managed to optimize it to 165,920 bytes using Leanify (I'm the author) and it's lossless.

Resources