使用PIL将灰度图像(以前从彩色图像转换为彩色图像)转换为彩色图像?

时间:2019-03-30 13:39:13

标签: python python-3.x image python-imaging-library

因此,我编写了一个代码,将数据隐藏到灰度图像中,并且可以从灰度图像中检索回去。我希望能够对彩色图像执行此操作。 目前,我正在考虑将彩色图像转换为灰度图像,隐藏数据,然后将图像转换回彩色图像。如果可能的话。 我想考虑的另一件事是,对于灰度,getpixel返回一个值,而对于彩色getpixel返回一个元组,所以我还想到了仅操作一个元组的值(如果正确)。

编辑:代码,我试图从彩色图像的元组中获取一个值。另外,对不起,根本没有记录。

from PIL import Image
import numpy as np
import glob
import os
from helper import tobits

image_list = []

for filename in glob.glob('*.png'):
  image_list.append(filename)
print(image_list)




#onlyforalphas
message = "he23@"*200
#print(message)
messagebi = ''.join(format(ord(x), '07b') for x in message)
#messagebi = tobits(message)
#print(messagebi)

payload = len(messagebi)

print(".........................PAYLOAD LENGTH : ", payload, "..................................................")

width = 512
height = 512

max_a = 0
min_b = 0

max_value = 0
min_value = 10000

z = 0
zi = 0
fileindex = 0
im = Image.open(image_list[0])
#print(im.histogram())
while payload > 0 and z < len(image_list):
  print(".........................PAYLOAD LENGTH : ", payload, "..........................................")
  print("OPENING FILE", image_list[z])
  im = Image.open(image_list[z])
  #print(im.histogram())
  z = z + 1
  hist_list = np.asarray(im.histogram())
  print(im.histogram())
  for i in range(len(hist_list)):
    if hist_list[i] >= max_value:
      max_value = hist_list[i]
      max_a = i
    if hist_list[i] <= min_value:
      min_value = hist_list[i]
      min_b = i
  if payload > max_value:
    print("ERROR:payload size: ", payload, " too large. Trying another image")

  hist_list = np.asarray(im.histogram())    
  print(max_a, " ", max_value)
  print(min_b, " ", min_value)
  payload = payload - max_value
  if payload < 0:
    payload = 0
  hist_list[max_a] = 0
  #zi = 0
  messagelength = len(messagebi)
  #print(messagebi, "   ", messagelength)
  for i in range(width):
      for j in range(height):
          temp = im.getpixel((i,j))[0]
          #print(temp)
          if  temp > max_a and temp < min_b:
              im.putpixel((i,j), temp + 1)
              #print(im.getpixel((i,j)), end = "")
          if zi < messagelength and messagebi[zi] == '1' and temp == max_a:
              im.putpixel((i,j), max_a+1)
              zi = zi + 1
          elif zi < messagelength and messagebi[zi] == '0' and temp == max_a:
              zi = zi + 1
      #print("")
  #imnu = Image.fromarray(hist_list, mode='L')
  print("payload size after ", fileindex, "iteration is:", payload)
  filename = "output/filename" + str(fileindex) + ".png"
  im.save(filename)
  fileindex = fileindex + 1
  print(im.histogram())

0 个答案:

没有答案