Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Why result files hash is not similar to calculated hash from image in memory

$
0
0

I have a code to open a png file then edit its pixels one by one and calculate hash. then save that image to hard disk if its hash is according to what i want.

import hashlibfrom PIL import Imageimport io# original file pathimg_path = "./Sample_Image.png"Target_hash = 'c133bf1fdc914ef930e2ae5101d2e2cb' # target MD5 hashimg = Image.open(img_path)hrange, wrane = img.sizem = hashlib.md5()min10 = 5with io.BytesIO() as memf:    for h in range (hrange):        for w in range (wrane):            img_pixel = img.load()            def_pixel = img_pixel[h, w]            for i in range (def_pixel[0], 256):                for j in range (def_pixel[1], 256):                    for k in range (def_pixel[2], 256):                        img_pixel[h, w] = (i, j, k)                        img.save(memf, 'PNG')                        m.update(memf.getvalue())                        hash = m.hexdigest()                        if (hash == Target_hash):                            new_img_name = './'+ hash + str(h) +'_'+ str(w) +'.png'                            img.save(new_img_name, 'PNG')                            print(hash," > ",new_img_name)                            pass

[What is issue as a diagram] (https://i.stack.imgur.com/VC0of.png)

Code finds a hash but the saved image's hash is not same as found hash(target hash)how I can do this?


Viewing all articles
Browse latest Browse all 23131

Trending Articles