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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from turtle import ycor
|
| 2 |
+
import numpy as np
|
| 3 |
+
import png
|
| 4 |
+
import cv2
|
| 5 |
+
|
| 6 |
+
class Image:
|
| 7 |
+
def __init__(self, x_pixels=0, y_pixels=0, filename=''):
|
| 8 |
+
# you need to input either filename OR x_pixels, y_pixels, and num_channels
|
| 9 |
+
self.input_path = 'pyphotoshop-main\input/'
|
| 10 |
+
self.output_path = 'pyphotoshop-main\output/'
|
| 11 |
+
|
| 12 |
+
self.x_pixels = x_pixels
|
| 13 |
+
self.y_pixels = y_pixels
|
| 14 |
+
|
| 15 |
+
self.array = np.zeros((x_pixels, y_pixels))
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
#Read original image
|
| 19 |
+
im=cv2.imread(r"Animate\images\flag (1).png")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
#Change to 2D array and canny_edges
|
| 23 |
+
canny_edges=cv2.Canny(image=im, threshold1=100, threshold2=200)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
manys=np.random.randint(255, size=(5,5))
|
| 27 |
+
ones=np.array([[0, 255, 0, 255,0],[ 255, 0, 0, 255, 255],[0, 255, 0, 255, 0],[255, 255, 0, 255, 0],[ 255, 0, 255, 0, 255]])
|
| 28 |
+
|
| 29 |
+
cv2.imwrite("ones.png",ones)
|
| 30 |
+
|
| 31 |
+
#Try loop through elements in the image matrice:
|
| 32 |
+
|
| 33 |
+
#-----------------------------------------------------------------------------------------------------------
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
#Getting the last key in a dictionary
|
| 39 |
+
def get_last_key(dictionary):
|
| 40 |
+
for key in dictionary.keys():
|
| 41 |
+
last_key=key
|
| 42 |
+
return last_key
|
| 43 |
+
|
| 44 |
+
#Get the coord of the key with the white value/255
|
| 45 |
+
def get_white_key(dictionary):
|
| 46 |
+
for key,value in dictionary.items():
|
| 47 |
+
if value==255:
|
| 48 |
+
white_coord=key
|
| 49 |
+
return white_coord
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# find neighbouring pixel:
|
| 53 |
+
def get_neighbours(image, x, y, x_pixels, y_pixels, kernel=0):
|
| 54 |
+
|
| 55 |
+
neighbour_coords=[
|
| 56 |
+
[max(0, (x-1)),max(0,(y-1))],
|
| 57 |
+
[max(0, (x-1)),y],
|
| 58 |
+
[max(0, (x-1)), min((y_pixels-1),(y+1))],
|
| 59 |
+
[x,max(0,y-1)],
|
| 60 |
+
[x,min((y_pixels-1),(y+1))],
|
| 61 |
+
[min((x_pixels-1),(x+1)),max(0,(y-1))],
|
| 62 |
+
[min((x_pixels-1),(x+1)),y],
|
| 63 |
+
[min((x_pixels-1),(x+1)),y+1]
|
| 64 |
+
] # to finish array kernel....
|
| 65 |
+
neighbour_coords=np.array(neighbour_coords)
|
| 66 |
+
print(f"Image pixel is : at {x,y} ")
|
| 67 |
+
return neighbour_coords
|
| 68 |
+
|
| 69 |
+
#find value at neighbour
|
| 70 |
+
def value_at_neighbour(new_frame,image,coord=[0,0],pixel_count=0):
|
| 71 |
+
pixel_count+=1
|
| 72 |
+
print(f"Pixel count is at {pixel_count}")
|
| 73 |
+
x_pixels, y_pixels=np.shape(image)
|
| 74 |
+
neighbour_coords=get_neighbours(image, coord[0], coord[1],x_pixels, y_pixels,kernel=0)
|
| 75 |
+
neighbour_values=[]#empty array with shape of nighbour-co-ords array
|
| 76 |
+
dict={}
|
| 77 |
+
|
| 78 |
+
#Run through coords in neighbours coord list and find their values
|
| 79 |
+
for coord in neighbour_coords:
|
| 80 |
+
neighbour_value = image[min(x_pixels-1,coord[0]),min(y_pixels-1,coord[1])]
|
| 81 |
+
neighbour_values.append(neighbour_value)
|
| 82 |
+
|
| 83 |
+
#Changing values back to normal arrays to work with in dict
|
| 84 |
+
pyneighbour_value=int(neighbour_value)
|
| 85 |
+
pyz=tuple(coord)
|
| 86 |
+
dict[pyz]=pyneighbour_value# append to dictionary of neighbour-co-ords
|
| 87 |
+
print(f"My dict of neighbour coords:values is {dict} and value is {pyneighbour_value} ")#At the end of this for loop, we finally get
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
if pixel_count <25:
|
| 93 |
+
if 255 in neighbour_values:
|
| 94 |
+
coord=get_white_key(dict)
|
| 95 |
+
print(f"\n \n New coordinate in recursive function is {coord} and pixl count{pixel_count}")
|
| 96 |
+
|
| 97 |
+
#Append dict of neighbours values to new_frame array
|
| 98 |
+
for key, value in dict.items():
|
| 99 |
+
x_index=int(key[0])
|
| 100 |
+
y_index=int(key[1])
|
| 101 |
+
new_frame[x_index][y_index]=value
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
#Convert array with new dict values to np array, then save it a
|
| 105 |
+
#list of variables that we can cv.write later
|
| 106 |
+
frames[pixel_count]=new_frame
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
value_at_neighbour(new_frame, image,coord,pixel_count=pixel_count)
|
| 110 |
+
|
| 111 |
+
#if all the values are black and it breaks out of loop
|
| 112 |
+
#We need to check the next square
|
| 113 |
+
elif 255 not in neighbour_values:
|
| 114 |
+
coord=get_last_key(dict)
|
| 115 |
+
print(f"\n \n Value is 0 so new coord is {coord}")
|
| 116 |
+
value_at_neighbour(new_frame, image,coord,pixel_count)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
#Create and write image with path
|
| 133 |
+
#Create an empty imaage of arrays with 0 and switch the 0 with the white values one by one
|
| 134 |
+
|
| 135 |
+
"""
|
| 136 |
+
That is, if neighbour coord is True
|
| 137 |
+
If neighbour coord is True, then move to square
|
| 138 |
+
Divide square by number of frames
|
| 139 |
+
We need it to pick a square
|
| 140 |
+
"""
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
#Initialize frame count
|
| 144 |
+
frame_count=0
|
| 145 |
+
|
| 146 |
+
#So now we have to create a path through the image and create frames
|
| 147 |
+
def create_path_frames(frames=10) :
|
| 148 |
+
new_frame=np.zeros(5,5)
|
| 149 |
+
for i in range(frames): #number of frames
|
| 150 |
+
frame_count += 1
|
| 151 |
+
cv2.imwrite(f'new{frame_count}.png',new_frame)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
new_frame=[[0]*5]*5
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
frames={}
|
| 160 |
+
|
| 161 |
+
value_at_neighbour(new_frame, ones)
|
| 162 |
+
|
| 163 |
+
print(len(frames))
|
| 164 |
+
"""
|
| 165 |
+
for key, value in frames.items():
|
| 166 |
+
frame=np.array(value)
|
| 167 |
+
cv2.imwrite(f'frame{key}.png',frame)
|
| 168 |
+
"""
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
|