I just found a piece of code that was used to determine if an image has transparent pixels:
my $alpha = $gd->transparent;
if ($alpha < 0) {
die("The image you uploaded has no transparent pixels. (alpha = $alpha)");
}
Obviously, this does not work. I tried it with the image user-desktop.png of the open icon library which has transparent pixels. The check returned -1.
I can only guess why this command was used. GD's manpage says:
If you call this method [transparent] without any parameters, it will return the current index of the transparent color, or -1 if none.
So, as a side question: the transparent pixels can have no color index at all - right?
Then I found the thread Perl GD check if pixel is transparent. But for this solution, I have to iterate over all pixels of an image (at least in the wort case, when the only transparent pixel would be the last one).
Isn't there an easy way of checking for this information? Maybe a method like $image_object->has_transparent_pixels ?
NB: I'm not bound to GD, so other Image modules might work as well (however, I'm on Windows - it should work there).
Updated Answer
As pointed out by #ikegami (thank you), GIFs have a designated transparent "colour" pixel in their palette rather than a alpha/transparency layer in which each pixel has its own transparency value, normally between 0-255.
I have generated a 2x2 pixel GIF with one transparent pixel, one red, one green and one blue - then ran ImageMagick's identify command against it and got the following. I have marked the transparent pixel parts with an arrow.
Image: a.gif
Format: GIF (CompuServe graphics interchange format)
Mime type: image/gif
Class: PseudoClass
Geometry: 4x4+0+0
Units: Undefined
Type: PaletteAlpha
Endianess: Undefined
Colorspace: sRGB
Depth: 8/1-bit
Channel depth:
red: 1-bit
green: 1-bit
blue: 1-bit
alpha: 1-bit
Channel statistics:
Pixels: 16
Red:
min: 0 (0)
max: 255 (1)
mean: 127.5 (0.5)
standard deviation: 127.5 (0.5)
kurtosis: -2
skewness: 0
Green:
min: 0 (0)
max: 255 (1)
mean: 127.5 (0.5)
standard deviation: 127.5 (0.5)
kurtosis: -2
skewness: 0
Blue:
min: 0 (0)
max: 255 (1)
mean: 127.5 (0.5)
standard deviation: 127.5 (0.5)
kurtosis: -2
skewness: 0
Alpha:
min: 0 (0)
max: 255 (1)
mean: 191.25 (0.75)
standard deviation: 110.418 (0.433013)
kurtosis: -0.666667
skewness: 1.1547
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 111.562 (0.4375)
standard deviation: 123.451 (0.484123)
kurtosis: -1.8275
skewness: 0.271109
Alpha: srgba(255,255,255,0) #FFFFFF00
Colors: 4
Histogram:
4: ( 0, 0,255,255) #0000FF blue
4: ( 0,255, 0,255) #00FF00 lime
4: (255, 0, 0,255) #FF0000 red
4: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0)
Colormap entries: 4
Colormap:
0: (255, 0, 0,255) #FF0000 red
1: ( 0,255, 0,255) #00FF00 lime
2: ( 0, 0,255,255) #0000FF blue
3: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0) <---------
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: srgba(255,255,255,0)
Border color: srgba(223,223,223,1)
Matte color: grey74
Transparent color: srgba(255,255,255,0) <---------------
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 4x4+0+0
Dispose: Undefined
Compression: LZW
Orientation: Undefined
Properties:
date:create: 2014-10-11T10:40:09+01:00
date:modify: 2014-10-11T10:40:08+01:00
signature: 1c82b4c2e772fb075994516cc5661e9dec35b8142f89c651253d07fc3c4642bb
Profiles:
Profile-gif:xmp dataxmp: 1031 bytes
Artifacts:
filename: a.gif
verbose: true
Tainted: False
Filesize: 1.11KB
Number pixels: 16
Pixels per second: 16PB
User time: 0.000u
Elapsed time: 0:01.000
Version: ImageMagick 6.8.9-7 Q16 x86_64 2014-09-10 http://www.imagemagick.org
So, IM does know about the GIF transparent pixel - I will dig some more and see if it can be found sensibly in Perl - for now though, you could just run the following in Perl's backticks.
my $output = `identify -verbose a.gif | grep -i transparent`; # or maybe with FINDSTR on Windows
As an alternatvie, and less problematic across platforms, the %A escape tells you if an image has transparency enabled:
convert a.gif -print "%A" null:
True
convert a.jpg -print "%A" null:
False
Or, in a more Perl-y way:
#!/usr/bin/perl
use warnings;
use strict;
use Image::Magick;
my $image = Image::Magick->new();
$image->Read($ARGV[0]);
my $a = $image->Get('%A');
print $a;
perl ./script.pl a.gif
True
perl ./script.pl a.jpg
False
Original Answer
I think you may be a little confused about transparency. Images either have transparency, which is an entire layer, or they do not. In general, it is not a question of a single pixel being transparent or not. From the outset, JPEGs do not support transparency, GIF and PNG can support transparency but they are not necessarily always transparent.
So, assuming you have a PNG or a GIF, it could have a transparency layer. If it has, each pixel could either be totally transparent, totally opaque or somewhere in between. If you use ImageMagick, it is available at the command line or with PHP, Perl and other bindings.
From the command line, you can tell if an image has a transparency layer using this command:
convert InputImage.png -format "%[opaque]" info:
and it will either return true or false.
In Perl you can do this:
#!/usr/bin/perl
use warnings;
use strict;
use Image::Magick;
my $image = Image::Magick->new();
$image->Read($ARGV[0]);
my $a = $image->Get('%[opaque]');
print $a;
then run as:
perl ./script.pl ImageName.png
Related
I'm trying to alter the displayed boot splash of an AMLogic device (using this tool: https://github.com/steeve/aml-imgpack). I have a JPG which I convert to BMP, as the device needs (let's say https://unsplash.com/photos/SnXIF8_2oPw) but the colors are completely wrong, the image is broken in "lines" and some parts from left are to the right (a bit like repeating the image, but not completely).
The original image, have the following attributes:
$ magick identify -verbose ../original/bootup.bmp
Image:
Filename: ../original/bootup.bmp
Format: BMP (Microsoft Windows bitmap image)
Class: DirectClass
Geometry: 1920x1080+0+0
Resolution: 28.34x28.34
Print size: 67.7488x38.1087
Units: PixelsPerCentimeter
Colorspace: sRGB
Type: TrueColor
Base type: Undefined
Endianness: Undefined
Depth: 8-bit
Channel depth:
Red: 8-bit
Green: 8-bit
Blue: 8-bit
Channel statistics:
Pixels: 2073600
Red:
min: 0 (0)
max: 255 (1)
mean: 4.64305 (0.018208)
standard deviation: 31.5717 (0.12381)
kurtosis: 47.4739
skewness: 6.93758
entropy: 0.0513943
Green:
min: 0 (0)
max: 255 (1)
mean: 3.77976 (0.0148226)
standard deviation: 26.4192 (0.103605)
kurtosis: 55.8096
skewness: 7.40689
entropy: 0.0502694
Blue:
min: 0 (0)
max: 239 (0.937255)
mean: 1.81429 (0.00711485)
standard deviation: 13.2764 (0.0520643)
kurtosis: 84.5432
skewness: 8.63456
entropy: 0.0528362
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 3.41236 (0.0133818)
standard deviation: 23.7558 (0.0931598)
kurtosis: 67.5353
skewness: 8.07788
entropy: 0.0515
Rendering intent: Perceptual
Chromaticity:
red primary: (0,0)
green primary: (0,0)
blue primary: (0,0)
white point: (0.3127,0.329)
Matte color: grey74
Background color: white
Border color: srgb(223,223,223)
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 1920x1080+0+0
Dispose: Undefined
Iterations: 0
Compression: Undefined
Orientation: Undefined
Convex hull: 931,391 932,391 1111,432 1223,484 1230,488 1247,544 1247,549 1244,551 1015,623 919,623 704,551 688,471 688,448 696,432 931,391
Minimum bounding box: 1247,391 1247,623 688,623 688,391
Properties:
date:create: 2020-10-31T22:40:38+00:00
date:modify: 2020-10-31T22:40:38+00:00
minimum-bounding-box:_p: 931,391
minimum-bounding-box:_q: 932,391
minimum-bounding-box:_v: 1015,623
minimum-bounding-box:angle: 0
minimum-bounding-box:area: 129688
minimum-bounding-box:height: 559
minimum-bounding-box:unrotate: -0
minimum-bounding-box:width: 232
signature: 31151e021e5d4cdde8d345165d8eb9cd6489f307272c8ca455d038002885d405
Artifacts:
verbose: true
Tainted: False
Filesize: 3.95515MiB
Number pixels: 2073600
Pixels per second: 79.4977MP
User time: 0.020u
Elapsed time: 0:01.026
Version: ImageMagick 7.0.10-30 Q16 x86_64 2020-09-20 https://imagemagick.org
I tried to convert using:
$ mogrify -format bmp ../new_bootup.jpg
$ mogrify -format bmp -type TrueColor ../new_bootup.jpg
$ mogrify -format bmp -define bmp:subtype=RGB565 ../new_bootup.jpg
but the image did not show correctly. The question is: which flags should I use (or which tool should I use) to achieve as close relpication of the original attributes as possible?
I'm processing two images from a HTML5 canvas, exporting it into a base64 using toDataUrl() and generating those images files, they have the same amount of pixels and same resolution, but one image file has 3x size than the other one.
Here is the Small sized image(1MB):
And here is the one with big size(3MB):
As you can see, the only difference is the black dots in the grid.
Here are the image magick's indentify verbose output.
To the non-dotted image:
Image: pontoSem.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 1805x1520+0+0
Resolution: 72x72
Units: PixelsPerInch
Type: TrueColor
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
Pixels: 2743600
Red:
min: 0 (0)
max: 255 (1)
mean: 239.227 (0.938144)
standard deviation: 50.1041 (0.196486)
kurtosis: 9.08055
skewness: -3.19968
Green:
min: 0 (0)
max: 255 (1)
mean: 241.654 (0.947662)
standard deviation: 44.0232 (0.17264)
kurtosis: 9.84544
skewness: -3.28708
Blue:
min: 0 (0)
max: 255 (1)
mean: 239.038 (0.937403)
standard deviation: 50.3878 (0.197599)
kurtosis: 8.93501
skewness: -3.18354
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 239.973 (0.94107)
standard deviation: 48.2611 (0.189259)
kurtosis: 9.42491
skewness: -3.24166
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgb(223,223,223)
Matte color: grey74
Transparent color: black
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 1805x1520+0+0
Dispose: Undefined
Iterations: 0
Compression: JPEG
Quality: 100
Orientation: Undefined
Properties:
date:create: 2017-03-02T16:56:35-03:00
date:modify: 2017-03-02T16:52:35-03:00
jpeg:colorspace: 2
jpeg:sampling-factor: 2x2,1x1,1x1
signature: 23b092dd21773df0a63a3fffe8241f9916354e3d29638979d672e9cb91026c25
Artifacts:
filename: pontoSem.jpg
verbose: true
Tainted: False
Filesize: 1.063MB
Number pixels: 2.744M
Pixels per second: 45.73MB
User time: 0.040u
Elapsed time: 0:01.060
Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-02-22
And here is the identify to the largest image (the dotted one)
Image: pontoCom.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 1805x1520+0+0
Resolution: 72x72
Print size: 25.0694x21.1111
Units: PixelsPerInch
Type: TrueColor
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
Pixels: 2743600
Red:
min: 0 (0)
max: 255 (1)
mean: 239.227 (0.938144)
standard deviation: 50.1041 (0.196486)
kurtosis: 9.08055
skewness: -3.19968
Green:
min: 0 (0)
max: 255 (1)
mean: 241.654 (0.947662)
standard deviation: 44.0232 (0.17264)
kurtosis: 9.84544
skewness: -3.28708
Blue:
min: 0 (0)
max: 255 (1)
mean: 239.038 (0.937403)
standard deviation: 50.3878 (0.197599)
kurtosis: 8.93501
skewness: -3.18354
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 239.973 (0.94107)
standard deviation: 48.2611 (0.189259)
kurtosis: 9.42491
skewness: -3.24166
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgb(223,223,223)
Matte color: grey74
Transparent color: black
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 1805x1520+0+0
Dispose: Undefined
Iterations: 0
Compression: JPEG
Quality: 100
Orientation: Undefined
Properties:
date:create: 2017-03-02T16:56:35-03:00
date:modify: 2017-03-02T16:52:35-03:00
jpeg:colorspace: 2
jpeg:sampling-factor: 2x2,1x1,1x1
signature: 23b092dd21773df0a63a3fffe8241f9916354e3d29638979d672e9cb91026c25
Artifacts:
filename: pontoSem.jpg
verbose: true
Tainted: False
Filesize: 1.063MB
Number pixels: 2.744M
Pixels per second: 45.73MB
User time: 0.040u
Elapsed time: 0:01.060
Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-02-22
I just would like to know why the add of some black dots triple the file size, because they have the same number of pixels, and the sape dpi resolution.
Thanks in advice :)
Jpeg format uses some clever tricks to reduce image size with respect to a raw bitmap image. Those techniques have different success depending on the particular image being represented and generally work better when there are no sharp differences between pixels that are close together. The black dots on white background are exactly the kind of thing that makes it difficult.
In the first image, lots of space can be described as "this area is all white" while the second one needs a much longer description mentioning the color of each pixel wherever those black pixels are near, which is almost everywhere
It's due to compression, the white squares on the first image is much more easily compressed than the second image with more randomness, as the image descriptor says it's using JPEG compression.
As most part of the surface area is using this background, it can easily impact in overall size as it compromises the compression algorithm efficiency.
The jpeg image was decoded in a string saved in uploaded_io:
uploaded_io = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2....."
In rails 4 app, the image magick library has been installed and gem mini_magick is in Gemfile. From uploaded_io, we need to retrieve content_type, size and file name (no need to resize and manipulation of the image). We tried to use Ruby's base64 module in controller and there is no success (error and what decoded64 returns is not an image file):
require 'base64'
img = Base64.decode64(uploaded_io)
type = img.content_type #error
size = img.size #error
There is a similar post . But the ActiveSupport::Base64 is discontinued in Rails 4 and it can't be applied to Rails 4.
Not speaking Ruby or Rails, I don't really understand your question, but I think you are trying to get information about a base64-encoded image and I can maybe help with that. I am afraid I can only come towards you from the command-line side of things, but hopefully you can Ruby-ize that...
Let's create a little image:
convert -size 50x50 xc:red xc:lime xc:blue +append image.gif
To get information about an image with ImageMagick, you generally use the identify program from the IM suite, like this:
identify -verbose image.gif
Image: image.gif
Format: GIF (CompuServe graphics interchange format)
Mime type: image/gif
Class: PseudoClass
Geometry: 150x50+0+0
Units: Undefined
Type: Palette
Endianess: Undefined
Colorspace: sRGB
Depth: 8/1-bit
Channel depth:
red: 1-bit
green: 1-bit
blue: 1-bit
Channel statistics:
Pixels: 7500
Red:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Green:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Blue:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Colors: 3
Histogram:
2500: ( 0, 0,255) #0000FF blue
2500: ( 0,255, 0) #00FF00 lime
2500: (255, 0, 0) #FF0000 red
Colormap entries: 4
Colormap:
0: (255, 0, 0) #FF0000 red
1: ( 0,255, 0) #00FF00 lime
2: ( 0, 0,255) #0000FF blue
3: ( 0, 0, 0) #000000 black
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: red
Border color: srgb(223,223,223)
Matte color: grey74
Transparent color: black
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 150x50+0+0
Dispose: Undefined
Compression: LZW
Orientation: Undefined
Properties:
date:create: 2015-11-17T10:07:39+00:00
date:modify: 2015-11-17T10:07:39+00:00
signature: 48d1b973cce66f9a19f3b3738773a30f0519438d893cdfff2223e1941589e008
Artifacts:
filename: image.gif
verbose: true
Tainted: False
Filesize: 325B
Number pixels: 7.5K
Pixels per second: 7.5EB
User time: 0.000u
Elapsed time: 0:01.000
Version: ImageMagick 6.9.2-6 Q16 x86_64 2015-11-15 http://www.imagemagick.org
If I want to base64 encode that, I use openssl like this:
openssl enc -base64 -in image.gif
R0lGODlhlgAyAPEAAP8AAAD/AAAA/wAAACH5BAAAAAAALAAAAACWADIAAAL+hI+p
y50Bo5y02juF3rz7D4adQ5YmgqXqKonuC3PnTD/sjbfxzsv1f8oJb73iDogkDZcp
o9OVjC6Y1Mrz+pFqD9VuBAvebLdebzg81pa7Z3Bauq62se9onDq/1pN3Zv65h9S3
9OcUCDQ4VGh0+JMotFjUWPOYE9kzSVOJc8mTObNJ1BnzGRS6MkpaWnKKmvqyytra
9AoV6zBLWxtyi5t7sSvS2/ALHAwyzFBscYycrLBs1ezxDB2dMT1SjXKtky22zdX9
9Q0eDjBOXi5wbpAOsa7Rjv4ez97+HmA/n7+PXx+PH8B1AtP5O9cv4D+DChEOLFdw
3MFwCQkulNiQ4sMXbxG7TdxWEeJFjxlBbszW8drHaiG/FQAAOw==
The point of my post is to show that you can then pump that base64 mess into identify to interpret it, like this:
identify -verbose "data:image/gif;base64,R0lGODlhlgAyAPEAAP8AAAD/AAAA/wAAACH5BAAAAAAALAAAAACWADIAAAL+hI+p
> y50Bo5y02juF3rz7D4adQ5YmgqXqKonuC3PnTD/sjbfxzsv1f8oJb73iDogkDZcp
> o9OVjC6Y1Mrz+pFqD9VuBAvebLdebzg81pa7Z3Bauq62se9onDq/1pN3Zv65h9S3
> 9OcUCDQ4VGh0+JMotFjUWPOYE9kzSVOJc8mTObNJ1BnzGRS6MkpaWnKKmvqyytra
> 9AoV6zBLWxtyi5t7sSvS2/ALHAwyzFBscYycrLBs1ezxDB2dMT1SjXKtky22zdX9
> 9Q0eDjBOXi5wbpAOsa7Rjv4ez97+HmA/n7+PXx+PH8B1AtP5O9cv4D+DChEOLFdw
> 3MFwCQkulNiQ4sMXbxG7TdxWEeJFjxlBbszW8drHaiG/FQAAOw=="
Image:
Base filename: FQAAOw==
Format: GIF (CompuServe graphics interchange format)
Mime type: image/gif
Class: PseudoClass
Geometry: 150x50+0+0
Units: Undefined
Type: Palette
Endianess: Undefined
Colorspace: sRGB
Depth: 8/1-bit
Channel depth:
red: 1-bit
green: 1-bit
blue: 1-bit
Channel statistics:
Pixels: 7500
Red:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Green:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Blue:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Colors: 3
Histogram:
2500: ( 0, 0,255) #0000FF blue
2500: ( 0,255, 0) #00FF00 lime
2500: (255, 0, 0) #FF0000 red
Colormap entries: 4
Colormap:
0: (255, 0, 0) #FF0000 red
1: ( 0,255, 0) #00FF00 lime
2: ( 0, 0,255) #0000FF blue
3: ( 0, 0, 0) #000000 black
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: red
Border color: srgb(223,223,223)
Matte color: grey74
Transparent color: black
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 150x50+0+0
Dispose: Undefined
Compression: LZW
Orientation: Undefined
Properties:
date:create: 2015-11-17T10:12:05+00:00
date:modify: 2015-11-17T10:12:05+00:00
signature: 48d1b973cce66f9a19f3b3738773a30f0519438d893cdfff2223e1941589e008
Artifacts:
filename: data:image/gif;base64,R0lGODlhlgAyAPEAAP8AAAD/AAAA/wAAACH5BAAAAAAALAAAAACWADIAAAL+hI+p
y50Bo5y02juF3rz7D4adQ5YmgqXqKonuC3PnTD/sjbfxzsv1f8oJb73iDogkDZcp
o9OVjC6Y1Mrz+pFqD9VuBAvebLdebzg81pa7Z3Bauq62se9onDq/1pN3Zv65h9S3
9OcUCDQ4VGh0+JMotFjUWPOYE9kzSVOJc8mTObNJ1BnzGRS6MkpaWnKKmvqyytra
9AoV6zBLWxtyi5t7sSvS2/ALHAwyzFBscYycrLBs1ezxDB2dMT1SjXKtky22zdX9
9Q0eDjBOXi5wbpAOsa7Rjv4ez97+HmA/n7+PXx+PH8B1AtP5O9cv4D+DChEOLFdw
3MFwCQkulNiQ4sMXbxG7TdxWEeJFjxlBbszW8drHaiG/FQAAOw==
verbose: true
Tainted: False
Filesize: 325B
Number pixels: 7.5K
Pixels per second: 7.5EB
User time: 0.000u
Elapsed time: 0:01.000
Version: ImageMagick 6.9.2-6 Q16 x86_64 2015-11-15 http://www.imagemagick.org
[1]: http://i.stack.imgur.com/j9Ei0.gif
Or, you can use convert and re-create the image from its base64-encoded state like this:
{ echo "data:image/png;base64,"; openssl enc -base64 -in image.png; } | convert inline:- decoded.gif
I have a directory of images in png format, which use a fixed color index for their encoding. I'm interested in producing images whose grayscale values correspond to the index itself, not the color for that index.
For example, suppose the color index has the entry index 3 -> (255, 0, 0) (red). I want to replace every instance of (255, 0, 0) in the RGB image with the grayscale value of 3.
My first thought is to 1) hard code the reversed color index into a lookup table 2) load the image, 3) iterate over pixels, doing a replace based on the look up table.
The problem with this is hard coding the lookup table. I can get it (via imagemagick identify), but that's tedious. Are there any libraries that can do this for me? I'm looking for either 1) cmd line transformations or 2) code libraries that get the color index for a pixel.
If you open a palettised image in PHP using GD, but accidentally forget to tell GD that it is palettised, you will actually get the palette index in the blue pixel. So you could take advantage of that to create a grayscale image by creating a truecolour image the same size as your original and then writing the palette index three times, once each for the Red, Green and Blue channels. It's easier than it sounds, here is the code:
#!/usr/local/bin/php
<?php
// Read in a palettised image
$im = imagecreatefrompng("pal.png");
// Create output image same size
$out = imagecreatetruecolor(imagesx($im), imagesy($im));
for ($x = 0; $x < imagesx($im); $x++) {
for ($y = 0; $y < imagesy($im); $y++) {
$pixel = imagecolorat($im, $x, $y);
$index = $pixel & 0xFF;
$outCol = imagecolorallocate($out,$index,$index,$index);
imagesetpixel($out,$x,$y,$outCol);
// printf("[%d,%d] Palette index:%d\n",$x,$y,$index);
}
}
imagepng($out,"result.png");
?>
So, if I create a 3 pixel palettised image with ImageMagick like this:
convert xc:red xc:lime xc:blue +append pal.png
and check it has palette like this
identify -verbose pal.png | more
Image: pal.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: PseudoClass
Geometry: 3x1+0+0
Units: Undefined
Type: Palette <--- it has a palette
Endianess: Undefined
Colorspace: sRGB
Depth: 8/1-bit
Channel depth:
red: 1-bit
green: 1-bit
blue: 1-bit
Channel statistics:
Pixels: 3
Red:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Green:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Blue:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 85 (0.333333)
standard deviation: 120.208 (0.471405)
kurtosis: -1.5
skewness: 0.707107
entropy: 0.918296
Colors: 3
Histogram:
1: ( 0, 0,255) #0000FF blue
1: ( 0,255, 0) #00FF00 lime
1: (255, 0, 0) #FF0000 red
Colormap entries: 4
Colormap: <--- here is the palette
0: (255, 0, 0) #FF0000 red
1: ( 0,255, 0) #00FF00 lime
2: ( 0, 0,255) #0000FF blue
3: (255,255,255) #FFFFFF white
Then check the result.png after running the PHP script, it now looks like this - i.e. greyscale and the colours match the former indices.
identify -verbose result.png
Image: result.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 3x1+0+0
Units: Undefined
Type: Grayscale <--- it is now greyscale, no palette
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
gray: 8-bit
Channel statistics:
Pixels: 3
Gray:
min: 0 (0)
max: 2 (0.00784314)
mean: 1 (0.00392157)
standard deviation: 0.816497 (0.00320195)
kurtosis: -1.5
skewness: 0
entropy: 1
Colors: 3
Histogram:
1: ( 0, 0, 0) #000000 gray(0)
1: ( 1, 1, 1) #010101 gray(1)
1: ( 2, 2, 2) #020202 gray(2)
Note that if your original image had very few colour palette entries, the output image will be dark so you will want to contrast-stretch or normalize it...
ImageMagick's identify command line tool provides information on sampled images contained in image files. I'm trying to understand what the "16" means in the context of what it reports back.
I'm pretty sure this is a monochrome image with just one bit of content and one transparency bit per pixel, so I don't understand the role the "16" plays here. Is it that the 1 represents a grey level "value" of 65535 (i.e. all white, in this case)? Or does this have something to do with the 1-bit of content channel and 1-bit of alpha/transparency channel being packed into a 16-bit word?
In non-verbose mode, it reports:
20140123124823096.pdf PDF 1008x612 1008x612+0+0 **16-bit Bilevel Gray** 77.2KB 0.000u 0:00.000
In verbose mode, it reports the following:
Image: 20140123124823096.pdf
Format: PDF (Portable Document Format)
Mime type: application/pdf
Class: DirectClass
Geometry: 1008x612+0+0
Resolution: 72x72
Print size: 14x8.5
Units: Undefined
Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: Gray
Depth: 16/1-bit
Channel depth:
gray: 1-bit
alpha: 1-bit
Channel statistics:
Gray:
min: 0 (0)
max: 65535 (1)
mean: 60686.5 (0.926017)
standard deviation: 17153.4 (0.261744)
kurtosis: 8.59646
skewness: -3.25522
Alpha:
min: 65535 (1)
max: 65535 (1)
mean: 65535 (1)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
Colors: 2
Histogram:
45640: ( 0, 0, 0,65535) #000000000000 graya(0,1)
571256: (65535,65535,65535,65535) #FFFFFFFFFFFF graya(255,1)
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: graya(255,1)
Border color: graya(223,1)
Matte color: graya(189,1)
Transparent color: graya(0,0)
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 1008x612+0+0
Dispose: Undefined
Iterations: 0
Compression: Undefined
Orientation: Undefined
Properties:
date:create: 2014-01-23T12:41:20-08:00
date:modify: 2014-01-23T12:41:20-08:00
pdf:HiResBoundingBox: 1008x612+0+0
pdf:Version: PDF-1.4
signature: 94e55396cd1b4dea139dcb39458ada0c904d80810722d7c03a62691d710755ab
Artifacts:
filename: 20140123124823096.pdf
verbose: true
Tainted: True
Filesize: 53.1KB
Number pixels: 617K
Pixels per second: 15.42MB
User time: 0.040u
Elapsed time: 0:01.039
Version: ImageMagick 6.8.7-3 2013-11-20 Q16 http://www.imagemagick.org
Update: It turns out that the information that ImageMagick's tool is reporting is not actually the information about the image in the PDF file, but apparently the information about the image after it's gone through some kind of internal transformation! I looked at the PDF file in a text editor and can see that it's unambiguously a 1-bit-per-pixel monochrome image at 200 dpi (legal size), rather than the 72 dpi image with an alpha channel that ImageMagick was reporting. All that said, I'm still curious as to what the 16 means even in ImageMagick's artificial world.
Here's a description of the image as it actually exists in the PDF:
<</Type/XObject
/Subtype/Image
/Width 2800
/Height 1700
/BitsPerComponent 1
/ColorSpace/DeviceGray
/Filter /CCITTFaxDecode
/DecodeParms <</Columns 2800 /Rows 1700>>
/Length 142719
>>