title: How to stop images from affecting other images in python?
body: <p>I'm writing a piece of code, in Python, to show image quantization (2,4,6,8 - Level).
  I'm using matplotlib, skimage and numpy.
  The code was running as expected for the individual levels, but when I tried to club them all into one program, the image produced first seems to affect the ones after it.</p>
  <p>I have used separate arrays for the different levels and have assigned them separately but nothing changed.</p>
  <p>I also tried using separate values for each snippet but to no avail.</p>
  <p>The only thing I have noticed is that when I write the code from 2-8 level instead of 8-2 level, the images turn out different. It is from here that I deduced that somehow, despite being declared separately, the transformations are being made on the previous image rather than the original image.</p>
  <pre><code>import numpy as np
  from skimage import color
  from skimage import io
  import matplotlib.pyplot as plt
  
  #Original Image
  i = io.imread('A1.jpg')
  plt.subplot(3, 2, 1)
  plt.imshow(i)
  plt.axis('off')
  plt.title(&quot;Original&quot;)
  
  #Grayscale
  iG1 = color.rgb2gray(i)
  (r,c) = iG1.shape
  plt.subplot(3, 2, 2)
  plt.imshow(iG1, cmap='gray')
  plt.axis('off')
  plt.title(&quot;Grayscale&quot;)
  
  iG8 = iG1
  iG6 = iG1
  iG4 = iG1
  iG2 = iG1 
  
  #8-Level Quantization
  for i8 in range(0,r):
      for j8 in range(0,c):
          f8 = iG8[i8,j8]
          if f8&lt;=0.125:
              iG8[i8,j8] = 0
          elif f8&gt;0.125 and f8&lt;=0.25:
              iG8[i8,j8] = 0.1875
          elif f8&gt;0.25 and f8&lt;=0.375:
              iG8[i8,j8] = 0.3125
          elif f8&gt;0.375 and f8&lt;=0.5:
              iG8[i8,j8] = 0.4375
          elif f8&gt;0.5 and f8&lt;=0.625:
              iG8[i8,j8] = 0.5625
          elif f8&gt;0.625 and f8&lt;=0.75:
              iG8[i8,j8] = 0.687
          elif f8&gt;0.75 and f8&lt;=0.875:
              iG8[i8,j8] = 0.8125 
          elif f8&gt;0.875 and f8&lt;=1.0:
              iG8[i8,j8] = 1
              
  plt.subplot(3, 2, 3)
  plt.imshow(iG8, cmap='gray')
  plt.axis('off')
  plt.title(&quot;8-Level Quantization&quot;) 
  
  #6-Level Quantization
  for i6 in range(0,r):
      for j6 in range(0,c):
          f6 = iG6[i6,j6]
          if f6&lt;=0.16:
              iG6[i6,j6] = 0
          elif f6&gt;0.16 and f6&lt;=0.32:
              iG6[i6,j6] = 0.24
          elif f6&gt;0.32 and f6&lt;=0.48:
              iG6[i6,j6] = 0.4
          elif f6&gt;0.48 and f6&lt;=0.64:
              iG6[i6,j6] = 0.56
          elif f6&gt;0.64 and f6&lt;=0.8:
              iG6[i6,j6] = 0.72
          elif f6&gt;0.8 and f6&lt;=1.0:
              iG6[i6,j6] = 1
              
  plt.subplot(3, 2, 4)
  plt.imshow(iG6, cmap='gray')
  plt.axis('off')
  plt.title(&quot;6-Level Quantization&quot;)
  
  #4-Level Quantization
  for i4 in range(0,r):
      for j4 in range(0,c):
          f4 = iG4[i4,j4]
          if f4&lt;=0.25:
              iG4[i4,j4] = 0
          elif f4&gt;0.25 and f4&lt;=0.50:
              iG4[i4,j4] = 0.25
          elif f4&gt;0.50 and f4&lt;=0.75:
              iG4[i4,j4] = 0.75
          elif f4&gt;0.75 and f4&lt;=1.0:
              iG4[i4,j4] = 1
              
  plt.subplot(3, 2, 5)
  plt.imshow(iG4, cmap='gray')
  plt.axis('off')
  plt.title(&quot;4-Level Quantization&quot;)  
  
  #2-Level Quantization
  for i2 in range(0,r):
      for j2 in range(0,c):
          f2 = iG2[i2,j2]
          if f2&lt;=0.50:
              iG2[i2,j2] = 0
          elif f2&gt;0.50:
              iG2[i2,j2] = 1
  
  plt.subplot(3, 2, 6)
  plt.imshow(iG2, cmap='gray')
  plt.axis('off')
  plt.title(&quot;2-Level Quantization&quot;)
  </code></pre>
  <p>This code gives me the output:
  <a href="https://i.stack.imgur.com/lL2ME.png" rel="nofollow noreferrer">8-2Level Quantization</a></p>
  <p>My problem is that the images are being affected by the transformations done on the previous image, clearly demonstrated by this output: <a href="https://i.stack.imgur.com/VPokk.png" rel="nofollow noreferrer">2-8Level Quantization</a></p>
  <p>The actual images should look like this: <a href="https://i.stack.imgur.com/WbVa9.png" rel="nofollow noreferrer">2Level</a> <a href="https://i.stack.imgur.com/ocAGa.png" rel="nofollow noreferrer">4Level</a> <a href="https://i.stack.imgur.com/r9OYB.png" rel="nofollow noreferrer">6Level</a> <a href="https://i.stack.imgur.com/8e0Ao.png" rel="nofollow noreferrer">8Level</a></p>