In my previous post I looked at turning an image (in this case XKCD #1696) into it’s pixel values. Now I am going to take the CSV of values and turn them back into an image.
The Python for this function (including the Python for the functions to turn the image into a CSV from the previous post) is:
Uses Numpy to create an array of zeros that matches the size of our image (423 by 253).
with open(‘pic_dict.csv’, newline=”) as csvfile:
reader = csv.DictReader(csvfile)
Opens the CSV.
for row in reader:
x = row[‘a’]
x = int(x)
y = row[‘b’]
y = int(y)
bgr = row[‘c’]
#gets the BGR value out of the []
bgr_edit = str(bgr)
print(bgr_edit)
head,sep,tail = bgr_edit.partition(‘ ‘)
b = head.strip(‘[‘)
bgr_tail = tail.strip()
bgr_tail = bgr_tail.replace(“]”,””)
head,sep,tail = bgr_tail.partition(” “)
g = head
r = tail
b = int(b)
g = int(g)
r = int(r)
image[x,y]=[b,g,r]
Reads through each row of the CSV and maps the BGR values to the appropriate pixel location (x,y).
cv2.imshow(‘xkcd 1696’,image)
Shows the image with the title (xkcd 1696)
A function to turn a CSV into an image
I have created a video (hosted on YouTube) of the program in action. The video shows the CSV created from my previous blog post and the function in action reading the CSV to recreate the image of https://xkcd.com/1696/.
Please note: The function took around 7 minutes to run. I have sped this up for the video on YouTube.
Related
Published by Geek_Dude
I'm a tech enthusiast that enjoys science, science fiction, comics and video games - pretty much anything geeky.
View all posts by Geek_Dude
You must be logged in to post a comment.