Pillow new RGB screen not generating - python-imaging-library

I built a test program using import PIL.imagedraw etc etc and was able to use the draw.line command to draw various lines. All good. In a new program I added new code which built a matrix list of x/y/z points (and cosine/sine expressions with math.lib), an input command for some variables, and then the draw.line command as in the previous code, but now the RGB window is not showing at all. The program doesn’t end and gives no errors. (Pythonista on iOS iPad Pro)
The test program, a noob attempt at perspective projection (which works as expected):
import PIL.ImageDraw as ImageDraw,PIL.Image as Image, PIL.ImageShow as ImageShow
im = Image.new("RGB", (1200,800))
draw = ImageDraw.Draw(im)
GL_z = 0
PP_y = 0
SP_x = 300
SP_y = -400
SP_z = 600
org=600
answer=0
width=100
depth=500
height=500
SP_z1=SP_z+height
px_1 = org
py_1 = org
px_2 = org+90
py_2 = 50 + org
px_3 = -160 + org
py_3 = 550 + org
px_4 = -250 + org
py_4 = 500+org
while answer==0:
SP_z1=SP_z-height
px1_1 = ((px_1 - SP_x)/(py_1 - SP_y))*(py_1 - PP_y)
px1_2 = ((px_2 - SP_x)/(py_2 - SP_y))*(py_2 - PP_y)
px1_3 = ((px_3 - SP_x)/(py_3 - SP_y))*(py_3 - PP_y)
px1_4 = ((px_4 - SP_x)/(py_4 - SP_y))*(py_4 - PP_y)
py1_1 = ((SP_z-GL_z)/(py_1-SP_y))*(py_1-PP_y)
py1_2 = ((SP_z-GL_z)/(py_2-SP_y))*(py_2-PP_y)
py1_3 = ((SP_z-GL_z)/(py_3-SP_y))*(py_3-PP_y)
py1_4 = ((SP_z-GL_z)/(py_4-SP_y))*(py_4-PP_y)
py1_5 = ((SP_z1-GL_z)/(py_1-SP_y))*(py_1-PP_y)
py1_6 = ((SP_z1-GL_z)/(py_2-SP_y))*(py_2-PP_y)
py1_7 = ((SP_z1-GL_z)/(py_3-SP_y))*(py_3-PP_y)
py1_8 = ((SP_z1-GL_z)/(py_4-SP_y))*(py_4-PP_y)
px1_1old = px1_1
px1_2old = px1_2
px1_3old = px1_3
px1_4old = px1_4
py1_1old = py1_1
py1_2old = py1_2
py1_3old = py1_3
py1_4old = py1_4
py1_5old = py1_5
py1_6old = py1_6
py1_7old = py1_7
py1_8old = py1_8
SP_yold = SP_y
draw.line((0,SP_y,1200,SP_y),fill=(255,255,255))
draw.line((px1_1,py1_1,px1_2,py1_2),fill=(0,0,255))
draw.line((px1_2,py1_2,px1_3,py1_3),fill=(0,255,0))
draw.line((px1_3,py1_3,px1_4,py1_4),fill=(255,255,255))
draw.line((px1_4,py1_4,px1_1,py1_1),fill=(100,255,0))
draw.line((px1_1,py1_1,px1_1,py1_5),fill=(255,255,255))
draw.line((px1_1,py1_5,px1_2,py1_6),fill=(255,255,255))
draw.line((px1_2,py1_6,px1_3,py1_7),fill=(255,255,255))
draw.line((px1_3,py1_7,px1_4,py1_8),fill=(255,255,255))
draw.line((px1_4,py1_8,px1_4,py1_4),fill=(150,150,0))
#draw.line((px1_1,py1_1,px1_2,py1_2),fill=(255,255,255))
#draw.line((px1_2,py1_2,px1_3,py1_3),fill=(255,255,255))
#draw.line((px1_3,py1_3,px1_4,py1_4),fill=(255,255,255))
#draw.line((px1_4,py1_4,px1_1,py1_1),fill=(255,255,255))
im.show()
move = input("''")
if move == "w":
SP_y += 50
if move == "s":
SP_x -= 50
if move == "z":
SP_y -= 50
if move == "a":
SP_x += 50
if move == "-":
SP_z -= 50
if move == "=":
SP_z += 50
draw.line((0,SP_yold,1200,SP_yold),fill=(0,0,0))
draw.line((px1_1old,py1_1old,px1_2old,py1_2old),fill=(0,0,0))
draw.line((px1_2old,py1_2old,px1_3old,py1_3old),fill=(0,0,0))
draw.line((px1_3old,py1_3old,px1_4old,py1_4old),fill=(0,0,0))
draw.line((px1_4old,py1_4old,px1_1old,py1_1old),fill=(0,0,0))
draw.line((px1_1old,py1_1old,px1_1old,py1_5old),fill=(0,0,0))
draw.line((px1_1old,py1_5old,px1_2old,py1_6old),fill=(0,0,0))
draw.line((px1_2old,py1_6old,px1_3old,py1_7old),fill=(0,0,0))
draw.line((px1_3old,py1_7old,px1_4old,py1_8old),fill=(0,0,0))
draw.line((px1_4old,py1_8old,px1_4old,py1_4old),fill=(0,0,0))
And this is the code that refuses to draw anything:
import math
import decimal
decimal.getcontext().prec = 6
import PIL.ImageDraw as ImageDraw,PIL.Image as Image, PIL.ImageShow as ImageShow
im = Image.new("RGB", (1200,800))
draw = ImageDraw.Draw(im)
PP_y = 0
SP_x = 300
SP_y = -400
SP_z = 600
org=600
answer=0
dims=[]
for i in range (1, 5):
dim = int(input())
dims.append(dim)
dims[3] = (dims[3]*math.pi)/180
obj1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18],[19,20,21],[22,23,24]]
y1 = [0,0,0,0,0,0,0,0]
y2 = [0,0,0,0,0,0,0,0]
y3 = [0,0,0,0,0,0,0,0]
x1 = [0,0,0,0,0,0,0,0]
z1 = [0,0,0,0,0,0,0,0]
ymod = [0,0,0,0,0,0,0,0]
xmod = [0,0,0,0,0,0,0,0]
obj1[0]=[0,0,0]
obj1[1]=[dims[0] * math.cos(dims[3]),dims[0] * math.sin(dims[3]),0]
obj1[2]=[obj1[1][0] - (dims[1] * math.sin(dims[3])),obj1[1][1] + (dims[1] * math.cos(dims[3])),0]
obj1[3]=[dims[1] * math.sin(dims[3]),dims[1] * math.cos(dims[3]),0]
obj1[4]=[0,0,dims[2]]
obj1[5]=[dims[0] * math.cos(dims[3]),dims[0] * math.sin(dims[3]),dims[2]]
obj1[6]=[obj1[1][0] - (dims[1] * math.sin(dims[3])),obj1[1][1] + (dims[1] * math.cos(dims[3])),dims[2]]
obj1[7]=[dims[1] * math.sin(dims[3]),dims[1] * math.cos(dims[3]),dims[2]]
for i in range (0,7):
y1[i]=obj1[i][1]-PP_y
y2[i]=obj1[i][1]-SP_y
y3[i]=y1[i]/y2[i]
x1[i]=obj1[i][0]-SP_y
z1[i]=obj1[i][2]-SP_z
obj1[i][0]=obj1[i][0] - (x1[i] * y3[i])
obj1[i][2]=obj1[i][2] - (z1[i] * y3[i])
#print(dims)
#print(obj1)
#print(y1,y2,y3,x1,z1,xmod,ymod)
while (True):
draw.line((obj1[0][0],obj1[0][2],obj1[1][0],obj1[1][2]),fill=(255,255,255))

