0

python-image-library (i.e. Pillow) is used to load the following JPG.

enter image description here

Here is my code.

from PIL import Image
import numpy as np

rgb_dict = {}

img = Image.open('Capture.JPG')
img.load()
data = np.asarray(img, dtype="int32")
for x in data.tolist():
    for y in x:
        r, g, b = y

        value = f"{r}_{g}_{b}"
        if value not in rgb_dict:
            rgb_dict[value] = 1
        else:
            rgb_dict[value] = rgb_dict[value] + 1
print(rgb_dict)
print(len(rgb_dict))

I am confused why there are so many RGB color code in my result.

Here is the result. There 551 RGB combination (i.e. len(rgb_dict)) in the result dictionary.

I can't understand. Visually check, it is very few colors in the JPG. And this JPG is a screenshot of SVG which is SVG rectangle with CSS styles.

{'255_255_255': 241030, '255_255_253': 38, '254_255_253': 11, '254_255_251': 10, '255_254_255': 100, '255_253_255': 31, '252_255_255': 60, '249_255_255': 23, '254_254_255': 3, '255_252_255': 38, '255_250_255': 6, '255_251_255': 11, '248_255_255': 22, '247_255_253': 1, '247_255_251': 2, '251_255_251': 2, '255_255_250': 31, '255_254_250': 2, '255_253_251': 3, '254_255_255': 81, '248_255_253': 4, '255_255_251': 26, '255_254_251': 5, '245_255_255': 3, '251_255_255': 20, '251_255_253': 7, '255_253_253': 9, '254_255_250': 3, '252_255_250': 4, '255_252_250': 4, '255_251_246': 2, '255_252_246': 1, '255_254_246': 1, '255_254_248': 1, '255_255_248': 2, '255_252_253': 12, '255_251_250': 1, '255_253_246': 1, '255_255_246': 2, '254_255_248': 1, '252_255_251': 3, '242_255_251': 1, '240_255_251': 2, '244_255_251': 1, '244_255_255': 2, '240_255_255': 1, '238_255_255': 2, '241_255_255': 1, '247_255_255': 3, '252_255_253': 1, '243_243_243': 3, '248_248_248': 14, '252_252_252': 1149, '251_251_251': 30, '253_253_253': 1448, '254_252_253': 2, '252_254_253': 2, '250_254_253': 3, '250_255_254': 1, '249_253_252': 21, '250_252_251': 1, '254_254_254': 3090, '254_253_251': 1, '254_252_255': 1, '255_251_253': 5, '255_249_253': 1, '255_250_251': 5, '255_251_251': 4, '253_254_249': 1, '249_255_249': 2, '252_254_251': 1, '244_244_244': 5, '241_241_241': 2, '249_255_253': 1, '253_255_250': 1, '251_255_249': 7, '251_255_250': 7, '255_249_255': 2, '254_253_255': 4, '245_245_245': 7, '255_250_254': 2, '253_251_254': 1, '252_252_254': 2, '251_252_254': 8, '248_252_251': 1, '252_252_250': 5, '255_253_252': 1, '252_246_246': 1, '255_246_247': 1, '253_251_252': 1, '252_253_248': 2, '253_252_248': 2, '250_250_250': 29, '249_249_249': 17, '255_252_252': 1, '255_250_253': 1, '253_254_255': 2, '250_255_255': 1, '253_255_252': 1, '255_250_250': 2, '255_248_250': 2, '247_247_247': 12, '251_255_254': 3, '255_252_251': 3, '254_245_246': 1, '255_248_248': 1, '255_249_251': 2, '255_246_249': 1, '248_255_241': 1, '251_255_236': 1, '255_255_237': 1, '255_253_237': 1, '255_250_239': 1, '255_249_241': 1, '255_251_243': 2, '255_253_244': 1, '253_255_245': 1, '247_255_249': 1, '244_255_250': 1, '244_255_252': 1, '255_252_254': 2, '255_248_255': 3, '238_238_238': 4, '236_236_236': 2, '255_248_251': 1, '255_238_241': 1, '255_241_241': 1, '255_238_237': 14, '255_241_234': 1, '255_245_223': 1, '255_245_220': 1, '255_241_221': 1, '255_237_225': 1, '255_234_227': 1, '255_234_230': 1, '255_238_234': 1, '255_244_237': 1, '247_255_250': 1, '244_255_253': 2, '76_76_76': 1, '62_62_62': 392, '65_60_66': 2, '63_61_66': 1, '62_61_66': 1, '62_62_64': 1, '61_62_64': 1, '61_63_62': 2, '64_64_64': 410, '66_62_63': 2, '72_63_64': 1, '78_63_66': 1, '75_56_58': 1, '70_48_51': 1, '83_56_61': 1, '124_62_67': 1, '168_33_37': 1, '193_23_26': 8, '195_22_26': 7, '190_24_24': 1, '185_27_24': 1, '186_27_24': 1, '197_20_30': 1, '197_19_35': 1, '188_23_39': 1, '169_31_46': 1, '144_43_51': 1, '104_44_44': 1, '80_55_50': 1, '62_63_57': 1, '52_67_60': 1, '52_66_66': 1, '56_63_69': 1, '65_59_71': 1, '66_58_69': 1, '63_61_64': 1, '0_0_0': 5744, '1_0_2': 2, '0_0_4': 1, '0_0_2': 4, '0_1_0': 10, '4_4_4': 38, '3_0_0': 2, '6_0_0': 16, '14_2_4': 1, '11_0_0': 1, '13_0_0': 1, '33_12_17': 1, '109_43_47': 1, '184_31_36': 1, '213_18_24': 7, '214_17_24': 7, '212_18_26': 1, '209_20_27': 1, '209_19_29': 1, '213_16_33': 1, '214_15_34': 1, '210_16_40': 1, '199_21_45': 1, '178_31_50': 1, '154_42_56': 1, '49_0_0': 1, '25_0_0': 1, '0_3_0': 3, '0_4_4': 1, '0_3_7': 1, '0_0_9': 1, '0_0_7': 1, '26_26_26': 11, '40_40_40': 3, '30_30_30': 374, '36_36_36': 9, '33_33_33': 10, '35_35_35': 6, '29_29_29': 229, '31_31_31': 446, '30_32_31': 5, '30_31_33': 1, '33_31_32': 1, '39_35_36': 1, '40_34_36': 1, '36_27_30': 1, '35_26_27': 1, '49_38_42': 1, '86_45_49': 1, '134_33_39': 1, '154_24_32': 7, '155_24_32': 7, '155_23_34': 1, '155_23_36': 1, '155_23_37': 2, '152_24_39': 1, '147_26_43': 1, '138_30_46': 1, '126_36_48': 1, '112_42_52': 1, '65_16_22': 1, '51_22_24': 1, '39_27_29': 1, '25_34_33': 1, '23_35_35': 1, '21_35_36': 1, '24_34_35': 1, '28_32_33': 1, '104_104_104': 1, '126_126_126': 2581, '124_124_124': 597, '129_129_129': 5730, '127_127_127': 5754, '126_128_127': 13, '124_128_127': 6, '129_127_128': 5, '130_128_129': 6, '129_125_126': 1, '126_122_123': 1, '131_127_128': 2, '150_130_131': 1, '169_114_117': 1, '180_110_112': 7, '181_109_113': 7, '183_108_113': 1, '187_106_113': 1, '187_106_115': 1, '186_106_117': 1, '181_108_117': 1, '177_110_119': 1, '170_113_120': 1, '165_116_122': 1, '158_119_124': 1, '147_118_122': 1, '141_121_123': 1, '134_124_125': 1, '128_126_127': 8, '124_128_129': 2, '120_130_129': 1, '117_131_131': 1, '119_131_129': 1, '95_95_95': 5, '125_125_125': 539, '131_131_131': 1107, '132_132_132': 152, '128_130_129': 4, '126_130_129': 3, '132_130_131': 3, '135_131_132': 2, '133_129_130': 2, '130_126_127': 3, '133_125_123': 1, '136_121_114': 1, '139_121_111': 7, '140_120_113': 7, '143_118_111': 1, '149_116_111': 1, '152_114_111': 1, '150_115_113': 1, '147_116_114': 1, '146_116_114': 1, '143_117_116': 1, '140_119_116': 1, '138_120_118': 1, '139_125_124': 1, '136_126_125': 1, '133_127_127': 1, '129_129_131': 1, '126_130_131': 3, '125_130_133': 1, '128_129_131': 1, '94_94_94': 4, '122_122_122': 47, '128_128_128': 7989, '127_125_126': 2, '134_130_131': 1, '127_126_124': 1, '131_132_126': 1, '130_133_124': 14, '131_133_122': 1, '134_132_120': 2, '134_131_122': 2, '134_131_124': 2, '132_131_126': 2, '128_127_123': 1, '128_127_125': 2, '128_126_129': 2, '127_126_131': 16, '127_127_129': 1, '96_96_96': 414, '123_123_123': 49, '126_124_125': 8, '127_127_125': 14, '124_129_123': 1, '119_132_122': 3, '116_134_120': 1, '116_134_122': 2, '120_131_123': 1, '121_131_123': 1, '124_129_125': 1, '126_128_125': 1, '131_125_129': 3, '130_125_129': 1, '100_100_100': 2, '130_130_130': 1595, '135_135_135': 29, '129_131_130': 6, '133_133_133': 52, '134_132_133': 1, '130_128_131': 8, '130_128_133': 7, '119_134_127': 2, '116_136_125': 1, '116_136_127': 1, '118_135_127': 1, '121_134_127': 1, '122_133_127': 1, '123_132_127': 1, '127_132_128': 1, '129_131_128': 1, '131_129_130': 2, '134_128_130': 1, '134_128_132': 1, '133_128_132': 1, '93_93_93': 3, '125_125_127': 1, '129_127_132': 9, '129_126_133': 7, '128_128_130': 2, '127_129_128': 6, '128_124_125': 2, '92_92_92': 1, '124_124_126': 1, '128_126_131': 1, '133_123_131': 1, '134_123_129': 2, '133_124_129': 3, '132_128_129': 2, '102_102_102': 3, '121_121_121': 38, '134_134_134': 47, '118_118_118': 11, '136_136_136': 21, '117_117_117': 10, '120_120_120': 29, '116_116_116': 7, '125_129_130': 1, '124_130_130': 14, '136_123_132': 2, '139_122_132': 2, '138_122_132': 2, '135_124_130': 1, '134_125_130': 1, '132_126_130': 1, '131_126_130': 1, '125_129_128': 2, '119_119_119': 12, '57_57_57': 7, '72_72_72': 10, '65_65_65': 482, '67_67_67': 397, '70_70_70': 15, '63_63_63': 298, '66_66_66': 510, '60_60_60': 8, '61_61_61': 329, '54_54_54': 1, '59_59_59': 8, '71_71_71': 227, '58_58_58': 10, '69_69_69': 20, '68_68_68': 25, '55_55_55': 2, '56_56_56': 5, '64_66_65': 11, '62_66_65': 11, '71_61_69': 2, '72_61_69': 4, '69_63_67': 1, '68_63_67': 2, '66_64_67': 1, '75_75_75': 9, '73_73_73': 7, '5_5_5': 44, '8_8_8': 247, '3_3_3': 62, '1_1_1': 1863, '10_10_10': 26, '12_12_12': 18, '6_6_6': 35, '1_0_0': 7, '6_4_5': 1, '8_6_7': 1, '5_3_4': 2, '0_2_1': 13, '4_0_0': 2, '5_0_0': 7, '7_0_0': 7, '4_0_1': 1, '0_2_3': 3, '0_3_3': 5, '9_9_9': 43, '7_7_7': 19, '2_2_2': 827, '10_8_9': 2, '3_1_2': 1, '0_4_0': 1, '0_6_0': 1, '0_5_2': 1, '0_5_0': 1, '0_4_2': 2, '0_3_2': 2, '0_2_0': 2, '16_16_16': 12, '15_15_15': 8, '17_17_17': 4, '14_14_14': 5, '139_139_139': 10, '141_141_141': 1, '28_28_28': 154, '246_246_246': 11, '23_21_22': 1, '5_1_2': 1, '76_72_73': 1, '242_255_253': 1, '18_18_18': 4, '22_22_22': 9, '179_179_179': 1, '99_99_99': 3, '34_34_34': 12, '113_113_113': 2, '112_112_112': 1, '78_78_78': 3, '97_97_97': 652, '38_38_38': 9, '41_37_38': 1, '55_51_52': 1, '245_243_244': 1, '251_249_250': 1, '251_253_252': 6, '248_254_250': 4, '246_255_248': 1, '246_255_250': 1, '52_52_52': 1, '188_188_188': 2, '13_13_13': 10, '91_91_91': 2, '20_20_20': 4, '32_32_32': 633, '77_77_77': 1, '137_137_137': 13, '31_27_28': 1, '11_7_8': 1, '250_248_249': 1, '255_254_253': 4, '187_187_187': 1, '39_39_39': 3, '98_98_98': 3, '26_24_25': 1, '65_61_62': 1, '252_250_251': 1, '25_25_25': 13, '199_199_199': 2, '66_64_65': 1, '190_190_190': 75, '107_107_107': 1, '79_79_79': 2, '85_85_85': 1, '111_111_111': 3, '101_101_101': 2, '74_74_74': 3, '240_240_240': 3, '195_195_195': 1, '198_198_198': 1, '42_42_42': 6, '37_37_37': 7, '189_189_189': 315, '24_24_24': 5, '11_11_11': 15, '51_51_51': 3, '48_48_48': 5, '49_49_49': 4, '50_50_50': 1, '27_27_27': 12, '23_23_23': 5, '19_19_19': 4, '21_21_21': 3, '114_114_114': 1, '140_140_140': 6, '138_138_138': 3, '144_144_144': 1, '44_44_44': 1, '80_80_80': 4, '235_235_235': 1, '47_47_47': 4, '200_200_200': 1, '46_46_46': 3, '110_110_110': 2, '115_115_115': 4, '83_83_83': 1, '239_239_239': 2, '196_196_196': 3, '41_41_41': 2, '191_191_191': 1, '193_193_193': 1, '45_45_45': 2, '237_237_237': 1, '201_201_201': 2, '185_185_185': 1, '197_197_197': 1, '82_82_82': 1, '43_43_43': 2, '103_103_103': 1, '105_105_105': 1, '242_242_242': 1, '231_231_231': 1}

