Last commit not found
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) | |
""" | |