Ah found the error, left out im.show(). Pretty fundamental…
reference
https://pillow.readthedocs.io/en/stable/reference/Image.html

Related

Condition only run ONCE (instead on all bars)

On checking for condition gap(high-low) > 0.1%(which is met multiple times), the label only gets
rendered ONCE (instead of on relevant bars within 25 bar lookback).
Plz provide a solution.
CODE :
Historical Bars
//#version=5
indicator("PriceMomemtum",overlay = true,max_bars_back = 25)
gap = (math.abs(high - low)/low ) * 100
//var gap = (math.abs(high - low)/low ) * 100
if gap > 0.1
var lbl = label.new(x = bar_index,y = na , text = na ,text_font_family = font.family_default ,xloc = xloc.bar_index,yloc =yloc.abovebar,style = label.style_arrowdown ,textcolor = color.white,size =size.small,textalign = text.align_left,tooltip = na)
label.set_text(lbl,str.tostring(gap,"#.00")+"%")
label.set_xy(lbl,bar_index,high )
Realtime Bars
//#version=5
indicator("PriceMomemtum",overlay = true,max_bars_back = 25)
if barstate.isrealtime
gap = (math.abs(high - low)/low ) * 100
//var gap = (math.abs(high - low)/low ) * 100
if gap > 0.1
var lbl = label.new(x = bar_index,y = na , text = na ,text_font_family = font.family_default ,xloc = xloc.bar_index,yloc =yloc.abovebar,style = label.style_arrowdown ,textcolor = color.white,size =size.small,textalign = text.align_left,tooltip = na)
label.set_text(lbl,str.tostring(gap,"#.00")+"%")
label.set_xy(lbl,bar_index,high )
alert(str.tostring(time(syminfo.timezone)) + "(PriceMomentum)", alert.freq_once_per_bar)
Have you tried defining "lbl" variable without "var"?
result

