else
Pixelify
120
Dots/dots.py
Normal file
|
@ -0,0 +1,120 @@
|
|||
import tkinter
|
||||
import threading
|
||||
import time
|
||||
|
||||
class CanvasGame:
|
||||
def managementthread(self):
|
||||
while True:
|
||||
w = self.pressed_dict.get('w', False)
|
||||
s = self.pressed_dict.get('s', False)
|
||||
a = self.pressed_dict.get('a', False)
|
||||
d = self.pressed_dict.get('d', False)
|
||||
if w:
|
||||
self.velocity_y -= self.velocity_speedup
|
||||
if s:
|
||||
self.velocity_y += self.velocity_speedup
|
||||
|
||||
if a:
|
||||
self.velocity_x -= self.velocity_speedup
|
||||
if d:
|
||||
self.velocity_x += self.velocity_speedup
|
||||
|
||||
if not any(movement for movement in [w,a,s,d]):
|
||||
nondirectional = abs(self.velocity_x) + abs(self.velocity_y)
|
||||
nondirectional /= 2
|
||||
if nondirectional < self.velocity_minimum:
|
||||
self.velocity_x = 0
|
||||
self.velocity_y = 0
|
||||
else:
|
||||
if self.velocity_x != 0:
|
||||
self.velocity_x *= self.velocity_slowdown
|
||||
if self.velocity_y != 0:
|
||||
self.velocity_y *= self.velocity_slowdown
|
||||
|
||||
|
||||
|
||||
self.velocity_x = self.numbercap(self.velocity_x, self.velocity_maximum)
|
||||
self.velocity_y = self.numbercap(self.velocity_y, self.velocity_maximum)
|
||||
|
||||
self.player_x += self.velocity_x
|
||||
self.player_y += self.velocity_y
|
||||
|
||||
x = self.x_camera + self.player_x
|
||||
y = self.y_camera + self.player_y
|
||||
xdiff = x - self.gamewidthcenter
|
||||
ydiff = y - self.gameheightcenter
|
||||
if xdiff > self.camera_distance:
|
||||
self.x_camera -= abs(xdiff / self.camera_distance)
|
||||
elif xdiff < -self.camera_distance:
|
||||
self.x_camera += abs(xdiff / self.camera_distance)
|
||||
|
||||
if ydiff > self.camera_distance:
|
||||
self.y_camera -= abs(ydiff / self.camera_distance)
|
||||
elif ydiff < -self.camera_distance:
|
||||
self.y_camera += abs(ydiff / self.camera_distance)
|
||||
|
||||
#print(self.player_x, x)
|
||||
|
||||
self.canvas.coords(self.player, x-self.player_radius, y-self.player_radius,
|
||||
x+self.player_radius, y+self.player_radius)
|
||||
|
||||
time.sleep(0.017)
|
||||
|
||||
def numbercap(self, i, cap):
|
||||
if i > cap:
|
||||
i = cap
|
||||
elif i < -cap:
|
||||
i = -cap
|
||||
return i
|
||||
|
||||
def pushkey(self, event):
|
||||
#print(event.char, "Down")
|
||||
self.pressed_dict[event.char] = True
|
||||
def releasekey(self, event):
|
||||
#print(event.char, "Up")
|
||||
self.pressed_dict[event.char] = False
|
||||
|
||||
def __init__(self):
|
||||
self.t = tkinter.Tk()
|
||||
self.pressed_dict = {}
|
||||
|
||||
self.gamewidth = 640
|
||||
self.gameheight = 480
|
||||
self.gamewidthcenter = self.gamewidth / 2
|
||||
self.gameheightcenter = self.gameheight / 2
|
||||
self.canvas = tkinter.Canvas(self.t, width=self.gamewidth, height=self.gameheight)
|
||||
self.x_camera = (self.gamewidth / 2)
|
||||
self.y_camera = (self.gameheight / 2)
|
||||
|
||||
x = 0
|
||||
y = 0
|
||||
self.player_x = x
|
||||
self.player_y = y
|
||||
self.player_radius = 3
|
||||
self.player = self.canvas.create_oval(x-self.player_radius, y-self.player_radius,
|
||||
x+self.player_radius, y+self.player_radius,
|
||||
fill="#ff0")
|
||||
self.camera = self.canvas.create_oval(self.x_camera, self.y_camera, self.x_camera, self.y_camera)
|
||||
self.camera_distance = 20
|
||||
self.playerthread = threading.Thread(name="Playerthread", target=self.managementthread)
|
||||
self.playerthread.daemon = True
|
||||
self.velocity_x = 0
|
||||
self.velocity_y = 0
|
||||
self.velocity_maximum = 4
|
||||
self.velocity_minimum = 0.2
|
||||
self.velocity_slowdown = 0.95
|
||||
self.velocity_speedup = 0.6
|
||||
|
||||
self.entities = []
|
||||
|
||||
self.canvas.bind("<KeyPress>", self.pushkey)
|
||||
self.canvas.bind("<KeyRelease>", self.releasekey)
|
||||
self.canvas.focus_set()
|
||||
|
||||
self.playerthread.start()
|
||||
|
||||
self.canvas.pack()
|
||||
|
||||
self.t.mainloop()
|
||||
|
||||
c = CanvasGame()
|
43
Pixelify/README.md
Normal file
|
@ -0,0 +1,43 @@
|
|||
Pixelify
|
||||
========
|
||||
|
||||
Takes an image, or a folder full of images, and produces pixelated versions of each image according to an "objective". That is, how the image would look if it was forced to fit into an *objective x objective* frame. But, the actual output file will be the same dimensions as the original, simply pixelated.
|
||||
|
||||
Examples:
|
||||
|
||||
You are in a folder called C:\pics where there are 2 images: 1.png, 2.png
|
||||
|
||||
Path to image or directory
|
||||
> 1.png
|
||||
Pixel Objective
|
||||
> 32
|
||||
|
||||
A folder called "pixel" is created, and 1_32.png is inside.
|
||||
|
||||
____________
|
||||
|
||||
Path to image or directory
|
||||
> 1.png
|
||||
Pixel Objective
|
||||
> 32, 64, 128
|
||||
|
||||
1_32.png, 1_64.png, and 1_128.png have been created
|
||||
|
||||
___________
|
||||
|
||||
Path to image or directory
|
||||
>
|
||||
Pixel Objective
|
||||
> 32
|
||||
|
||||
Path was left completely blank (no spaces or anything), so it uses the current folder.
|
||||
1_32.png and 2_32.png have been created.
|
||||
|
||||
____________
|
||||
|
||||
Path to image or directory
|
||||
> C:\otherfolder
|
||||
Pixel Objective
|
||||
> 24
|
||||
|
||||
C:\otherfolder\pixel\ is now full of 24-objective images.
|
BIN
Pixelify/examples/Kurtjmac.jpg
Normal file
After Width: | Height: | Size: 135 KiB |
BIN
Pixelify/examples/NeilDeGrasseTyson.png
Normal file
After Width: | Height: | Size: 410 KiB |
BIN
Pixelify/examples/pixel/Kurtjmac_128.jpg
Normal file
After Width: | Height: | Size: 29 KiB |
BIN
Pixelify/examples/pixel/Kurtjmac_32.jpg
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
Pixelify/examples/pixel/Kurtjmac_64.jpg
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
Pixelify/examples/pixel/NeilDeGrasseTyson_128.png
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
Pixelify/examples/pixel/NeilDeGrasseTyson_32.png
Normal file
After Width: | Height: | Size: 8.8 KiB |
BIN
Pixelify/examples/pixel/NeilDeGrasseTyson_64.png
Normal file
After Width: | Height: | Size: 18 KiB |
60
Pixelify/pixelify.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
from PIL import Image
|
||||
import os
|
||||
|
||||
def boot():
|
||||
path = input("Path to image or directory\n> ")
|
||||
if path == '':
|
||||
path = os.getcwd()
|
||||
objectives = input("Pixel Objective\n> ")
|
||||
objectives = objectives.replace(' ', '')
|
||||
objectives = [int(x) for x in objectives.split(',')]
|
||||
pixelify(path, objectives)
|
||||
|
||||
def pixelify(path, objectives=[32], subfolder="pixel"):
|
||||
if '.' in path:
|
||||
name = path.split('/')[-1]
|
||||
path = '/'.join(path.split('/')[:-1])
|
||||
images = [name]
|
||||
else:
|
||||
images = os.listdir(path)
|
||||
if path[-1] in ['/', '\\']:
|
||||
path = path[:-1]
|
||||
|
||||
done = False
|
||||
while not done:
|
||||
done = True
|
||||
for name in images:
|
||||
ext = name.lower()[-4:]
|
||||
if ext != '.png' and ext != '.jpg':
|
||||
done = False
|
||||
images.remove(name)
|
||||
if name != subfolder:
|
||||
print('Unlisted "%s": not .jpg or .png' % name)
|
||||
break
|
||||
|
||||
newdir = path +'/' + subfolder + '/'
|
||||
if not os.path.exists(newdir):
|
||||
print('Creating directory: ' + newdir)
|
||||
os.makedirs(newdir)
|
||||
|
||||
for name in images:
|
||||
filepath = path + '/' + name
|
||||
image = Image.open(filepath)
|
||||
|
||||
for objective in objectives:
|
||||
print("Working: " + name, objective)
|
||||
image_width = image.size[0]
|
||||
image_height = image.size[1]
|
||||
ratio = objective / max([image_width, image_height])
|
||||
new_width = image_width * ratio
|
||||
new_height = image_height * ratio
|
||||
nimage = image.resize((round(new_width), round(new_height)), 1)
|
||||
nimage = nimage.resize((image_width, image_height), 0)
|
||||
|
||||
parts = name.split('.')
|
||||
newpath = newdir + parts[0] + '_' + str(objective) + '.' + parts[1]
|
||||
nimage.save(newpath)
|
||||
|
||||
|
||||
|
||||
boot()
|