Skip to content

Instantly share code, notes, and snippets.

@ehmo
Last active August 20, 2021 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehmo/7f515ac6461c1c4d3e5a74f12e6eb5ea to your computer and use it in GitHub Desktop.
Save ehmo/7f515ac6461c1c4d3e5a74f12e6eb5ea to your computer and use it in GitHub Desktop.
from PIL import Image
import imagehash
import copy
import time
def change_pixel(img, pix, position, color):
if img.mode == '1':
value = int(color >= 127) # Black-and-white (1-bit)
elif img.mode == 'L':
value = color # Grayscale (Luminosity)
elif img.mode == 'RGB':
value = (color, color, color)
elif img.mode == 'RGBA':
value = (color, color, color, 255)
elif img.mode == 'P':
raise NotImplementedError("TODO: Look up nearest color in palette")
else:
raise ValueError("Unexpected mode for PNG image: %s" % img.mode)
pix[position] = value
return pix
# img1 = Image.open('pic.jpg')
img1 = Image.new("RGB", (960, 960), (255, 255, 255))
img2 = copy.deepcopy(img1)
original_img2 = copy.deepcopy(img2)
img1.save("{}.jpg".format(imagehash.phash(img1)))
img1 = img1.resize((12, 12))
hash1 = imagehash.phash(img1)
width, height = img2.size
for w in range(width):
for h in range(height):
pix = img2.load()
color = 0
position = (h,w)
print(position)
pix = change_pixel(img2, pix, position, color)
# if we found image that doesn't match, we want to actually save it
preserve_img = copy.deepcopy(img2)
img2 = img2.resize((12, 12))
hash2 = imagehash.phash(img2)
print(hash1)
print(hash2)
print(hash1 == hash2)
if hash1 != hash2:
preserve_img.save("{}.jpg".format(hash2))
img2 = copy.deepcopy(original_img2)
from PIL import Image
import imagehash
import sys
cmd_in = sys.argv
if len(cmd_in) != 3:
raise ValueError("You only provided {} arugments insteads of 2".format(len(cmd_in)))
img1 = Image.open(cmd_in[1])
width, height = img1.size
if width < 960 or height < 960:
raise ValueError("Image can't be smaller than 960x960 but yours is: {}x{}".format(width, height))
img2 = Image.open(cmd_in[2])
width, height = img2.size
if width < 960 or height < 960:
raise ValueError("Image can't be smaller than 960x960 but yours is: {}x{}".format(width, height))
img1 = img1.resize((12,12))
img2 = img2.resize((12,12))
hash1 = imagehash.phash(img1)
hash2 = imagehash.phash(img2)
print(hash1, hash2, hash1 == hash2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment