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