Calculating jpeg size from dimensions and bit depth - jpeg

I have jpeg file with following details
ExampleJPEG File
The file size is 1342 bytes as shown above and also confirmed by Hex Editor.
I was thinking i could also calculate the file size by using its dimensions and bit depth.
i.e Total Bytes = ( Width X Height X bitDepth ) / 8
(28 X 28 X 24) / 8 = 2352 bytes
Why is this difference? The calculation formula says 2352 bytes whereas actual size is 1342 bytes. What am i missing?

When expanded out (decoded) and held as bytes in memory (RAM), the image will indeed occupy 2,352 bytes. But JPEGs are compressed to save space on disk, camera SD cards and when being Emailed and transmitted, so they are smaller and take less space/bandwidth.
If you want to see the pixel intensities in their expanded out RGB format, you can use a tool such as ImageMagick in the Terminal/command-line. Let's make a red image 28x28 - I'll make it in PNG format for now:
magick -size 28x28 xc:red PNG24:image.png
Now, if you want to view the pixels, you can either convert the image to a text dump with ImageMagick like this:
magick image.png -depth 8 -colorspace rgb txt:
Sample Output
# ImageMagick pixel enumeration: 28,28,65535,rgb
0,0: (255,0,0) #FF0000 rgb(255,0,0)
1,0: (255,0,0) #FF0000 rgb(255,0,0)
2,0: (255,0,0) #FF0000 rgb(255,0,0)
3,0: (255,0,0) #FF0000 rgb(255,0,0)
...
...
24,27: (255,0,0) #FF0000 rgb(255,0,0)
25,27: (255,0,0) #FF0000 rgb(255,0,0)
26,27: (255,0,0) #FF0000 rgb(255,0,0)
27,27: (255,0,0) #FF0000 rgb(255,0,0)
Or, if you want to view them in a more raw form of hex:
convert image.png -depth 8 rgb:image.raw
and view image.raw in a hex editor, or use Linux built-in tools:
convert image.png -depth 8 rgb: | xxd -g 3 -c12 | more
00000000: ff0000 ff0000 ff0000 ff0000 ............
0000000c: ff0000 ff0000 ff0000 ff0000 ............
00000018: ff0000 ff0000 ff0000 ff0000 ............
Note that if you check the size of the raw, uncompressed (decoded) image, you will see it happily matches your calculation of 2,352 bytes:
ls -l image.raw
-rw-r--r-- 1 mark staff 2352 27 Aug 09:56 image.raw
Note that I used PNG rather than JPEG above because JPEG is lossy and you don't get back what you save, you get something that looks similar but takes less space - like I said above.
Here is an example. I'll draw a red and a green line on a black background and save as a PNG and count the colours:
magick -size 100x100 xc:black +antialias -fill red -draw "line 5,5 95" -fill lime -draw "line 95,5 5,95" image.png
Now count the colours:
magick -format %k image.png info:
3
Now do exactly he same thing as a lossy JPEG:
magick -size 100x100 xc:black +antialias -fill red -draw "line 5,5 95,95" image.jpg
And count the colours in the JPEG and there are 148 now:
magick -format %k image.jpg info:
148
Keywords: Image Processing, ImageMagick, JPEG, lossy, PNG lossless, prime.

Related

Symbol color in postscript terminal

