Nik333

Untitled

Sep 15th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. from PIL import Image
  2. import math
  3.  
  4.  
  5. def numToHex(num):
  6.     if num < 10:
  7.         return str(num)
  8.     elif num == 10:
  9.         return 'a'
  10.     elif num == 11:
  11.         return 'b'
  12.     elif num == 12:
  13.         return 'c'
  14.     elif num == 13:
  15.         return 'd'
  16.     elif num == 14:
  17.         return 'e'
  18.     elif num == 15:
  19.         return 'f'
  20.  
  21.  
  22. def encode(input):
  23.     string = ""
  24.     inImg = Image.open(input)
  25.     width = inImg.size[0]
  26.     height = inImg.size[1]
  27.     # encode image 2 into a copy of image 1
  28.     for y in range(height):
  29.         for x in range(width):
  30.             pixel = inImg.getpixel((x, y))
  31.             for i in range(3):
  32.                 string += numToHex(pixel[i] // 16)
  33.                 string += numToHex(pixel[i] % 16)
  34.     print(string)
  35.     return string
  36.  
  37.  
  38. def putContents(file, string):
  39.     with open(file, "w+") as f:
  40.         f.write(string)
  41.  
  42.  
  43. putContents("converted.txt", encode("image.png"))
Advertisement
Add Comment
Please, Sign In to add comment