Turning A CSV Back Into An Image (Python)

A function to turn a CSV into an image

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:

# geektechstuff Image CV
import cv2
import csv
import numpy as np
def pic_data_strip_csv():
   img = cv2.imread(‘image.png’)
   img_height, img_width = img.shape[:2]
   for x in range (0, img_height,1):
      for y in range (0,img_width,1):
         pixel_value = img[x,y,:]
         with open(‘pic.csv’,mode=’a’) as pic_data:
            pic_data_writer = csv.writer(pic_data, delimiter=’,’, quotechar='”‘, quoting=csv.QUOTE_MINIMAL)
            write_pic_data = pic_data_writer.writerow([x,y,pixel_value])
   return()
def pic_data_strip_dict_csv():
   img = cv2.imread(‘image.png’)
   img_height, img_width = img.shape[:2]
   for x in range (0, img_height,1):
      for y in range (0,img_width,1):
         pixel_value = img[x,y,:]
         with open(‘pic_dict.csv’,mode=’a’,newline=”) as pic_data:
            field_names = [‘x’,’y’,’bgr’]
            pic_data_writer = csv.DictWriter(pic_data, fieldnames=field_names)
            write_pic_data = pic_data_writer.writerow({‘x’:x,’y’:y,’bgr’:pixel_value})
   return()
def create_pic_from_csv():
   image = np.zeros((423,253,3), np.uint8)
   with open(‘pic_dict.csv’, newline=”) as csvfile:
      reader = csv.DictReader(csvfile)
      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]
      cv2.imshow(‘xkcd 1696’,image)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
   return()

 

Here is what the function does:
  image = np.zeros((423,253,3), np.uint8) 
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)
geektechstuff_open_cv_generating_image_1
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.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.