I create code convert an image to binary number and save to file but I need after that Vice versa this method. Means convert binary numbers to image file.
from PIL import Image
# Load the image
img = Image.open("image.jpg")
# Convert the image to grayscale
img = img.convert("L")
# Get the pixel values as a list of tuples
pixels = list(img.getdata())
# Convert each pixel value to binary and concatenate them into a string
binary_str = ''.join(['{0:08b}'.format(pixel) for pixel in pixels])
# Save the binary data to a text file
with open("image_data.txt", "w") as f:
f.write(binary_str)
Thanks in advance.
My code just convert image file to binary numbers. I need vice versa of this code, thanks.