如何更快地将数据插入像素?

qq24tv8q  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(306)

我目前正在开发一个立体图像应用程序,我正在获取每个像素值并将数据一个一个地嵌入其中。这种顺序处理需要很长时间来处理,
守则:

  1. import config_loader
  2. import numpy as np
  3. from PIL import Image
  4. import encryption
  5. import time
  6. def byte2bin(bytestring):
  7. # print("\n from byte 2 bin\n")
  8. # print(bytestring)
  9. bitstring = bin(int.from_bytes(bytestring, byteorder="big"))
  10. return bitstring[2:]
  11. def insert_data_in_pixel(raw_data, string, ptr, bits=1): # this function takes a pixel's data and then converts it to
  12. # binary and then change the last bit to the secret
  13. color = bin(int(raw_data))[2:]
  14. # old = color # troubleshooting lines
  15. color = color[:len(color) - bits]
  16. color = color + string[ptr: ptr + bits]
  17. # print("original-> ", old,"| |added bits ",string[ptr: ptr+bits],"| |Modified-> ", color) # troubleshooting lines
  18. return np.uint8(int(color, 2))
  19. def insert_length(length, new_img): # inserts length of our secret and the length itself is obfuscated
  20. secret_string_len = '<l>' + str(int(length / 4) + 16) + '<l>' # Added ambiguity
  21. secret_string_len = ''.join(format(_, '08b') for _ in bytearray(str(secret_string_len), encoding='utf-8'))
  22. length = len(secret_string_len)
  23. str_len_ptr = 0
  24. for y in range(length):
  25. x = 0
  26. if str_len_ptr < length:
  27. new_img[x][y][0] = insert_data_in_pixel(new_img[x][y][0], secret_string_len, str_len_ptr, bits=3)
  28. str_len_ptr += 3
  29. if str_len_ptr == length:
  30. break
  31. new_img[x][y][1] = insert_data_in_pixel(new_img[x][y][1], secret_string_len, str_len_ptr, bits=3)
  32. str_len_ptr += 3
  33. if str_len_ptr == length:
  34. break
  35. new_img[x][y][2] = insert_data_in_pixel(new_img[x][y][2], secret_string_len, str_len_ptr, bits=2)
  36. str_len_ptr += 2
  37. if str_len_ptr == length:
  38. break
  39. def secret_Loader(): # loads secret from a file
  40. with open('Message.txt', 'r', encoding='utf-8', errors='ignore') as file:
  41. lines = file.readlines()
  42. message = ''.join(lines)
  43. key = config_loader.read('''data['key']''')
  44. # print(key)
  45. enc_message = encryption.encrypt(message, key)
  46. return enc_message
  47. def insert():
  48. start = time.time()
  49. image_path = config_loader.read('''data['environment']['cover_image']''')
  50. photo = Image.open(image_path).convert('RGB') # just insert the image name here
  51. data = np.asarray(photo).copy()
  52. width, height = photo.size
  53. secret = byte2bin(secret_Loader())
  54. secret_pointer = 0
  55. lensecret = len(secret)
  56. insert_length(lensecret, data)
  57. insertion = time.time()
  58. for x in range(1, height):
  59. for y in range(width):
  60. if lensecret > secret_pointer:
  61. # RED
  62. data[x][y][0] = insert_data_in_pixel(data[x][y][0], secret, secret_pointer, bits=2)
  63. secret_pointer += 2
  64. if lensecret == secret_pointer:
  65. break
  66. # Green
  67. data[x][y][1] = insert_data_in_pixel(data[x][y][1], secret, secret_pointer, bits=2)
  68. secret_pointer += 2
  69. if lensecret == secret_pointer:
  70. break
  71. # Blue
  72. data[x][y][2] = insert_data_in_pixel(data[x][y][2], secret, secret_pointer, bits=1)
  73. secret_pointer += 1
  74. if lensecret == secret_pointer:
  75. break
  76. print("data insertion",time.time()-insertion)
  77. generation = time.time()
  78. # print(data)
  79. data = Image.fromarray(data)
  80. print("image generation in ", time.time()-generation)
  81. # data.show()
  82. _ = time.time()
  83. data = data.save(r'stg.PNG')
  84. print("saving time ", time.time()-_)
  85. print('Exectuted in->', time.time() - start)
  86. if __name__ == '__main__':
  87. insert()

时间安排

  1. encryption in 1.0841524600982666
  2. data insertion 9.439783811569214
  3. image generation in 0.039893388748168945
  4. saving time 6.283206939697266
  5. Exectuted in-> 17.11327576637268

我考虑过多线程,但这是不可靠的,因为数据中的每一位都很重要,而且它在序列中的位置也很重要。
p、 s数据插入时间为10000行

  1. this is a message to test the limit of the program let's check when it breaks and how, also i'm running out of words0

所以这并不坏,但如果可以改进,我该如何实现呢?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题