In: Computer Science
from PIL import Image
def stringToBits(string):
return str(bin(int.from_bytes(string.encode(), 'big')))[2:]
def bitsToString(bits):
if bits[0:2] != '0b':
bits = '0b' + bits
value = int(bits, 2)
return value.to_bytes((value.bit_length() + 7) // 8, 'big').decode()
def writeMessageToRedChannel(file, message):
image = Image.open(file)
width, height = image.size
messageBits = stringToBits(message)
messageBitCounter = 0
y = 0
while y < height:
x = 0
while x < width:
r, g, b, a = image.getpixel((x, y))
print("writeMessageToRedChannel: Reading pixel %d, %d - Original values (%d, %d, %d, %d)" % (x, y, r, g, b, a))
if(messageBitCounter < len(messageBits)):
r = int(messageBits[messageBitCounter])
else:
r = 255
print("writeMessageToRedChannel: Editing pixel %d, %d - New values (%d, %d, %d, %d)" % (x, y, r, g, b, a))
image.putpixel((x,y), (r, g, b, a))
x += 1
messageBitCounter += 1
y += 1
image.save(file)
def readMessageFromRedChannel(file):
image = Image.open(file)
width, height = image.size
message = ""
y = 0
while y < height:
x = 0
while x < width:
r, g, b, a = image.getpixel((x, y))
if(r != 255):
message += str(r)
else:
return bitsToString(message)
x += 1
y += 1
#########################################
### EDIT CODE BELOW THIS COMMENT ########
#########################################
def resetImage(file):
# Open the Image specified in file
# Get the width and height
# Write a nested loop statement to access each pixel
# Get the current pixel RGBA
# Print the current value to the screen
# Write new RGBA of (255, 255, 255, 255) to the pixel
# Print the new value to the screen
# Save the image file
#########################################
### EDIT CODE ABOVE THIS COMMENT ########
#########################################
message = "Hello, there! How are you! I want to tell you a secret... "
message += "and the secret is that I am Batman!!!"
# Reset the image to all white pixels
# RGBA = (255, 255, 255, 255)
resetImage("secret_image.png")
# Write message to the image
print("Writing the secret message: %s" % message)
writeMessageToRedChannel("secret_image.png", message)
# Read message from the image (if it worked properly, the
# message should be the same as the one written to the image)
print("Reading the secret message: %s" % readMessageFromRedChannel("secret_image.png"))
I need help with the missing code please.
Input:
Input image
Program:
Output:
Summary:
The program is to reset the image pixels to white.
Input: Image from the user
Step:
1)Open the image file.
2) Get the width and height of the image using image.size
3) Convert the image to RBGA image (some images can be RGB)
4) Iterate through each pixel .
1)Get the current rgba using image.getpixel((row,col))
2)Print current value to the screen
3)Write new rgba to the pixel using image.putpixel((row,col),(255,255,255,255)) => white
4)Print the new value to screen
5) Save image