cdhit
  • 1,384
  • 1
  • 15
  • 38
  • The answer is here... https://stackoverflow.com/a/51994153/2836621 – Mark Setchell Aug 27 '22 at 09:30
  • Also, try to avoid `for` loops and Python lists of pixels. It is horribly slow, inefficient and error-prone. See https://stackoverflow.com/a/59443225/2836621 and https://stackoverflow.com/a/72992518/2836621 – Mark Setchell Aug 27 '22 at 09:35
  • If you want to count/examine the colours in an image with **PIL**, consider using `Image.getcolors()` – Mark Setchell Aug 27 '22 at 09:46
  • Another potential issue is anti-aliasing. When your SVG is rendered to the screen, the renderer may introduce pixels that are half (or some other percentage) of the way between the foreground edge and the background edge to make it look smoother. See ClearType for example https://en.wikipedia.org/wiki/ClearType – Mark Setchell Aug 27 '22 at 20:47

1 Answers1

1

The issue is probably that JPEG is a lossy format - it permits itself to change your data in order to make the file smaller. It tries to do that imperceptibly and succeeds very well for images like real-life photographs, but not so well for computer-generated, or "blocky" images.

Here is an example:

#!/usr/bin/env python3

from PIL import Image, ImageDraw

w, h = 400, 400

# Make solid red image
im   = Image.new('RGB', (w,h), 'red')

print(f'Initial colours: {len(im.getcolors(16000))}')

# Draw blue polygon
draw = ImageDraw.Draw(im)
draw.regular_polygon((w//2, h//2, w//2), 5, fill='blue')
print(f'With polygon colours: {len(im.getcolors(16000))}')

# Save as PNG and JPEG
im.save("result.png")
im.save("result.jpg")

print(f'PNG colours: {len(Image.open("result.png").getcolors(16000))}')
print(f'JPEG colours: {len(Image.open("result.jpg").getcolors(16000))}')

Image

enter image description here

Output

Initial colours: 1
With polygon colours: 2
PNG colours: 2
JPEG colours: 4448

If you zoom in on the left-most vertex of the pentagon, you will see what JPEG has done for you:

enter image description here


Another potential issue is "anti-aliasing" - which is well described here.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432