Ahmad Uzzam Masood
Ahmad Uzzam Masood

Reputation: 23

Replicating Photoshop's dithering in Python

I want to do the same dithering that Photoshop does in its Save for Web export section. This is my original image: Original image

Diffusion dithering in Photoshop using the following settings:

Photoshop Save for Web

This yields the following image: Photoshop dithering

In Python, I'm using PIL's Image.convert() function with Adaptive color reduction and Floyd-Steinberg algorithm for diffusion dithering. The problem is that Python produces a slightly different dithered image (as shown in the retrieved palette below). The image produced by Python is this: Python dithering

This is my code:

from PIL import Image
MyImageTrueColor = Image.open('vijnbjz_2K_Albedo.jpg')

MyImageDithered = MyImageTrueColor.convert(mode='P',
    colors=64,
    dither=1,
    palette = 1
    )

MyImageDithered.save('dithered.gif')
im2 = MyImageDithered.getpalette()

dithered = []
for i in im2:
    if i != 0:
        dithered.append(i)

palette = []

for i in range(len(dithered)):
    if (i % 3) == 2:
        palette.append(tuple(im2[i - 2:i + 1]))

print("Palette from Python:")
print(len(palette))
print(palette)

print("\n Palette from Photoshop:")

a = Image.open("industrial_storage.gif")
b = a.getpalette()

palette = []

for i in range(len(b)):
    if (i % 3) == 2:
        palette.append(tuple(b[i - 2:i + 1]))

print(len(palette))
print(palette)

Output:

Palette from Python:
64
[(185, 183, 178), (167, 166, 161), (160, 157, 151), (151, 153, 152), (150, 145, 138), (143, 143, 139), (138, 141, 142), (136, 137, 135), (138, 131, 122), (132, 130, 125), (129, 131, 129), (128, 125, 118), (125, 128, 126), (124, 123, 118), (120, 125, 126), (119, 122, 121), (129, 117, 102), (123, 117, 107), (121, 117, 109), (120, 119, 115), (119, 113, 104), (116, 119, 118), (117, 116, 110), (116, 113, 107), (116, 110, 99), (112, 117, 120), (113, 115, 111), (112, 112, 109), (112, 110, 106), (112, 109, 101), (108, 112, 112), (108, 108, 104), (104, 110, 111), (118, 103, 85), (111, 104, 93), (108, 106, 100), (108, 102, 90), (105, 107, 104), (104, 104, 101), (105, 103, 95), (104, 98, 87), (100, 105, 107), (101, 103, 100), (100, 100, 97), (100, 98, 89), (96, 101, 102), (96, 97, 93), (92, 98, 99), (110, 91, 68), (100, 92, 79), (94, 94, 89), (95, 90, 78), (91, 93, 92), (90, 89, 82), (87, 90, 88), (83, 88, 89), (101, 81, 59), (89, 81, 70), (83, 81, 75), (76, 79, 77), (90, 68, 47), (86, 56, 34), (71, 65, 57), (66, 46, 30)]

 Palette from Photoshop:
64
[(119, 120, 118), (98, 74, 49), (134, 122, 109), (80, 53, 32), (87, 88, 86), (102, 99, 88), (100, 89, 76), (118, 115, 106), (136, 136, 135), (175, 174, 171), (136, 132, 121), (152, 153, 151), (87, 83, 72), (115, 107, 102), (54, 37, 23), (116, 105, 87), (107, 115, 117), (90, 99, 100), (169, 164, 155), (72, 71, 67), (151, 148, 139), (107, 107, 98), (123, 132, 134), (167, 155, 142), (104, 107, 115), (107, 107, 107), (218, 213, 205), (119, 123, 132), (99, 99, 99), (121, 96, 63), (149, 140, 132), (83, 74, 64), (87, 90, 99), (99, 107, 107), (140, 148, 150), (74, 82, 84), (136, 140, 148), (192, 185, 176), (203, 197, 188), (90, 99, 90), (107, 115, 107), (107, 94, 67), (99, 107, 99), (153, 156, 165), (99, 99, 107), (128, 114, 87), (163, 165, 165), (107, 99, 99), (123, 132, 123), (72, 74, 82), (56, 67, 70), (74, 82, 74), (48, 53, 57), (140, 148, 140), (65, 62, 52), (156, 165, 156), (182, 185, 191), (197, 199, 199), (115, 107, 115), (188, 190, 188), (132, 123, 123), (107, 99, 107), (148, 140, 148), (99, 90, 90)]

Anyone know how I can get accurate results in Python by using alternative code or any other algorithm other than Floyd-Steinberg dithering?

Upvotes: 0

Views: 178

Answers (0)

Related Questions