How to get SubtractAsync to fully slice off piece of the part

I'm trying to slice a part of this part, so that it matches the bottom part, and the method I'm trying is SubtractAsync.
When I do it however, the part gets sliced, but not so that the remaining part gets removed. Here's what I mean:
How do I edit my code to slice off the piece of the part?
My code:
local Brick = workspace.Brick
local Stack = Brick:Clone()
local TS = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, math.huge, true)
Stack.Position = Vector3.new(Brick.Position.X, Brick.Position.Y + Brick.Size.Y, Brick.Position.Z - 55)
local tween = TS:Create(Stack, tweenInfo, {Position = Vector3.new(Stack.Position.X, Stack.Position.Y, Stack.Position.Z + (55 * 2))})
Stack.Parent = workspace
tween:Play()
game.ReplicatedStorage.PlaceDown.OnServerEvent:Connect(function()
tween:Pause()
local PartA = Instance.new("Part")
local PartB = Instance.new("Part")
PartA.Transparency = 1
PartB.Transparency = 1
PartA.Parent = workspace
PartB.Parent = workspace
PartA.Orientation = Vector3.new(-90, 0, 0)
PartB.Orientation = Vector3.new(-90, 0, 0)
PartA.Size = Vector3.new(15, 0.051, 15)
PartB.Size = Vector3.new(15, 0.051, 15)
PartA.Position = Vector3.new(Brick.Position.X, Brick.Position.Y + 2.5, Brick.Position.Z - (Brick.Size.Z/2))
PartB.Position = Vector3.new(Brick.Position.X, Brick.Position.Y + 2.5, Brick.Position.Z + (Brick.Size.Z/2))
PartA.Anchored = true
PartB.Anchored = true
local SlicedStack = Stack:SubtractAsync({PartA, PartB})
SlicedStack.Position = Stack.Position
SlicedStack.Parent = workspace
Stack:Destroy()
PartA:Destroy()
PartB:Destroy()
end)

How to find the first value of Bollinger Bands when bar open

Actually, the Bollinger Bands code is:
//#version=4
study(title="AAAA", shorttitle="AAAA", overlay=true)
len = 5
multi = 2
bb5med = sma(close, len)
devBB5 = mult2 * stdev(close, len)
bb5top = bb5med + devBB5
bb5bot = bb5med - devBB5
I would want to find the first value of those 3 lines when the new bar comes, means, when close==open.
Also, I need it to work when I change the len to 20, 50 and/or when I change the multi to 3
Please help me. Thank you.
//#version=5
indicator("BB Open", overlay = true)
len = input.int(20)
mult = input.float(2.000)
basis = (math.sum(close, len - 1)[1] + open) / len
float dev_sum = 0.0
for i = 1 to len - 1
dev_sum += math.pow(basis - close[i], 2)
dev_sum += math.pow(basis - open, 2)
stdev = math.sqrt(dev_sum / len)
up = basis + stdev * mult
dn = basis - stdev * mult
plot(basis, color = color.yellow)
plot(up)
plot(dn)
Function :
f_BBopen(_close, _open, _len, _mult) =>
_basis = (math.sum(_close, _len - 1)[1] + _open) / _len
float _dev_sum = 0.0
for i = 1 to _len - 1
_dev_sum += math.pow(_basis - _close[i], 2)
_dev_sum += math.pow(_basis - _open, 2)
_stdev = math.sqrt(_dev_sum / _len)
_up = _basis + _stdev * _mult
_dn = _basis - _stdev * _mult
[_basis, _up, _dn]
[basis, up, dn] = f_BBopen(close, open, len, mult)

MATLAB taking hours to solve an equation

I've been trying to solve an equation composed of matrices. MATLAB has run it for almost two hours now and nothing. It's supposed to multiply some 8x8 matrices and invert one 8x8 matrix. Should it take this long? I'm using an I7 8th gen 2.20 running at 4ghz, 16 gb and RTX 2060.
Code (the final line is where MATLAB is taking so long):
%% Modelagem do conversor SEPIC-Zeta em espaço de estados
%% Declarando variáveis simbólicas:
% Variáveis com o índice "_" são derivadas de primeira ordem.
% RL1, RL2, RL3 e RL4 são as resistências série dos indutores.
% RseC1, RseC2, RseC3 e RseC4 são as resistências série dos capacitores.
Vi = sym('Vi');
C1 = sym('C1');
C2 = sym('C2');
C3 = sym('C3');
C4 = sym('C4');
L1 = sym('L1');
L2 = sym('L2');
L3 = sym('L3');
L4 = sym('L4');
VC1 = sym('VC1');
VC2 = sym('VC2');
VC3 = sym('VC3');
VC4 = sym('VC4');
VC1_ = sym('VC1_');
VC2_ = sym('VC2_');
VC3_ = sym('VC3_');
VC4_ = sym('VC4_');
IL1 = sym('IL1');
IL2 = sym('IL2');
IL3 = sym('IL3');
IL4 = sym('IL4');
IL1_ = sym('IL1_');
IL2_ = sym('IL2_');
IL3_ = sym('IL3_');
IL4_ = sym('IL4_');
RL1 = sym('RL1');
RL2 = sym('RL2');
RL3 = sym('RL3');
RL4 = sym('RL4');
RseC1 = sym('RseC1');
RseC2 = sym('RseC2');
RseC3 = sym('RseC3');
RseC4 = sym('RseC4');
Ro = sym('Ro');
D = sym('D');
s = sym('s');
%% Determinando as matrizes de coeficientes do conversor em t = [0, DTs]:
% Equações LKT no conversor:
eqn11 = IL1_ - (-RL1*IL1 + Vi)/L1 == 0;
eqn21 = IL2_ - ((RseC1 - RL2)*IL2 - VC1)/L2 == 0;
eqn31 = IL3_ - (VC2 + (RseC2 - RL3)*IL3 + RseC2*IL4)/L3 == 0;
eqn41 = IL4_ - ((-VC3 + RseC2*C2*VC2_ - (RseC3 + RL4)*IL4 + Ro*C4*VC4_ + VC2)/L4) == 0;
eqn51 = VC1_ - (Vi - RL1*IL1 + L1*IL1_ + VC1 - L2*IL2_)/C1*(RseC1 - RL2) == 0;
eqn61 = VC2_ - (-RseC1*IL2 - VC1 - RL2*IL2 - L2*IL2_ + VC2 + L3*IL3_ + RL3*IL3)/(C2*RseC2) == 0;
eqn71 = VC3_ - (Ro*IL4 - Ro*C4*VC4_ - VC3 - L4*IL4_ + L3*IL3_ + RL3*IL3 - RL4*IL4)/(C3*RseC3) == 0;
eqn81 = VC4_ - (Ro*IL4 - VC4)/(C4*(RseC4 + Ro)) == 0;
% Solucionando o sistema de equações lineares:
sol1 = solve([eqn11, eqn21, eqn31, eqn41, eqn51, eqn61, eqn71, eqn81], [IL1_, IL2_, IL3_, IL4_, VC1_, VC2_, VC3_, VC4_]);
% Separando as matrizes A e B dos coeficientes:
system_A1 = [sol1.IL1_ == IL1_, sol1.IL2_ == IL2_, sol1.IL3_ == IL3_, sol1.IL4_ == IL4_,...
sol1.VC1_ == VC1_, sol1.VC2_ == VC2_, sol1.VC3_ == VC3_, sol1.VC4_ == VC4_];
vars1 = [ IL1, IL2, IL3, IL4, VC1, VC2, VC3, VC4 ];
vars21 = [ Vi ];
% Definindo as matrizes A1, B1, C1 e D1:
A1 = equationsToMatrix(system_A1,vars1);
B1 = equationsToMatrix(system_A1,vars21);
C1_ = [ 0; 0; 0; RseC4*Ro/(RseC4 + Ro); 0; 0; 0; Ro/(RseC4 + Ro) ];
D1 = 0;
%% Deterninando as matrizes dos coeficientes em t = [DTs, Ts]:
eqn12 = IL1_ - (-Vi + IL1*(RseC1 - RL1) + VC1 + RL2*IL2 + L2*IL2_)/L1 == 0;
eqn22 = IL2_ - (RseC2*IL1 + IL2*(-RL2 + RseC2) + VC2)/L2 == 0;
eqn32 = IL3_ - (-VC3 -RL3*IL3 - RseC3*IL3)/L3 == 0;
eqn42 = IL4_ - ((IL4*RL4 - Ro*IL4 - Ro*C4*VC4_)/L4) == 0;
eqn52 = VC1_ - (Vi - RseC1*IL1 - L1*IL1_ - VC1 + RL2*IL2 + L2*IL2_)/C1*(RseC1 + RseC2 - RL1) == 0;
eqn62 = VC2_ - (RL2*IL2 + L2*IL2_ - VC2)/(C2*RseC2) == 0;
eqn72 = VC3_ - (-Ro*IL4 - Ro*C4*VC4_ - VC3 + L4*IL4_ - L3*IL3_ + RL4*IL4)/(C3*(RL3 + RseC3)) == 0;
eqn82 = VC4_ - (Ro*IL4 - VC4)/(C4*(RseC4 - Ro)) == 0;
% Solucionando o sistema de equações lineares:
sol2 = solve([eqn12, eqn22, eqn32, eqn42, eqn52, eqn62, eqn72, eqn82], [IL1_, IL2_, IL3_, IL4_, VC1_, VC2_, VC3_, VC4_]);
% Separando as matrizes A e B dos coeficientes:
system_A2 = [sol2.IL1_ == IL1_, sol2.IL2_ == IL2_, sol2.IL3_ == IL3_, sol2.IL4_ == IL4_,...
sol2.VC1_ == VC1_, sol2.VC2_ == VC2_, sol2.VC3_ == VC3_, sol2.VC4_ == VC4_];
vars2 = [ IL1, IL2, IL3, IL4, VC1, VC2, VC3, VC4 ];
vars22 = [ Vi ];
% Definindo as matrizes A2, B2, C2 e D2:
A2 = equationsToMatrix(system_A2,vars2);
B2 = equationsToMatrix(system_A2,vars22);
C2_ = [ 0; 0; 0; RseC4*Ro/(RseC4 - Ro); 0; 0; 0; -Ro/(RseC4 - Ro) ];
D2 = 0;
%% Equacionamento para o espaço de estados médio:
A = simplify(A1*D + A2*(1-D));
B = simplify(B1*D + B2*(1-D));
C = simplify(C1_*D + C2_*(1-D));
D = simplify(D1*D + D2*(1-D));
X = simplify(-inv(A)*B*Vi);
I = eye(8);
Tp = C*inv(s*I - A)*((A1-A2)*X + (B1-B2)*Vi)+(C1-C2)*X;
Without understanding the math in detail I can see that it slows at line 108 X = simplify(-inv(A)*B*Vi); rather than the last line, but I haven't been running it that long <1min. Try to utilize A\b instead of inv(A)*b which takes much more time.

whenever I try to run the code with Lua I get the error :Error Map.lua:107: attempt to call method 'makepyramid' (a nil value)

Map = Class{}
TILE_BRICK = 1
TILE_EMPTY = -1
POLE_TOP = 8
POLE_MIDDLE = 12
POLE_BOTTOM = 16
FLAG_TILE = 13
local SCROLL_SPEED = 62
function Map:init()
self.spritesheet = love.graphics.newImage('graphics/spritesheet.png')
self.sprites = generateQuads(self.spritesheet, 16, 16)
self.music = love.audio.newSource('sounds/music.wav', 'static')
self.tileWidth = 16
self.tileHeight = 16
self.mapWidth = 30
self.mapHeight = 28
self.tiles = {}
self.flag = self.mapWidth - 3
self.pyramid = self.mapWidth - 4
self.pyramidHeight = 8
self.gravity = 15
-- associate player with map
self.player = Player(self)
-- camera offsets
self.camX = 0
self.camY = -3
self.mapWidthPixels = self.mapWidth * self.tileWidth
self.mapHeightPixels = self.mapHeight * self.tileHeight
for y = 1, self.mapHeight do
for x = 1, self.mapWidth do
self:setTile(x, y, TILE_EMPTY)
end
end
local x = 1
while x < self.mapWidth do
if x == self.flag then
self:setTile(x, (self.mapHeight / 2) - 3, POLE_TOP)
self:setTile(x, (self.mapHeight / 2) - 2, POLE_MIDDLE)
self:setTile(x, (self.mapHeight / 2) - 1, POLE_BOTTOM)
self:setTile(x+1, (self.mapHeight / 2) - 3, FLAG_TILE)
for y = self.mapHeight / 2, self.mapHeight do
self:makepyramid(x, y, TILE_BRICK)
end
self:setTile(self.flag)
x = x+1
end
This the code that I have written it does require other files to work but the core problem exists within this code: most probably with "makepyramid" in "map init()"
please suggest some possible changes that I can make to make this code work properly. comment down if you need more chunks of code to try some solutions.