I made classes to use in generating square/rectangular shapes on a texture.
import numpy as np import random class Tex_SHR: def __init__(self, resolution: set, colorGrade:str, genNum: int, genOption: int): self.resolution = resolution self.colorGrade = colorGrade #Not yet in use self.genNum = genNum self.genOption = genOption self.texture = None self.blurFeild = None #Not yet in use self.noiseFeild = None #Not yet in use self.seedVal = None #Not yet in use def createArray(self): return np.ones(shape = (self.resolution[0] , self.resolution[1]), dtype = np.int16, order = 'C') class Rect_GenTex_SHR(Tex_SHR): def __init__(self, resolution: set, colorGrade: str, genNum: int, genOption:int): super().__init__(resolution, colorGrade, genNum, genOption) self.texture = self.createArray() # Might implement different rectGen Types i.e.: Near Square(Creates squares or rects close # to squares), square, large, large and small, small, etc. def genRectsNormal(self): xDmn = self.texture.shape[0] yDmn = self.texture.shape[1] paddingFactorX = round( xDmn/ 2) paddingFactorY = round( yDmn/2 ) xRand = random.randint(0, xDmn - paddingFactorX ) xRandMax = random.randint(xRand + 3 , xDmn) yRand = random.randint(0, yDmn - paddingFactorY ) yRandMax = random.randint(yRand + 3, yDmn) return ((xRand, xRandMax), (yRand, yRandMax)) #Implement different types of value generation i.e.: Close values(Values of #rects don't differ by much), Far(Differ by a lot), cluster(Think about this one) def grab_GenType(self): if self.genOption == 0: return self.genRectsNormal() def generate_TEX(self): squares = [] for num in range(self.genNum): rectValue = random.randint(0,256) coords = self.grab_GenType() squares.append((coords, rectValue)) for xRange in range(coords[0][0], coords[0][1]): for yRange in range(coords[1][0], coords[0][1]): self.texture[xRange, yRange] = rectValue #print("TEST: ", "\n", self.texture, "\n", coords) b = Rect_GenTex_SHR((500,800), "Red", 100, 0) b.generate_TEX()
I'm using numpy arrays because I want it to be useable with textures and numpy is said to faster and takes less space in memory compared to a List of Lists approach. Is numpy still the best choice?
Used the Tex_SHR
class because I plan on having different classes that generate different shapes using the same set as the base class.
The dtype = np.int16
is to limit memory allocated to the array.
paddingFactorX/Y
is there to keep the randomint from generating numbers really close to each other
genOption
and grab_GenType()
is there because I want to have different ways to generate the rectangles/squares.
The biggest issue I've had is that running the function at the end takes on average 2.45 seconds on my laptop. Even though my laptop is pretty weak I'm worried that when thrown larger textures, with larger amounts to generate it'll basically become unusable even on better machines. Would also like to make my code more "pythonic" if that's possible.
Laptop specs: 12 GB Ram Intel I5-1035G1 Quad core Windows 10