How to draw slick lines using PIL and Tkinter - python

I'm making a Pixel Art package with Pillow (PIL) and Tkinter. So far, I have set up a code that draws a continuous line on mouse motion path. However, the line isn't slick.
I'm tried drawing five pixels x-2, x-1, x, x+1, x+2 yet still, the line isnt't slick.
import tkinter as tk
from PIL import Image
def tinker(img, pos, color):
pixels = img.load()
x = pos[0]
y = pos[1]
if (x > 3 and y > 3) and (x < 253 and y < 253):
pixels[x-2, y] = color
pixels[x - 1, y] = color
pixels[x , y] = color
pixels[x + 1, y] = color
pixels[x + 2, y] = color
elif x < 3 and y < 3:
x = 3
y = 3
elif x > 253 and y > 253:
x = 253
y = 253
img.save("image.png")
root = tk.Tk()
img = Image.new("RGB", (256, 256), color="white")
img.save("image.png")
openimg = tk.PhotoImage(file="image.png")
panel = tk.Label(root, image=openimg)
panel.pack(side="bottom", fill="both", expand="yes")
def callback(e):
x, y = e.x, e.y
print(f"({x}, {y})")
tinker(img, pos=(x, y), color=(0, 0, 0))
img2 = tk.PhotoImage(file="image.png")
panel.configure(image=img2)
panel.image = img2
root.bind("<B1-Motion>", callback)
root.mainloop()
I expected slick lines, a la Photoshop, GIMP and Paint.net.

Related

Moving widgets in Canvas Tkinter

I have a canvas with a little oval in it. It moves throughout the widget using the arrow keys but when it's on the edge of the canvas if I move it beyond that, the oval just disappears.
I want the oval stays on any edge of the canvas no matter if I continue pressing the arrow key corresponding to that edge without disappearing.
This is the code:
from tkinter import *
root = Tk()
root.title("Oval")
root.geometry("800x600")
w = 600
h = 400
x = w//2
y = h//2
my_canvas = Canvas(root, width=w, height=h, bg='black')
my_canvas.pack(pady=20)
my_circle = my_canvas.create_oval(x, y, x+20, y+20, fill='cyan')
def left(event):
x = -10
y = 0
my_canvas.move(my_circle, x, y)
def right(event):
x = 10
y = 0
my_canvas.move(my_circle, x, y)
def up(event):
x = 0
y = -10
my_canvas.move(my_circle, x, y)
def down(event):
x = 0
y = 10
my_canvas.move(my_circle, x, y)
root.bind('<Left>', left)
root.bind('<Right>', right)
root.bind('<Up>', up)
root.bind('<Down>', down)
root.mainloop()
This is what it looks like:
The oval on an edge
And if I continue pressing the key looks like this:
The oval disappearing
You could test the current coordinates and compare them to your canvas size.
I created a function to get the current x1, y1, x2, y2 from your oval. This way you have the coordiantes of the borders of your oval.
So all I do is testing if the oval is touching a border.
from tkinter import *
root = Tk()
root.title("Oval")
root.geometry("800x600")
w = 600
h = 400
x = w // 2
y = h // 2
my_canvas = Canvas(root, width=w, height=h, bg='black')
my_canvas.pack(pady=20)
my_circle = my_canvas.create_oval(x, y, x + 20, y + 20, fill='cyan')
def left(event):
x1, y1, x2, y2 = get_canvas_position()
if x1 > 0:
x = -10
y = 0
my_canvas.move(my_circle, x, y)
def right(event):
x1, y1, x2, y2 = get_canvas_position()
if x2 < w:
x = 10
y = 0
my_canvas.move(my_circle, x, y)
def up(event):
x1, y1, x2, y2 = get_canvas_position()
if y1 > 0:
x = 0
y = -10
my_canvas.move(my_circle, x, y)
def down(event):
x1, y1, x2, y2 = get_canvas_position()
if y2 < h:
x = 0
y = 10
my_canvas.move(my_circle, x, y)
def get_canvas_position():
return my_canvas.coords(my_circle)
root.bind('<Left>', left)
root.bind('<Right>', right)
root.bind('<Up>', up)
root.bind('<Down>', down)
root.mainloop()
The canvas object is stored via 2 sets of coordinates [x1, y1, x2, y2]. You should check against the objects current location by using the .coords() method. The dimensions of the canvas object will affect the coordinates.
def left(event):
x = -10
y = 0
if my_canvas.coords(my_circle)[0] > 0: # index 0 is X coord left side object.
my_canvas.move(my_circle, x, y)
def right(event):
x = 10
y = 0
# The border collision now happens at 600 as per var "w" as previously defined above.
if my_canvas.coords(my_circle)[2] < w: # index 2 is X coord right side object.
my_canvas.move(my_circle, x, y)
Now repeat a similar process for up and down.

When opening image with PILLOW the image is modified

When opening the postscript image with online tools all the pixels align correctly but when using pillow, the pixels are in different sizes.
[Image of the problem]
[Image of the desired result]
def saveFile(canvas:tk.Canvas):
EpsImagePlugin.gs_windows_binary = r'C:\Program Files\gs\gs9.56.1\bin\gswin64c'
file_name = "img"
canvas.postscript(file=f"images\ps\{file_name}.ps", colormode='color')
psimage=Image.open(f'images\ps\{file_name}.ps')
psimage.save(f'images\png\{file_name}.png', "png")
psimage.close()
Keep in mind the pixels are not the size of a 'real' pixel, they are much bigger and changing the format to 'png' or 'jpg' didn't solve the problem.
If someone knows the solution to this problem, I will greatly appreciate it.
Sorry for the missing information, hopefully this is enough.
Img.ps file as text
And this is how the code generates the .ps file
import tkinter as tk
import random
from save import saveFile
pixels = []
FIELD_SIZE = (1280, 720)
PIXEL_SIZE = 10
class random_pixels():
def draw_full_screen(canvas):
height = round(FIELD_SIZE[1] / PIXEL_SIZE)
width = round(FIELD_SIZE[0] / PIXEL_SIZE)
for y in range(height):
for x in range(width):
color = ["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])]
# color = "#444"
x_top_left = x * PIXEL_SIZE + 1
y_top_left = y * PIXEL_SIZE + 1
x_bottom_right = x_top_left + PIXEL_SIZE - 1
y_bottom_right = y_top_left + PIXEL_SIZE - 1
resolution = width * height
util.draw_pixel(canvas, color, x_top_left, x_bottom_right, y_top_left, y_bottom_right, resolution)
canvas.update()
canvas.update()
print("\nPixels drawn!")
print("\nSaving image...")
saveFile(canvas)
canvas.focus_set()
print('\nImage saved!')
class util:
def draw_pixel(canvas:tk.Canvas, color, x0, x, y0, y, pixels_to_draw=1):
pixel = canvas.create_rectangle(x0, y0, x, y, fill=color, outline=color)
pixels.append(pixel)
print(f"{len(pixels)}/{pixels_to_draw} | { round(len(pixels) / pixels_to_draw * 100, 2)}%")
return None
def get_theme(e, canvas, root):
if len(pixels) != 0:
canvas.delete('all')
pixels.clear()
root.focus_set()
if e.char == "g":
random_pixels.draw_full_screen(canvas)
canvas.focus_set()

A few minor problems with my tkinter sand simulation

I have made a sand simulation in tkinter. Only tkinter. No pygame, no matplotlib, none of that. It works great, it looks great, but nothing in life is perfect.
Minor Problem #1: When I hold down and let the sand fall, the column where the cursor is stays the same color. I haven't been able to track this down yet.
Minor Problem #2: When I create happy little piles of sand, the sides seem to build up from the bottom rather that fall from the top. I suspect this is a rendering issue, but I also haven't found the reason for this.
That's all the problems I've noticed, but if you see any others feel free to let me know.
Code:
from tkinter import *
from random import choice, random
from copy import deepcopy
from colorsys import hsv_to_rgb
CELLSIZE = 30
AIR = 0
WALL = 1
SAND = 2
BG = '#cef'
SANDCOLOR = (45, 45, 86)
WALLCOLOR = (224, 37, 34)
TARGETFPS = 100
def randomColor(h, s, v):
h, s, v = (h / 360), s / 100, v / 100
s += (random() - 0.5) * 0.1
v += (random() - 0.5) * 0.1
if s < 0: s = 0
if s > 1: s = 1
if v < 0: v = 0
if v > 1: v = 1
r, g, b = [round(i * 255) for i in hsv_to_rgb(h, s, v)]
return '#%02x%02x%02x' % (r, g, b)
class App:
def __init__(self):
global WIDTH, HEIGHT, SANDCOLOR, WALLCOLOR
self.master = Tk()
self.master.title('Sand Simulation')
self.master.resizable(0, 0)
self.master.attributes('-fullscreen', True)
WIDTH = self.master.winfo_screenwidth() // CELLSIZE
HEIGHT = self.master.winfo_screenheight() // CELLSIZE
Width, Height = WIDTH * CELLSIZE, HEIGHT * CELLSIZE
self.canvas = Canvas(self.master, width=Width, height=Height, bg=BG, highlightthickness=0)
self.canvas.pack()
self.map = [[AIR] * WIDTH for i in range(HEIGHT)]
self.colors = [[BG] * WIDTH for i in range(HEIGHT)]
self.positions = []
for x in range(WIDTH):
for y in range(HEIGHT):
self.positions.append([x, y])
self.positions.reverse()
self.dragging, self.dragX, self.dragY = False, 0, 0
self.canvas.bind('<Button-1>', self.mouseDown)
self.canvas.bind('<B1-Motion>', self.mouseDrag)
self.canvas.bind('<ButtonRelease-1>', self.mouseUp)
## self.images = [PhotoImage(file='images/sandButton.png'), PhotoImage(file='images/sandButtonActivated.png'),
## PhotoImage(file='images/wallButton.png'), PhotoImage(file='images/wallButtonActivated.png')]
self.images = [PhotoImage().blank(), PhotoImage().blank(), PhotoImage().blank(), PhotoImage().blank()]
self.sandButton = self.canvas.create_image(125, 125, anchor='center', image=self.images[1])
self.wallButton = self.canvas.create_image(125, 325, anchor='center', image=self.images[2])
self.drawingMode = 'SAND'
self.master.after(round(1 / TARGETFPS * 1000), self.frame)
self.master.mainloop()
def swapBlocks(self, x1, y1, x2, y2):
block1 = self.map[y1][x1]
color1 = self.colors[y1][x1]
self.map[y1][x1] = self.map[y2][x2]
self.colors[y1][x1] = self.colors[y2][x2]
self.map[y2][x2] = block1
self.colors[y2][x2] = color1
def mouseDown(self, event):
if 50 < event.x < 200 and 50 < event.y < 200:
self.drawingMode = 'SAND'
self.canvas.itemconfig(self.sandButton, image=self.images[1])
self.canvas.itemconfig(self.wallButton, image=self.images[2])
elif 50 < event.x < 200 and 250 < event.y < 400:
self.drawingMode = 'WALL'
self.canvas.itemconfig(self.sandButton, image=self.images[0])
self.canvas.itemconfig(self.wallButton, image=self.images[3])
else:
self.dragging = True
self.dragX = event.x // CELLSIZE
self.dragY = event.y // CELLSIZE
if self.dragX > WIDTH - 1: self.dragX = WIDTH - 1
if self.dragX < 0: self.dragX = 0
if self.dragY > HEIGHT - 1: self.dragY = HEIGHT - 1
if self.dragY < 0: self.dragY = 0
def mouseDrag(self, event):
self.dragX = event.x // CELLSIZE
self.dragY = event.y // CELLSIZE
if self.dragX > WIDTH - 1: self.dragX = WIDTH - 1
if self.dragX < 0: self.dragX = 0
if self.dragY > HEIGHT - 1: self.dragY = HEIGHT - 1
if self.dragY < 0: self.dragY = 0
def mouseUp(self, event):
self.dragging = False
def updateParticles(self):
if self.dragging:
color = choice(['red', 'white', 'blue'])
if self.drawingMode == 'SAND':
self.map[self.dragY][self.dragX] = SAND
self.colors[self.dragY][self.dragX] = randomColor(SANDCOLOR[0], SANDCOLOR[1], SANDCOLOR[2])
elif self.drawingMode == 'WALL':
self.map[self.dragY][self.dragX] = WALL
self.colors[self.dragY][self.dragX] = randomColor(WALLCOLOR[0], WALLCOLOR[1], WALLCOLOR[2])
for block in self.positions:
x, y = block
block = self.map[y][x]
if block == SAND:
if y == HEIGHT - 1:
below = WALL
else:
below = self.map[y + 1][x]
if below == AIR:
self.swapBlocks(x, y, x, y + 1)
else:
left, right, belowLeft, belowRight = AIR, AIR, AIR, AIR
if y == HEIGHT - 1:
belowLeft, belowRight = WALL, WALL
else:
if x == 0:
belowLeft = WALL
left = WALL
else:
belowLeft = self.map[y + 1][x - 1]
left = self.map[y][x - 1]
if x == WIDTH - 1:
belowRight = WALL
right = WALL
else:
belowRight = self.map[y + 1][x + 1]
right = self.map[y][x + 1]
if belowLeft == AIR and belowRight == AIR:
if choice([True, False]):
if left == AIR:
self.swapBlocks(x, y, x - 1, y + 1)
else:
if right == AIR:
self.swapBlocks(x, y, x + 1, y + 1)
else:
if belowLeft == AIR and left == AIR:
self.swapBlocks(x, y, x - 1, y + 1)
if belowRight == AIR and right == AIR:
self.swapBlocks(x, y, x + 1, y + 1)
def renderMap(self, previousMap):
for block in self.positions:
x, y = block
previousBlock = previousMap[y][x]
currentBlock = self.map[y][x]
x1, y1 = x * CELLSIZE, y * CELLSIZE
x2, y2 = x1 + CELLSIZE, y1 + CELLSIZE
if previousBlock == AIR and currentBlock != AIR:
if currentBlock == WALL: color = self.colors[y][x]
if currentBlock == SAND: color = self.colors[y][x]
rect = self.canvas.create_rectangle(x1, y1, x2, y2, outline='', fill=color)
self.canvas.tag_lower(rect)
if previousBlock != AIR and currentBlock == AIR:
blockAtPosition = self.canvas.find_enclosed(x1, y1, x2, y2)
self.canvas.delete(blockAtPosition)
if previousBlock != AIR and currentBlock != AIR and previousBlock != currentBlock:
blockAtPosition = self.canvas.find_enclosed(x1, y1, x2, y2)
self.canvas.delete(blockAtPosition)
if currentBlock == WALL: color = self.colors[y][x]
if currentBlock == SAND: color = self.colors[y][x]
rect = self.canvas.create_rectangle(x1, y1, x2, y2, outline='', fill=color)
self.canvas.tag_lower(rect)
self.canvas.update()
def frame(self):
previousMap = deepcopy(self.map)
self.updateParticles()
self.renderMap(previousMap)
self.master.after(round(1 / TARGETFPS * 1000), self.frame)
def main():
app = App()
if __name__ == '__main__':
main()
Please help me fix any bugs, glitches, etc...
Problem #1 is caused by the fact that your rendering function doesn't create a new rectangle if both the old and new block types are sand. As the sand falls in a column, the first bit of sand causes rectangles to be created with its color, and because another bit of sand always falls in its place on the next frame, it just stays that color. You could compare old and new colors to see which blocks need refreshing, or store which blocks changed.
Problem #2 is related to the order of your positions. You're inserting [x,y] positions into the positions array from the left to the right, one column at a time, each column from top to bottom, and then reversing the result, which gives you an ordering of bottom to top per column with the columns from right to left. So when you iterate through the positions, you're essentially sweeping from right to left as you process the blocks, so any block that falls to the left is going to be reprocessed in the same update, and as it keeps falling to the left it will keep being reprocessed, until it settles, and all of that happened in one frame. So particles will seem to fall to the left instantly.
I bet you don't have this problem with particles falling to the right, which is why on the right side of your image you have some diagonal bands of color: problem #1 is happening there too any time two sand blocks fall to the right in sequence.
You can fix problem #2 by reordering your width and height loops when you set up self.positions. Since blocks always move down one row when they are moved, iterating from bottom to top will always update each particle only once. If you ever introduce anything that makes particles move up, you'll need a better solution.

Convert Current Image to Black and White

The goal is to convert the current image in GUI window to black and white
Below is my code:
def BlackAndWhite(self):
from images import Image
LoadAFile = self.inputText.getText()
CurrentImage = open(LoadAFile)
image = self.image = PhotoImage(file = LoadAFile)
image.draw()
BlackAndWhite(image)
image.draw()
self.imageLabel["image"] = self.image
blackPixel = (0,0,0)
whitePixel = (255,255,255)
for y in range(image.getHeight()):
for x in range(image.getWidth()):
(r,g,b) = image.getPixel(x,y)
average = (r+b+g) /3
if average < 128:
image.setPixel(x,y,blackPixel)
else:
image.setPixel(x,y, whitePixel)
I am getting this error message:
image.draw()
AttributeError: 'PhotoImage' object has no attribute 'draw'
Here's working code, you should be able to tweak it to work with your work:
from tkinter import Tk, Canvas, NW
from PIL import ImageTk, Image
root = Tk()
canvas = Canvas(root, width=1000, height=1000)
canvas.pack()
img = Image.open("PATH_TO_AN_IMAGE")
blackPixel = (0, 0, 0)
whitePixel = (255, 255, 255)
for y in range(img.height):
for x in range(img.width):
pixelVal = img.getpixel((x, y))
# Unpacking in this way in case the pixel contains more than R, G, B (ex: a png)
r, g, b = pixelVal[0:3]
average = (r + b + g) / 3
if average < 128:
img.putpixel((x, y), blackPixel)
else:
img.putpixel((x, y), whitePixel)
photoimage = ImageTk.PhotoImage(img)
canvas.create_image((20, 20), anchor=NW, image=photoimage, state="normal")
root.mainloop()

Additive color with Tkinter

I'm trying to reproduce additive color with Tkinter.
My function :
def synthese(red,green,blue):
win2 = Tk()
win2.title("ADDITIVE COLOR")
win2.geometry("500x500")
win2.resizable(0,0)
hred = "#%02x%02x%02x" % (red, 0, 0) #RGB to Hexadecimal
hgreen = "#%02x%02x%02x" % (0, green, 0)
hblue = "#%02x%02x%02x" % (0, 0, blue)
r = 50
Width = 450
Height = 450
win3 = Canvas(win2, width = Width, height = Height, bg = 'white')
win3.pack(padx=5,pady=5)
win3.create_oval(10,150,300,440, outline=hred, fill=hred)
win3.create_oval(150,150,440,440, outline=hblue, fill=hblue)
win3.create_oval(75,10,375,300, outline=hgreen, fill=hgreen)
win2.mainloop()
What I get :
And what I would like :
It is possible to merge the colors or I need to find the collision zones?
You can use ImageChops to add images.
So you can do something like this:
from Tkinter import Tk, Canvas, Label
import ImageDraw, ImageChops, Image, ImageTk
image1 = Image.new("RGBA", (500, 500), color=0)
image2 = Image.new("RGBA", (500, 500), color=0)
image3 = Image.new("RGBA", (500, 500), color=0)
draw1 = ImageDraw.Draw(image1)
draw2 = ImageDraw.Draw(image2)
draw3 = ImageDraw.Draw(image3)
draw1.ellipse([10, 150, 300, 440], (128,0,0))
draw2.ellipse([150, 150, 440, 440], (0,0,128))
draw3.ellipse([75, 10, 375, 300], (0,128,0))
out = ImageChops.add(image1,image2,0.5)
out = ImageChops.add(out,image3,0.5)
win2 = Tk()
photo = ImageTk.PhotoImage(out)
label = Label(win2, image=photo)
label.pack()
win2.mainloop()
output:
Here's a way to draw additive RGB circles using Numpy. It converts the Numpy data to a Tkinter PhotoImage object using PIL (Pillow), and displays the results in a Tkinter Label. I use a black background because we're doing additive color mixing.
import numpy as np
from PIL import Image, ImageTk
import tkinter as tk
width, height = 400, 360
# Make RGB colors
red, grn, blu = np.eye(3, dtype=np.uint8) * 255
class GUI:
def __init__(self, width, height):
self.root = root = tk.Tk()
root.title('Circles')
root.geometry('%dx%d' % (width, height))
self.img_label = tk.Label(self.root)
self.img_label.pack(fill='both', expand=True)
gui = GUI(width, height)
# Increase the scale for smoother circles
scale = 4
width *= scale
height *= scale
screen = np.zeros((height, width, 3), dtype=np.uint8)
def show(fname=None):
img = Image.fromarray(screen, 'RGB')
img = img.resize((width // scale, height // scale), resample=Image.BILINEAR)
gui.photo = ImageTk.PhotoImage(image=img)
gui.img_label.config(image=gui.photo)
gui.root.update()
if fname is not None:
img.save(fname)
def disc(radius):
diameter = 2 * radius
yy, xx = np.mgrid[:diameter, :diameter] - radius
c = xx * xx + yy * yy < radius * radius
return c.reshape(diameter, diameter, 1)
def get_region(cx, cy, radius):
ylo = cy - radius
yhi = cy + radius
xlo = cx - radius
xhi = cx + radius
return screen[ylo:yhi, xlo:xhi]
radius = 120 * scale
circle = disc(radius)
cx = width // 2
cy = 130 * scale
region = get_region(cx, cy, radius)
region |= circle * red
show()
cy += 97 * scale
cx -= 56 * scale
region = get_region(cx, cy, radius)
region |= circle * grn
show()
cx += 112 * scale
region = get_region(cx, cy, radius)
region |= circle * blu
show('rgb.png')
gui.root.mainloop()
output
Using PIL you can create three grayscale layers, draw circles and use them to create expected circles but on black background.
If you use inverted layers then you get white background but with wrong circles.
With PIL you can even display it or save in file.
from PIL import Image, ImageDraw
def synthese(red=255, green=255, blue=255):
background = 0 # black
# layers in greyscale
layer_R = Image.new('L', (450, 450), background)
layer_G = Image.new('L', (450, 450), background)
layer_B = Image.new('L', (450, 450), background)
# draw circle on red layer
draw_R = ImageDraw.Draw(layer_R)
draw_R.ellipse((10,150,300,440), red)
# draw circle on green layer
draw_G = ImageDraw.Draw(layer_G)
draw_G.ellipse((150,150,440,440), green)
# draw circle on blue layer
draw_B = ImageDraw.Draw(layer_B)
draw_B.ellipse((75,10,375,300), blue)
#layer_R.show()
#layer_G.show()
#layer_B.show()
#layer_R.save('layer_r.png')
#layer_G.save('layer_g.png')
#layer_B.save('layer_b.png')
# create RGB image using greyscale layers
image_RGB = Image.merge('RGB', (layer_R, layer_G, layer_B))
# show it
image_RGB.show()
#image_RGB.save('rgb.png')
synthese(255, 255, 255)

Categories

Resources