Skip to content

Instantly share code, notes, and snippets.

@nenodias
Last active April 5, 2017 16:24
Show Gist options
  • Select an option

  • Save nenodias/571c7d1b2e7661e302c2f7c3e6701888 to your computer and use it in GitHub Desktop.

Select an option

Save nenodias/571c7d1b2e7661e302c2f7c3e6701888 to your computer and use it in GitHub Desktop.
Image Processing with PILLOW
import tkinter as tk
from PIL import Image, ImageDraw, ImageTk
im = Image.new('RGB', (256, 256))
draw = ImageDraw.Draw(im)
width = 256
height = 256
cor1 = (255, 0, 0)
cor2 = (0, 0, 255)
even = True
inicio = 0
tamanho = 16
for x1 in range(inicio, height, tamanho):
even = not even
for y1 in range(inicio, width, tamanho):
x2 = x1+tamanho
y2 = y1+tamanho
coord = (x1, y1, x2, y2)
even = not even
if even:
draw.rectangle(coord, fill=cor1)
else:
draw.rectangle(coord, fill=cor2)
im.save("chessboard.png")
# Initializing tkinter
root = tk.Tk()
root.title('Shot Viewer')
w, h, x, y = 256, 256, 0, 0
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
# Loading Image on tkinter
photo = tk.PhotoImage(file="chessboard.png")
photo_label = tk.Label(image=photo)
photo_label.grid()
photo_label.image = photo
root.mainloop()
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageDraw
filetypes = (
("PNG", "*.png"),
("JPEG", "*.jpg"),
("BMP", "*.bmp")
)
root = tk.Tk()
image_name = tk.filedialog.askopenfilename(filetypes=filetypes)
im = Image.open(image_name)
width, height = im.size
if __name__ == '__main__':
from negative import negative
negative(im)
root.title('Shot Viewer')
w, h, x, y = width, height, 0, 0
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
# Loading Image on tkinter
photo = tk.PhotoImage(file=image_name)
photo_label = tk.Label(image=photo)
photo_label.grid()
photo_label.image = photo
root.mainloop()
"""Negative."""
def negative(img):
pix = img.load()
width, height = img.size
print(img.mode)
for x in range(width):
for y in range(height):
color = pix[x, y]
if img.mode == 'RGB':
r, g, b = color
new_color = (255 - r, 255 - g, 255 - b)
elif img.mode == 'RGBA':
r, g, b, a = color
new_color = (255 - r, 255 - g, 255 - b, a)
pix[x, y] = new_color
img.save(img.filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment