I have looked around extensively trying to find a method to fade between one color to another in Python. Most of the examples I've found are usually specific to a device or in another language which doesn't translate well.
I currently have a modified piece of code which was used to create a breathing effect which worked pretty well fading from 0 to 255 for example. What I have now sets the from color, say green; flickers; then sets the to color:
def colorFade(strip, colorFrom, colorTo, wait_ms=20, steps=10):
steps = 200
step_R = int(colorTo[0]) / steps
step_G = int(colorTo[1]) / steps
step_B = int(colorTo[2]) / steps
r = int(colorFrom[0])
g = int(colorFrom[1])
b = int(colorFrom[2])
for x in range(steps):
c = Color(int(r), int(g), int(b))
for i in range(strip.numPixels()):
strip.setPixelColor(i, c)
strip.show()
time.sleep(wait_ms / 1000.0)
r += step_R
g += step_G
b += step_B
Calling code:
colorFade(strip, [200, 0, 0], [0, 200, 0])
It sounds like you want to start at colorFrom, and gradually step
along a straight line until you reach colorTo.
What this code does is start at colorFrom, then increment the current
color as if you were stepping from black to colorTo.
Hard to be sure without the full code, but it looks like you should
replace this:
step_R = int(colorTo[0]) / steps
step_G = int(colorTo[1]) / steps
step_B = int(colorTo[2]) / steps
with this:
step_R = (colorTo[0] - colorFrom[0]) / steps
step_G = (colorTo[1] - colorFrom[1]) / steps
step_B = (colorTo[2] - colorFrom[2]) / steps
Edit: And, as jasonharper pointed out, you may be doing integer division. Not clear what your types are. If you're using Python 2, / is integer division. In Python 3, it's floating point.
Related
I was working on a project to create a tool that helps engineers to automate drawing tasks and as my company using ZWcad instead of Autocad I found myself obliged to use pyzwcad but I could not find enough information about it in one place or I was not searching in the correct places since I'm a beginner programmer and I need to gather all the Data I collect in one place.
first of all you need to import pyzwacad
from pyzwcad import *
or just import the used methods
from pyzwcad import ZwCAD, ZCAD, APoint, aDouble
my preferred way to automate tasks using API is to let the user start the program before using the tool.
so we now need to define the cad object
acad = ZwCAD()
in the next paragraphs I will summarize some of pyzwacad methods I use in my project.
Add line type in the current drawing file.
def G_add_line_typ(ltyp_name, acad):
line_type_found = False
for ltyp in acad.doc.Linetypes:
if ltyp.name == ltyp_name:
line_type_found = True
if not line_type_found:
acad.doc.Linetypes.Load(ltyp_name, "ZWCADiso.lin")
Draw square:
X_coo: is the X coordinate for the center point of the square shape.
y_coo: is the Y coordinate for the center point of the square shape.
we need to make an array/list for square points coordinate for example first point will take the first two positions in the list so
list[0] is the first point X coordinate and
list1 is the first point Y coordinate.
def draw_sqr(acad, X_coo, y_coo, d, w, n_color):
sqr_pts = [X_coo - (d / 2), y_coo + w / 2, X_coo - (d / 2), y_coo - w / 2, X_coo + (d / 2), y_coo - w / 2, X_coo + (d / 2), y_coo + w / 2, X_coo - (d / 2), y_coo + w / 2]
sqr = acad.model.AddLightWeightPolyline(aDouble(sqr_pts))
sqr.color = n_color
# shape color is an integer from color index 1 for red.
3- Add Circle:
# add insertion point
x_coo = 50
y_coo = 50
c1 = APoint(x_coo, y_co)
radius = 500
circle= acad.model.AddCircle(c1, radius)
4- Rotate object:
# add base point
x_coo = 50
y_coo = 50
base_point= APoint(x_coo, y_co)
r_ang = (45 * np.pi) / 180
object.Rotate(base_point, r_ang)
# object is refering to the object name it could be difrent based on your code
5- Add text:
# add insertion point
x_coo = 50
y_coo = 50
pttxt = APoint(x_coo, y_coo)
txt_height = 200 # text height
text = acad.model.AddText("text string", pttxt, txt_height)
6- Change text alignment:
# first we need to sort the current insertion point for the exist text object as it will be reset after changing the alignment.
old_insertion_point = APoint(text.InsertionPoint)
# text is refering to text object name it could be difrent based on your code
text.Alignment = ZCAD.zcAlignmentBottomCenter
# modify the text insertion point as the above step automaticaly reset it to (0, 0)
text.TextAlignmentPoint = old_insertion_point
7- Add rotated dimension line:
we need to define 3 points
start point, end point and dimension text point,also we should use math lib to use the radians method
import math
acad = ZwCAD()
st_dim = APoint(0, 0)
end_dim = APoint(100, 30)
text_dim = APoint(50, 15)
dim_line = acad.model.AddDimRotated(st_dim, end_dim, text_dim, math.radians(30))
acad.Application.ZoomAll()
the above code could be used to add liner dimention line by use 0 in angle place for horizontal dimension or math.radians(90) for vertical dimensions.
8- Add Aligned dimension line:
same as above but without using rotation angle.
acad = ZwCAD()
st_dim = APoint(0, 0)
end_dim = APoint(100, 30)
text_dim = APoint(50, 15)
dim_line = acad.model.AddDIMALIGNED(st_dim, end_dim, text_dim)
9- override existing dimension line text:
dim_line.TextOverride = "new text"
"dim_line" is referring to the wanted dimension line object name.
I am currently simulation light passing through an optics system with python and Zemax. I have it set up currently where I define the x and y boundaries of the "sensor" to that i can choose the size of the area I want to simulate. I get 1 rectangle.
I'd like to simulate nine rectangles, in a 3x3 Grid. I am unsure which way would be the most elegant... my first Idea was to "hardcode" the different intervals into 9 different scripts and run those through a bash script, but it seemst a bit to "unelegant".
How do I have to define xmax, xmin, ymax, ymin now so that i can run the same simulation and get those nine retangles?
My thought was to maybe create some sort of list where the boundaries are defined, and then perhaps rerun the simulation with a different boundary each time and finally merging the images that appear.
The current code is quite long, but the parameters are all set in a main functions which looks like this:
if __name__ == '__main__':
DirNameZmx = r'C:\Some\Path'
FileNameZmx = r"Optics.zmx"
DirNameResults = r"C:\Some\Other\Path"
FileNameResults = r"Interferogram_Result"
(QueueFieldsOut, QueueToDetector, ProcessRaytracing, ProcessesPsfWorkers, ProcessDetector) = \
InitializeSimulation(DirNameZmx=DirNameZmx, FileNameZmx=FileNameZmx,
DirNameResults=DirNameResults, FileNameResults=FileNameResults,
FieldAngleHxMin=-0.02, FieldAngleHxMax=+0.02, dFieldAngleX=0.001,
FieldAngleHyMin=-0.06, FieldAngleHyMax=+0.06, dFieldAngleY=0.001,
NbrWavelength=1, Configurations=[1, 2], NbrRaysFieldRow=32, RAperture=0.99,
DetectorImageSize=11., DetectorPixelSize=0.011, ZeroPadding=8,
BatchRaysMax=512**2, NbrProcessWorkers=2)
print(ProcessRaytracing.join())
for Process in ProcessesPsfWorkers:
print(Process.join())
print(Process.name, Process.exitcode)
print(ProcessDetector.join())
data = np.load(os.path.join(DirNameResults, FileNameResults+'.npy'))
plt.imshow(data, cmap="coolwarm")
plt.show()
The FieldAngleHxMin/Max and FieldAngleHyMin/Max are the rectangle boundaries. The result looks like this:
Simple iteration will do the work.
Try this:
def nine_squares(FieldAngleHxMin, FieldAngleHxMax, FieldAngleHyMin, FieldAngleHyMax):
xstep = (FieldAngleHxMax - FieldAngleHxMin) / 3
ystep = (FieldAngleHyMax - FieldAngleHyMin) / 3
for i in range(3):
for j in range(3):
xstartpoint = xstep + i * FieldAngleHxMin
xendpoint = xstep + (i + 1) * FieldAngleHxMin
ystartpoint = ystep + i * FieldAngleHyMin
yendpoint = ystep + (i + 1) * FieldAngleHyMin
yield (xstartpoint, xendpoint, ystartpoint, yendpoint)
It will return lists of start and end coordinates every time it is called.
I'm currently trying to figure out how to increase/decrease the brightness of a .tiff file without parsing each pixel (too high power consumption). Right now, using the front micro-service, the user uses a ng-slider to change the value of the desired brightness,which goes directly to the back where it is parsed to try to compute a new .tiff.
So, I'm wondering if there isn't a gdal function I can't find to directly alter the images and increase/decrease the brightness at will!
The code currently looks like this (also trying to change the contrast, but I could find my way if I understand how to change the brightness) :
# Contrast & Luminosity
def get_correctMap(path, luminosity, contrast):
ds = gdal.Open(image_path)
#To normalize
band1 = ds.GetRasterBand(1)
#Get the max value
maxValue = int(2**16 -1)
if band1.DataType == gdal.GDT_UInt16:
maxValue = int(2**16 -1)
elif band1.DataType == gdal.GDT_Byte:
maxValue = int(2**8 -1)
else:
LOGGER.info(f"band type {band1.DataType} not handled: use default size of value (16 bits)")
band1 = ds.ReadAsArray(0,0,ds.RasterXSize,ds.RasterYSize)[0]
band2 = ds.ReadAsArray(0,0,ds.RasterXSize,ds.RasterYSize)[1]
band3 = ds.ReadAsArray(0,0,ds.RasterXSize,ds.RasterYSize)[2]
for x in range(0,ds.RasterXSize):
for y in range(0,ds.RasterXSize):
r = float(band1[x,y]) / maxValue
g = float(band2[x,y]) / maxValue
b = float(band3[x,y]) / maxValue
#Convert to HLS them apply luminosity and contrast
(h,l,s) = colorsys.rgb_to_hls(r, g, b)
l = min(max(0, l + (l - 0.5)*(luminosity - 0.5)) , 1)
s = min(max(0, s + (s - 0.5)*(contrast - 0.5)) , 1)
(r,g,b) = colorsys.hls_to_rgb(h, l, s)
band1[x,y] = int(r * maxValue)
band2[x,y] = int(g * maxValue)
band3[x,y] = int(b * maxValue)
#Need to save the changes, but obviously already too long to pursue this way
#and save the news bands
ds.flushCache()
return path
Hope you know a better way I can't find!
Thanks in advance.
A first lead could be to use the last features provide by OpenLayer for me, but it is not a back solution anymore, I'm digging it.
https://geoadmin.github.io/ol3/apidoc/ol.layer.Tile.html
EDIT: The constrast and luminosity feature are only implemented on OpenLayer 3 but not in the next version (including mine OL 5), so the proper answer is : it is not possible.
I'm trying to model a pursuit problem of bugs chasing one another in a 2 dimensional plane and I'm using SciPY.odeint to help me with this. With the following code, the model works however as the bugs get closer together the model breaks down and emits the excess work done on this call (perhaps wrong Dfun type) error.
import numpy as np
from scipy.integrate import odeint
def split_list(a_list):
half = len(a_list)//2
return a_list[:half], a_list[half:]
def diff(w, t):
x_points, y_points = split_list(w)
def abso(f, s):
return np.sqrt((x_points[f] - x_points[s])**2 + (y_points[f] - y_points[s])**2)
x_funct = [(x_points[i+1] - x_points[i]) / abso(i+1, i) for i in range(len(x_points) - 1)]
x_funct.append((x_points[0] - x_points[-1]) / abso(0,-1))
y_funct = [(y_points[i+1] - y_points[i]) / abso(i+1,i) for i in range(len(x_points) - 1)]
y_funct.append((y_points[0] - y_points[-1]) / abso(0,-1))
funct = x_funct + y_funct
return funct
def ode(tstart, tend, init_cond):
t = np.linspace(tstart, tend, step_size)
wsol = odeint(diff, init_cond, t)
sols = [wsol[:,i] for i in range(len(init_cond))]
x_sols, y_sols = split_list(sols)
return x_sols, y_sols, t, tend
bug_init_cond = [[-0.5, 1],
[0.5, 1],
[0.5, -1],
[-0.5, -1],]
amount_of_bugs = 4
step_size = 10000
x_sols, y_sols, t, tend = ode(0, 5, [bug_init_cond[i][j] for j in range(2) for i in range(amount_of_bugs)])
As I'm quite new with using the Scipy.odeint function, I was wondering if there is a solution to this excess work done? Thank-you for your time.
Your problem is that in the exact solution the paths meet at time t=1.48 to t=1.5. In an exact solution, you would get a division by zero error, with floating point noise that is "degraded" to a stiff situation where the step size is regulated down until the output time step requires more than mxstep=500 internal steps.
You could add conditions so that the right side is set to zero if the positions get to close. One quick hack to achieve that is to modify the distance function abso to
def abso(f, s):
return np.sqrt(1e-12+(x_points[f] - x_points[s])**2 + (y_points[f] - y_points[s])**2)
so that you never divide by zero, and for visible distances the perturbation is negligible.
I was looking at the wikipedia page for the Koch Snowflake (here) and was bothered by the all the examples all being in the logo/turtle style. So i set out to make my own that returned a list or coordinates.
My implementation is in python and i basically ripped off the python turtle implementation but replaced the turtle specific stuff with basic trig. It resulted in some ugly code. My challenge for you is to either improve my code or come up with a more elligant solution of your own. It can be in python, or your favorite language.
My Code:
from math import sin, cos, radians
def grow(steps, length = 200, startPos = (0,0)):
angle = 0
try:
jump = float(length) / (3 ** steps)
except:
jump = length
set="F"
for i in xrange(steps): set=set.replace("F", "FLFRFLF")
coords = [startPos]
for move in set:
if move is "F":
coords.append(
(coords[-1][0] + jump * cos(angle),
coords[-1][1] + jump * sin(angle)))
if move is "L":
angle += radians(60)
if move is "R":
angle -= radians(120)
return coords
EDIT: due to lazy copying, i forgot the import
I don't see it as particularly ugly and I'd only refactor it incrementally, e.g. as a first step (I've removed the try/except because I don't know what you're trying to ward against... if it needs to get back in it should be a bit more explicit, IMHO):
import math
angles = [math.radians(60*x) for x in range(6)]
sines = [math.sin(x) for x in angles]
cosin = [math.cos(x) for x in angles]
def L(angle, coords, jump):
return (angle + 1) % 6
def R(angle, coords, jump):
return (angle + 4) % 6
def F(angle, coords, jump):
coords.append(
(coords[-1][0] + jump * cosin[angle],
coords[-1][1] + jump * sines[angle]))
return angle
decode = dict(L=L, R=R, F=F)
def grow(steps, length=200, startPos=(0,0)):
pathcodes="F"
for i in xrange(steps):
pathcodes = pathcodes.replace("F", "FLFRFLF")
jump = float(length) / (3 ** steps)
coords = [startPos]
angle = 0
for move in pathcodes:
angle = decode[move](angle, coords, jump)
return coords
If a second step was warranted I'd probably roll this functionality up into a class, but I'm not sure that would make things substantially better (or, better at all, in fact;-).
I liked your question so much that I posted an answer to it as a new question, so that other people could improve it:
https://stackoverflow.com/questions/7420248
I used no Logo/Turtle stuff, neither trigonometry.
Congrats for being the first one bringing this problem to StackOverflow!
Mathematica is superior when it comes to math stuff:
points = {{0.0, 1.0}};
koch[pts_] := Join[
pts/3,
(RotationMatrix[60 Degree].#/3 + {1/3, 0}) & /# pts,
(RotationMatrix[-60 Degree].#/3 + {1/2, 1/Sqrt[12]}) & /# pts,
(#/3 + {2/3, 0}) & /# pts
];
Graphics[Line[Nest[koch, points, 5]], PlotRange -> {{0, 1}, {0, 0.3}}] //Print
Something to consider, if not for your implementation then for testing your implementation, is that Python turtle can record what it's doing and give you back the coordinates. You use begin_poly() and end_poly() around the code you want to record and then use get_poly() afterwards to get the points.
In this example, I'll draw the snowflake based on code from this site then register those coordinates back as a new turtle shape that I'll randomly (and quickly) stamp about the screen:
import turtle
from random import random, randrange
def koch_curve(turtle, steps, length):
if steps == 0:
turtle.forward(length)
else:
for angle in [60, -120, 60, 0]:
koch_curve(turtle, steps - 1, length / 3)
turtle.left(angle)
def koch_snowflake(turtle, steps, length):
turtle.begin_poly()
for _ in range(3):
koch_curve(turtle, steps, length)
turtle.right(120)
turtle.end_poly()
return turtle.get_poly()
turtle.speed("fastest")
turtle.register_shape("snowflake", koch_snowflake(turtle.getturtle(), 3, 100))
turtle.reset()
turtle.penup()
turtle.shape("snowflake")
width, height = turtle.window_width() / 2, turtle.window_height() / 2
for _ in range(24):
turtle.color((random(), random(), random()), (random(), random(), random()))
turtle.goto(randrange(-width, width), randrange(-height, height))
turtle.stamp()
turtle.done()
You can have the pen up and turtle hidden during polygon generation if you don't want this step to be seen by the user.