I've a top view of a pm3d plot in black and white. I am rendering the plot via postscript terminal enhanced eps.
I've to place zeta symbol over the data in white color. I've used
set label "{/Symbol=11 \245} = 1.0" at first 0.25, first 0.75 tc rgbcolor "#FFFFFF"
command. But the symbol is being rendered in black color. My terminal command is
set term postscript eps enhanced color font "Helvectica, 9" size 6.5cm, 6cm.
I've removed the tag color, and yet the color is still black.
What could be the command for a white symbol? I'm using gnuplot version 5.2.
My *.eps output:
/Helvetica findfont 90 scalefont setfont
1.000 UL
LTb
LCb setrgbcolor
1105 1598 M
63 0 V
1.000 UP
stroke
LCb setrgbcolor
644 1547 M
[ [(Symbol) 110.0 0.0 true true 0 (z)]
[(Helvetica) 90.0 0.0 true true 0 ( = 1.0)]
] -36.7 MLshow
I've attached below the image of the output, where I've shown what label appears as (I've moved it to x position 0.5).
Your command works correctly when run here, or at any rate it prints a label in white. Note, however
The symbol \245 renders as ∞ rather than ζ. I think you want \172.
The font name Helvetica is misspelled.
To make sure the label is not hidden by the pm3d surface you should add the "front" attribute.
I split the command into three lines for clarity
set label 1 "{/Symbol=11 \172} = 1.0" at first 0.25, first 0.75
set label 1 tc rgbcolor "#FFFFFF"
set label 1 front
The corresponding code in the *.eps output looks like this:
1.00 1.00 1.00 C
1144 2527 M
[ /Symbol reencodeISO15 def
[(Symbol) 110.0 0.0 true true 0 (z)]
[(Helvetica) 90.0 0.0 true true 0 ( = 1.0)]
] -36.7 MLshow
The first line sets the color to white.
The second line moves to the position on the page.
The following four lines print the label.
Does your output produce a different sequence of PostScript commands? Showing the relevant fragment of the output might help debug the problem.
EDIT
The equivalent section of *.eps output if the terminal is set term postscript eps mono rather than set term postscript eps color:
LCb setrgbcolor
1144 2527 M
[ /Symbol reencodeISO def
[(Symbol) 110.0 0.0 true true 0 (z)]
[(Helvetica) 90.0 0.0 true true 0 ( = 1.0)]
] -36.7 MLshow

Can I set saturation and luminosity while preserving hue in imagemagick?

I'm tring to set the saturation and luminosity channels to a fixed value (100% saturation, 75% luinosity) on an image.
Following this I thought I'd use a evaluate - and this works ok:
convert input.png -colorspace HSL \
-channel B -evaluate multiply 0.80 \
-channel G -evaluate multiply 1.20 \
output.png
but doing this:
convert input.png -colorspace HSL \
-channel G -evaluate set 100 \
-channel B -evaluate set 50 \
output.png
results in a black image.
What am I doing wrong?
What am I doing wrong?
Your setting the channel data to a specific quantum value. A quantum value of 100 is about 1.5% if your working with ImageMagick Q16.
Ensure that the value ends with '%'
convert input.png -colorspace HSL \
-channel G -evaluate set 100% \
-channel B -evaluate set 50% \
output.png
#emcconville is correct. But I will add one more thing. Perhaps you also want to convert back to sRGB colorspace. So
convert lena.png -colorspace HSL \
-channel G -evaluate set 100% +channel \
-channel B -evaluate set 50% +channel \
-colorspace sRGB output2.png

Inexplicable color difference of two images created with imagemagick (convert + montage)

I am in the process of creating several images, containing text and diagrams, using imagemagick and other tools.
At some point I realized that, even though the same instructions were being used in batch with little changes, the colors - defined very precisely by their hex value - change from image to image!
Here is an example (no input file needed) that gives reproduceably wrong (or at least unexpected) results on my machine (Ubuntu 14.04, ImageMagick 6.7.7-10 2017-07-31) [edited per fmw42's suggestion]
convert -size 66x46 -bordercolor "#a0a0a0" -border 2 xc:White -gravity Center -fill "#a0a0a0" -weight 700 -pointsize 24 -annotate 0 "0" a_1.png
convert -size 326x46 -bordercolor "#f0f0f0" -border 2 xc:White -gravity West -fill "#000000" -weight 700 -pointsize 14 -annotate +10+0 "4-Fluoroacetophenone" a_2.png
montage -colorspace sRGB a_1.png a_2.png -tile 2x1 -mode Concatenate -set colorspace sRGB a_3.png
convert -size 66x46 -bordercolor "#a0a0a0" -border 2 xc:White -gravity Center -fill "#a0a0a0" -weight 700 -pointsize 24 -annotate 0 "CN" b_1.png
convert -size 326x46 -bordercolor "#f0f0f0" -border 2 xc:White -gravity West -fill "#000000" -weight 700 -pointsize 14 -annotate +10+0 "trans-3-dimethylaminoacrylonitrile" b_2.png
montage -colorspace sRGB b_1.png b_2.png -tile 2x1 -mode Concatenate -set colorspace sRGB b_3.png
(I have left the original text variables, since I haven't been able to understand what triggers the change of color, and different rendered words give different results)
The 6 istructions above create these 6 images:
a_1.png a_2.png a_3.png
b_1.png b_2.png b_3.png
that appear like this:
As you can see, while a_1 and b_1 share the same color - and a_2 and b_2 as well - a_3 and b_3 (which are composite of a_1+a_2 and b_1+b_2) have different colors! Having added explicit specification of colorspace hasn't helped.
(This is not an artifact of having put the 6 pictures in a single file. The color difference is apparent in the separate files as well)
What is causing this? How can I get consistent colors in imagemagick? As I'm using visual color codes to convey information, I need palettes that I can rely on.
Edit: this does't happen (i.e. colors are consistent) with ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 that unfortunately isn't the version I have on my production machine.
I'm not removing the question as finding a solution within ImageMagick 6.7.7 would help. Or is it just a (known) bug and so the only solution is upgrading?
Your ImageMagick 6.7.7-10 2017-07-31 has been patched numerous times and the latest patch was 2017-07-31. There have been reports of other issues with the patch. So I suspect it had a bad patch and you should inquire of your Linux distribution.
Note that proper Imagemagick syntax for raster images is to read the input image first (or create it) before any settings and operators. So properly, your syntax should be
convert -size 66x46 xc:White -bordercolor "#a0a0a0" -border 2 -gravity Center -fill "#a0a0a0" -weight 700 -pointsize 24 -annotate 0 "0" a_1.png
IM 6 is forgiving, so it likely will not matter. But IM 7 is not as forgiving.
The -set colorspace sRGB is likely un-necessary in your command, but should not hurt. But if used, should be properly placed after creating your input image.
Using Imagemagick 6.9.9.5 Q16 Mac OSX (2017-08-04) and Imagemagick 7.0.6.5 Q16 HDRI, proper syntax should be:
IM 6
convert -size 66x46 xc:White -bordercolor "#a0a0a0" -border 2 -gravity Center -fill "#a0a0a0" -weight 700 -pointsize 24 -annotate 0 "0" a_1.png
convert -size 326x46 xc:White -bordercolor "#f0f0f0" -border 2 -gravity West -fill "#000000" -weight 700 -pointsize 14 -annotate +10+0 "4-Fluoroacetophenone" a_2.png
montage a_1.png a_2.png -tile 2x1 -mode Concatenate -set colorspace sRGB a_3.png
convert -size 66x46 xc:White -bordercolor "#a0a0a0" -border 2 -gravity Center -fill "#a0a0a0" -weight 700 -pointsize 24 -annotate 0 "CN" b_1.png
convert -size 326x46 xc:White -bordercolor "#f0f0f0" -border 2 -gravity West -fill "#000000" -weight 700 -pointsize 14 -annotate +10+0 "trans-3-dimethylaminoacrylonitrile" b_2.png
montage b_1.png b_2.png -tile 2x1 -mode Concatenate -set colorspace sRGB b_3.png
IM 7
magick -size 66x46 xc:White -bordercolor "#a0a0a0" -border 2 -gravity Center -fill "#a0a0a0" -weight 700 -pointsize 24 -annotate 0 "0" a_1.png
magick -size 326x46 xc:White -bordercolor "#f0f0f0" -border 2 -gravity West -fill "#000000" -weight 700 -pointsize 14 -annotate +10+0 "4-Fluoroacetophenone" a_2.png
magick montage -colorspace sRGB a_1.png a_2.png -tile 2x1 -mode Concatenate -set colorspace sRGB a_3.png
magick -size 66x46 xc:White -bordercolor "#a0a0a0" -border 2 -gravity Center -fill "#a0a0a0" -weight 700 -pointsize 24 -annotate 0 "CN" b_1.png
magick -size 326x46 xc:White -bordercolor "#f0f0f0" -border 2 -gravity West -fill "#000000" -weight 700 -pointsize 14 -annotate +10+0 "trans-3-dimethylaminoacrylonitrile" b_2.png
magick montage -colorspace sRGB b_1.png b_2.png -tile 2x1 -mode Concatenate -set colorspace sRGB b_3.png
For both I get:
Is this correct? Do you get something different using my commands?

Is there a difference between computing LAB and RGB avg?

I have a number of RGB pixel sets that I need to compute an average colour for. So far I have simply been averaging each R, G and B value separately to compute the average RGB value and then converting it into LAB for comparison against another colour with the DeltaE 2000 algorithm.
Is there any difference to the final computed LAB average if I instead first convert each individual RGB set to LAB and then average the L, A and B values separately?
Yes, there's a difference. RGB values suffer from color metamerism which is where multiple combinations of R G and B can produce the same perceived color. My hunch is that averaging the RGB values will also get you a washed out version of the color as well.
Here's some rgb metamer examples:
light pink 233 200 244 vs 245 196 234
aqua 40 238 234 vs 99 235 224
light green 21 237 70 vs 78 234 62

Gnuplot histogram with rgb colours from file

I have a file formatted as follows:
A 12.0 255,20,147
B 325.0 255,255,0
C 134456.0 255,255,0
D 13869.0 0,0,0
E 4321.0 255,0,0
F 43676.0 165,42,42
I would like to produce a histogram where $2 is the height of the bar, $1 is the label (i.e.: it is written below the bar, under the x axis) and $3 is the RGB colour of the bar.
I have read this and this but I still can't figure out how to do what I just described.
I found a way to do it: you first have to change the formatting of the rgb colours changing the commas to spaces, so that the new file becomes:
A 12.0 255 20 147
B 325.0 255 255 0
C 134456.0 255 255 0
D 13869.0 0 0 0
E 4321.0 255 0 0
F 43676.0 165 42 42
then you have to instruct gnuplot on how to parse the rgb colours:
rgb(r,g,b) = int(r)*65536 + int(g)*256 + int(b)
finally you can specify that you want column 1 to be the label (xticlabels(1)) and that you want variable colour (lc rgb variable), passing your rgb function the appropriate columns of your file (rgb($3,$4,$5)).
Summing it all up, the following commands worked for me:
set style fill solid
rgb(r,g,b) = int(r)*65536 + int(g)*256 + int(b)
plot 'hist_test.dat' using (column(0)):2:(0.5):(rgb($3,$4,$5)):xticlabels(1) w boxes lc rgb variable;

Resources