Advertisement
apollon314

hide text in an image

Mar 19th, 2023
3,757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. harfler = r"""abcçdefgğhıijklmnoöprsştuüvyzxqw 0123456789!?.:,_-=;()"\/&%+'*$#"""
  2. from PIL import Image
  3. start = '##-start-##'
  4. end = '##-end-##'
  5. def embed(text, imageLoc='image.png', saveLoc='out.png'):
  6.     print(len(text))
  7.     print(text[:1000])
  8.     text = '{}{}{}'.format(start,text,end)
  9.     im = Image.open(imageLoc)
  10.     width, height = im.size
  11.     pixColors = im.load()
  12.     hiddenText = calcText(text)
  13.    
  14.     for h in range(height):
  15.         for w in range(width):
  16.             loc = h*width+w
  17.             if loc < len(text)*2:
  18.                 hR, hG, hB = hiddenText[loc]
  19.                 R,G,B = pixColors[w,h]
  20.                 pixColors[w,h] = R-R%2+hR, G-G%2+hG, B-B%2+hB
  21.             else:
  22.                 im.save(saveLoc, 'PNG')
  23.                 im.close()
  24.                 return loc
  25.  
  26. def calcText(text):
  27.     hiddenText = []
  28.     for harf in text:
  29.         hRGB = "{:06b}".format(harfler.find(harf))
  30.         hRGB1, hRGB2 = hRGB[:3], hRGB[3:]
  31.         hiddenText.append(tuple(map(int,tuple(hRGB1))))
  32.         hiddenText.append(tuple(map(int,tuple(hRGB2))))
  33.     return hiddenText
  34.  
  35. def readImg(imageLoc='out.png'):
  36.     im = Image.open(imageLoc)
  37.     width, height = im.size
  38.     pixColors = im.load()
  39.     hiddens = []
  40.     for h in range(height):
  41.         for w in range(width):
  42.             R,G,B = pixColors[w,h]
  43.             hiddens.append((R%2, G%2, B%2))
  44.     hiddenLetters = [harfler[int(''.join(map(str,hiddens[i]+
  45.                      hiddens[i+1])), 2)] for i in range(0, len(hiddens), 2)]
  46.     hiddenText = ''.join(hiddenLetters)
  47.     startLoc = hiddenText.find(start)+len(start)
  48.     endLoc = hiddenText.find(end)
  49.     hiddenText = hiddenText[startLoc:endLoc]
  50.     return hiddenText
  51.  
  52. def onlyCode(text, imageLoc='image.png', saveLoc='OnlyCode.png'):
  53.     print(len(text))
  54.     print(text[:1000])
  55.     text = '{}{}{}'.format(start,text,end)
  56.     im = Image.open(imageLoc)
  57.     width, height = im.size
  58.     pixColors = im.load()
  59.     hiddenText = calcText(text)
  60.     for h in range(height):
  61.         for w in range(width):
  62.             loc = h*width+w
  63.             if loc < len(text)*2:
  64.                 R, G, B = hiddenText[loc]
  65.                 pixColors[w,h] = R*100, G*100, B*100
  66.             else:
  67.                 im.save(saveLoc, 'PNG')
  68.                 im.close()
  69.                 return loc
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement