Cannot make new images in Python Pillow - python-imaging-library

In the following code I try to create one image with an HSB values of 0,360,360 and a second image with values of 345,360,360. With the following code:
from Pillow import Image
en = enumerate
l = (0, 360, 360)
l1 = (345, 360, 360)
lst2 = [l, l1]
for e, x in en(lst2):
img = Image.new('HSV', (500, 500), x)
img.show()
img.close()
Instead the second image is the same as the first and I don't understand why. I tried saving the images to the disk but that did not work either. (For some reason the Pillow model ranges saturation and brightness from 0 to 360 and it should be 0 to 100 but don't worry about that).
######## UPDATE
I'm able to get the code to produce different images if I put in a sleep delay. But I don't see why Pillow cannot do it without a sleep delay. Here's the updated code:
from Pillow import Image
en = enumerate
l = (0, 360, 360)
l1 = (345, 360, 360)
lst2 = [l, l1]
for e, x in en(lst2):
img = Image.new('HSV', (500, 500), x)
img.show()
img.close()
time.sleep(1)

Related

Pymunk dynamic object overlapping static object

I am starting out with pymunk and have a static floor with a dynamic object (ball). When the ball falls onto the floor there is an initial overlap where the ball is absorbed into the floor before slowly being pushed back up.
Why does this happen? Is this normal?
import pygame
import pymunk
import pymunk.pygame_util
pygame.init()
#game window
SCREEN_WIDTH, SCREEN_HEIGHT = 600, 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
#pymunk space
space = pymunk.Space()
space.gravity = (0, 981)
clock = pygame.time.Clock()
fps = 60
dt = 1/60
draw_options = pymunk.pygame_util.DrawOptions(screen)
def create_floor(space, width, height, pos):
body = pymunk.Body(body_type = pymunk.Body.STATIC)
body.position = pos
shape = pymunk.Poly.create_box(body, (width, height))
space.add(body, shape)
def create_ball(space, radius, mass):
body = pymunk.Body()
body.position = (300, 250)
shape = pymunk.Circle(body, radius)
shape.mass = mass
shape.color = (255, 0, 0, 100)
space.add(body, shape)
return shape
ball = create_ball(space, 30, 10)
create_floor(space, SCREEN_WIDTH, 20, (SCREEN_WIDTH / 2, SCREEN_HEIGHT - 10))
run = True
while run:
clock.tick(fps)
space.step(dt)
screen.fill((255, 255, 255))
space.debug_draw(draw_options)
#event handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
Yes, this is normal. There are however things that can be done to minimize this behavior:
The easiest method is to call space.step() with a smaller timestep (and instead call it more times). Replace space.step(dt) with something like this:
for _ in range(10):
space.step(dt/10)
Its also possible to limit the maximum speed that objects move with. The slower they move, the less overlap there will be. Finally, you can also try to adjust the collision_bias parameter on the space.

How to create Bezier curves from B-Splines in Sympy?

I need to draw a smooth curve through some points, which I then want to show as an SVG path. So I create a B-Spline with scipy.interpolate, and can access some arrays that I suppose fully define it. Does someone know a reasonably simple way to create Bezier curves from these arrays?
import numpy as np
from scipy import interpolate
x = np.array([-1, 0, 2])
y = np.array([ 0, 2, 0])
x = np.r_[x, x[0]]
y = np.r_[y, y[0]]
tck, u = interpolate.splprep([x, y], s=0, per=True)
cx = tck[1][0]
cy = tck[1][1]
print( 'knots: ', list(tck[0]) )
print( 'coefficients x: ', list(cx) )
print( 'coefficients y: ', list(cy) )
print( 'degree: ', tck[2] )
print( 'parameter: ', list(u) )
The red points are the 3 initial points in x and y. The green points are the 6 coefficients in cx and cy. (Their values repeat after the 3rd, so each green point has two green index numbers.)
Return values tck and u are described scipy.interpolate.splprep documentation
knots: [-1.0, -0.722, -0.372, 0.0, 0.277, 0.627, 1.0, 1.277, 1.627, 2.0]
# 0 1 2 3 4 5
coefficients x: [ 3.719, -2.137, -0.053, 3.719, -2.137, -0.053]
coefficients y: [-0.752, -0.930, 3.336, -0.752, -0.930, 3.336]
degree: 3
parameter: [0.0, 0.277, 0.627, 1.0]
Not sure starting with a B-Spline makes sense: form a catmull-rom curve through the points (with the virtual "before first" and "after last" overlaid on real points) and then convert that to a bezier curve using a relatively trivial transform? E.g. given your points p0, p1, and p2, the first segment would be a catmull-rom curve {p2,p0,p1,p2} for the segment p1--p2, {p0,p1,p2,p0} will yield p2--p0, and {p1, p2, p0, p1} will yield p0--p1. Then you trivially convert those and now you have your SVG path.
As demonstrator, hit up https://editor.p5js.org/ and paste in the following code:
var points = [{x:150, y:100 },{x:50, y:300 },{x:300, y:300 }];
// add virtual points:
points = points.concat(points);
function setup() {
createCanvas(400, 400);
tension = createSlider(1, 200, 100);
}
function draw() {
background(220);
points.forEach(p => ellipse(p.x, p.y, 4));
for (let n=0; n<3; n++) {
let [c1, c2, c3, c4] = points.slice(n,n+4);
let t = 0.06 * tension.value();
bezier(
// on-curve start point
c2.x, c2.y,
// control point 1
c2.x + (c3.x - c1.x)/t,
c2.y + (c3.y - c1.y)/t,
// control point 2
c3.x - (c4.x - c2.x)/t,
c3.y - (c4.y - c2.y)/t,
// on-curve end point
c3.x, c3.y
);
}
}
Which will look like this:
Converting that to Python code should be an almost effortless exercise: there is barely any code for us to write =)
And, of course, now you're left with creating the SVG path, but that's hardly an issue: you know all the Bezier points now, so just start building your <path d=...> string while you iterate.
A B-spline curve is just a collection of Bezier curves joined together. Therefore, it is certainly possible to convert it back to multiple Bezier curves without any loss of shape fidelity. The algorithm involved is called "knot insertion" and there are different ways to do this with the two most famous algorithm being Boehm's algorithm and Oslo algorithm. You can refer this link for more details.
Here is an almost direct answer to your question (but for the non-periodic case):
import aggdraw
import numpy as np
import scipy.interpolate as si
from PIL import Image
# from https://stackoverflow.com/a/35007804/2849934
def scipy_bspline(cv, degree=3):
""" cv: Array of control vertices
degree: Curve degree
"""
count = cv.shape[0]
degree = np.clip(degree, 1, count-1)
kv = np.clip(np.arange(count+degree+1)-degree, 0, count-degree)
max_param = count - (degree * (1-periodic))
spline = si.BSpline(kv, cv, degree)
return spline, max_param
# based on https://math.stackexchange.com/a/421572/396192
def bspline_to_bezier(cv):
cv_len = cv.shape[0]
assert cv_len >= 4, "Provide at least 4 control vertices"
spline, max_param = scipy_bspline(cv, degree=3)
for i in range(1, max_param):
spline = si.insert(i, spline, 2)
return spline.c[:3 * max_param + 1]
def draw_bezier(d, bezier):
path = aggdraw.Path()
path.moveto(*bezier[0])
for i in range(1, len(bezier) - 1, 3):
v1, v2, v = bezier[i:i+3]
path.curveto(*v1, *v2, *v)
d.path(path, aggdraw.Pen("black", 2))
cv = np.array([[ 40., 148.], [ 40., 48.],
[244., 24.], [160., 120.],
[240., 144.], [210., 260.],
[110., 250.]])
im = Image.fromarray(np.ones((400, 400, 3), dtype=np.uint8) * 255)
bezier = bspline_to_bezier(cv)
d = aggdraw.Draw(im)
draw_bezier(d, bezier)
d.flush()
# show/save im
I didn't look much into the periodic case, but hopefully it's not too difficult.

Determine the proportion of specific colors in an image

Do you have any idea how can I determine the proportion of Yellow (or Yellowish), Brown, and Red colour in a specific image? I tried to use HSV, but I could not find any threshold for H, S, and V for the aforementioned colours.
I attached a sample image.
So, let's do this one step at a time.
First how to know which color is represented by what value? For that, I referred to this stackoverflow question, from where you can get this HSV color map,
If you DuckDuckGo/Google/search for "HSV or HSL color map", you could find many examples.
Now, we can pick a color from the along the horizontal axis. We can use a single value e.g. 120 for dark blue or we can use a range of values e.g. 45 to 80 for all hues of green.
What I wasn't sure about was, how to define the,
proportion of Yellow (or Yellowish), Brown, and Red colour in a specific image
as you ask in your question.
I thought of then two ways to represent the color proportion.
Proportion of pixels containing some portion of that hue.
Proportion of the specific hue relative to all the hues in the image.
Then using the following script, you could get some numbers:
NOTE: (This the Python script, I originally posted. The corresponding Matlab script is further down the post.)
import cv2
import numpy as np
img = cv2.imread("D:\\lenna.jpg")
height_img, width_img, channels_img = img.shape
# Converts images from RGB to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask1 = cv2.inRange(hsv, (150, 0, 0), (150, 255,255)) #150 seems like pinkish
mask2 = cv2.inRange(hsv, (1,0,0), (20, 255, 255)) #1 to 20 seems orangish
total_num_of_pixels = height_img * width_img
all_colors = np.sum(hsv[:,:,:] > 0)
num_of_pixels_with_pinkish_component = np.sum(mask1 > 0)
num_of_pixels_with_orangish_component = np.sum(mask2 > 0)
print("%age of pixels with pinkish component:", "{:.2f}".format(num_of_pixels_with_pinkish_component/total_num_of_pixels * 100))
print("%age of pixels with orangish component:", "{:.2f}".format(num_of_pixels_with_orangish_component/total_num_of_pixels * 100))
print("%age of pinkish component in the entire HSV image:", "{:.2f}".format(num_of_pixels_with_pinkish_component/all_colors * 100))
print("%age of orangish in the entire HSV image:", "{:.2f}".format(num_of_pixels_with_orangish_component/all_colors * 100))
# To visualize the results
res1 = cv2.bitwise_and(img, img, mask=mask1)
res2 = cv2.bitwise_and(img, img, mask=mask2)
cv2.imshow('img', img)
cv2.imshow('mask1', mask1)
cv2.imshow('mask2', mask2)
cv2.imshow('res1', res1)
cv2.imshow('res2', res2)
# To save the output
cv2.imwrite('D:\\mask1.png', mask1)
cv2.imwrite('D:\\mask2.png', mask2)
cv2.imwrite('D:\\res1.png', res1)
cv2.imwrite('D:\\res2.png', res2)
Output:
%age of pixels with pinkish component: 0.41
%age of pixels with orangish component: 35.58
%age of pinkish component in the entire HSV image: 0.15
%age of orangish in the entire HSV image: 13.27
Here is how the output looks then:
MASK1 (150 on the hue axis seems pinkish)
MASK2 (1 ~ 20 on the hue axis seems orange)
RES1
RES2
Here is the equivalent MATLAB script.
close all;
clear all;
clc;
img = imread("/home/junglefox/Downloads/lenna.png");
figure, imshow(img), title('original image (RGB)');
img_size = size(img);
hsv_img = rgb2hsv(img);
hsv_img = im2uint8(hsv_img);
figure, imshow(hsv_img), title('original image in HSV');
% Orange component between 1 and 20 on the HSV map
minval = [1 0 0]; %// Define three element vector here for each colour plane i.e. [0 128 128];
maxval = [20 255 255]; %// Define three element vector here for each colour plane i.e. [0 128 128];
out = true(img_size(1), img_size(2));
for p = 1 : 3
out = out & (hsv_img(:,:,p) >= minval(p) & hsv_img(:,:,p) <= maxval(p));
end
figure, imshow(out), title('image of orange component in image only');
total_num_of_pixels = img_size(1) * img_size(2);
all_colors = sum(hsv_img(:,:,:) > 0);
num_of_pixels_with_orangish_component = sum(sum(out > 0));
percentage_orange = num_of_pixels_with_orangish_component/total_num_of_pixels * 100;
printf("percentage of orange component in all pixels:%d\n", percentage_orange);

How to make scoreborad in tkinter

I would like to implement a scoreboard using a tkinter.
and i want make if the distance between the object bullet and the object enemy is less than 10, I want to increase the score by 10.
How do I add code?
Thank you in advance.
from tkinter import *
import time
import random
WIDTH = 800
HEIGHT = 800
class Ball:
def __init__(self, canvas, color, size, x, y, xspeed, yspeed):
self.canvas = canvas
self.color = color
self.size = size
self.x = x
self.y = y
self.xspeed = xspeed
self.yspeed = yspeed
self.id = canvas.create_oval(x, y, x+size, y+size, fill=color)
def move(self):
self.canvas.move(self.id, self.xspeed, self.yspeed)
(x1, y1, x2, y2) = self.canvas.coords(self.id)
(self.x, self.y) = (x1, y1)
if x1 <= 0 or x2 >= WIDTH:
self.xspeed = - self.xspeed
if y1 <= 0 or y2 >= HEIGHT:
self.yspeed = - self.yspeed
bullets = []
def fire(event):
bullets.append(Ball(canvas, "red", 10, 150, 250, 10, 0))
def up(event):
spaceship.yspeed-=1
def down(event):
spaceship.yspeed+=1
window = Tk()
canvas = Canvas(window, width=WIDTH, height=HEIGHT)
canvas.pack()
canvas.bind("<Button-1>", fire)
window.bind("<Up>",up)
window.bind("<Down>",down)
spaceship = Ball(canvas, "green", 100, 100, 200, 0, 0)
enemy = Ball(canvas, "red", 100, 500, 200, 5, 0)
while True:
for bullet in bullets:
bullet.move()
if (bullet.x+bullet.size) >= WIDTH:
canvas.delete(bullet.id)
bullets.remove(bullet)
enemy.move()
spaceship.move()
window.update()
time.sleep(0.03)
There are many ways to improve your program but I will concentrate on the collision aspect only.
Tkinter canvas has several methods; find_closest, find_enclosed and find_overlapping (See here) to allow you to detect where objects are in relation to each other. find_closest would be my first choice.
find_closest should take the x, y coordinates of the 'enemy' and a 'halo distance' (your less than 10 pixels). This will return the id's of objects nearby. If one of those objects is a bullet, then add 10 points to the score.
Some other things to fix/work on
You don't have a tkinter.mainloop. You should
Your method for moving the spaceship by changing the speed rather than the xy coords is poor and ends up with a very fast moving spaceship.

Converting PIL image to GTK pixmap with alpha

So I need to take an image I made in PIL and convert it to a pixmap to be displayed in a drawable.
How do I convert from PIL to pixmap and keep the images alpha?
Currently I have this code written:
def gfx_draw_tem2(self, r, x, y):
#im = Image.open("TEM/TEM cropped.png")
im = Image.new("RGBA", (r*2,r*2), (255, 255, 255, 255))
draw = ImageDraw.Draw(im)
for i in range(0,r*2):
for j in range(0,r*2):
if(self.in_circle(i,j,r)):
draw.point((i,j), fill=(100,50,75,50)) #alpha at 255 for test2.png
im.save("test.png")
im_data = im.tostring()
pixbuf = gdk.pixbuf_new_from_data(im_data, gdk.COLORSPACE_RGB, True, 8, im.size[0], im.size[1], 4*im.size[0])
pixmap2, mask = pixbuf.render_pixmap_and_mask()
self.pixmap.draw_drawable(self.white_gc, pixmap2, 0,0,x-r,y-r,-1,-1)
Here are the images I created from im.save("test.png"):
http://imgur.com/43spsBG,lqowten#0
Notice the first picture has an alpha of 255 (full) and the seconds has an alpha of 50.
However When I convert the images to a pixmap with my current code I lose the transparent affect.
Thanks for your help,
Ian
EDIT: I have narrowed it down a little bit with more testing. I am losing the alpha of my image when converting the pixbuf to a pixmap.
Okay figured it out.
Trick here is to not convert the pixbuf to a pixmap using pixbuf.render_pixmap_and_mask()
Instead I took my self.pixmap that I draw onto my drawable and called draw_pixbuf() on it.
Here is the new code I used.
def gfx_draw_tem2(self, r, x, y):
im = Image.new("RGBA", (r*2,r*2), (1, 1, 1, 0))
draw = ImageDraw.Draw(im)
for i in range(0,r*2):
for j in range(0,r*2):
if(self.in_circle(i,j,r)):
draw.point((i,j), fill=(100,50,75,140))
im_data = im.tostring()
pixbuf = gdk.pixbuf_new_from_data(im_data, gdk.COLORSPACE_RGB, True, 8, im.size[0], im.size[1], 4*im.size[0])
self.pixmap.draw_pixbuf(self.white_gc, pixbuf, 0, 0, x, y, -1, -1, gdk.RGB_DITHER_NORMAL, 0, 0)
Hope this helps someone.