Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from PIL import Image
  5.  
  6.  
  7. def load_image(filename):
  8.     image = Image.open(filename)
  9.     image.load()
  10.     data = np.asarray(image, dtype="uint8")
  11.     return data
  12.  
  13.  
  14. def to_image(data):
  15.     image = Image.fromarray(data)
  16.     return image
  17.  
  18.  
  19. r = tf.placeholder(dtype=tf.float32, shape=[None, None])
  20. g = tf.placeholder(dtype=tf.float32, shape=[None, None])
  21. b = tf.placeholder(dtype=tf.float32, shape=[None, None])
  22.  
  23. d = tf.placeholder(dtype=tf.float32, shape=None)
  24.  
  25. out = (0.3*r + 0.59*g + 0.11*b) * d / 255
  26.  
  27. img = load_image("HappyFish.jpg")
  28. img_ = Image.open("HappyFish.jpg")
  29.  
  30. with tf.Session() as sess:
  31.     plt.subplot(1, 2, 1)
  32.     plt.imshow(to_image(img))
  33.     plt.title("Original")
  34.     plt.subplot(1, 2, 2)
  35.     plt.title("Grayscaled")
  36.     plt.imshow(to_image(sess.run(out, feed_dict={r: img_.split()[0], g: img_.split()[1], b: img_.split()[2], d: 255})))
  37.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement