Created
April 30, 2020 00:02
-
-
Save sam2332/f2028bef193e8553774a55c1be4ac39b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# In[2]: | |
from PIL import Image | |
import random | |
import uuid | |
import os | |
from docx import Document | |
from docx.shared import Inches | |
from PIL import ImageEnhance | |
import imageio | |
import numpy as np | |
import zlib | |
import math, wave, array | |
import zipfile | |
class Trasher(): | |
def __init__(self,path): | |
self.path = path | |
self.subdir = None | |
if not os.path.exists(self.path): | |
os.mkdir(self.path) | |
def changeSubDir(self,name=None): | |
if name is None: | |
name = uuid.uuid4().hex | |
self.subdir = name | |
print(self.path, self.subdir) | |
if not os.path.exists(os.path.join(self.path, self.subdir)): | |
os.mkdir(os.path.join(self.path,self.subdir)) | |
def make_png(self, fname=None, height=None, width=None, color=None): | |
if fname is None: | |
fname = uuid.uuid4().hex | |
if width is None: | |
width = random.randint(10,2000) | |
if height is None: | |
height = random.randint(10,2000) | |
if color is None: | |
color = (random.randint(10,255),random.randint(10,255),random.randint(10,255)) | |
img = Image.new('RGB', (height,width), color) | |
out = self.path | |
if self.subdir is not None: | |
out = os.path.join(out,self.subdir) | |
out = os.path.join(out,fname+".png") | |
img.save(out, "PNG") | |
def make_txt(self, fname=None, text=None): | |
if fname is None: | |
fname = uuid.uuid4().hex | |
if text is None: | |
text = uuid.uuid4().hex | |
out = self.path | |
if self.subdir is not None: | |
out = os.path.join(out,self.subdir) | |
out = os.path.join(out,fname+".txt") | |
with open(out,'w') as f: | |
f.write(text) | |
def make_docx(self,fname=None): | |
if fname is None: | |
fname = uuid.uuid4().hex | |
document = Document() | |
document.add_heading('Document Title'+uuid.uuid4().hex, 0) | |
p = document.add_paragraph('A plain paragraph having some ') | |
p.add_run('bold').bold = True | |
p.add_run(' and some ') | |
p.add_run('italic.').italic = True | |
document.add_heading('Heading, level 1', level=1) | |
document.add_paragraph('Intense quote'+uuid.uuid4().hex, style='Intense Quote') | |
document.add_paragraph( | |
'first item in unordered list'+uuid.uuid4().hex, style='List Bullet' | |
) | |
document.add_paragraph( | |
'first item in ordered list'+uuid.uuid4().hex, style='List Number' | |
) | |
records = ( | |
(3, '101', 'Spam'+uuid.uuid4().hex), | |
(7, '422', 'Eggs'+uuid.uuid4().hex), | |
(4, '631', 'Spam, spam, eggs, and spam'+uuid.uuid4().hex) | |
) | |
table = document.add_table(rows=1, cols=3) | |
hdr_cells = table.rows[0].cells | |
hdr_cells[0].text = 'Qty' | |
hdr_cells[1].text = 'Id' | |
hdr_cells[2].text = 'Desc' | |
for qty, id, desc in records: | |
row_cells = table.add_row().cells | |
row_cells[0].text = str(qty) | |
row_cells[1].text = id | |
row_cells[2].text = desc | |
document.add_page_break() | |
out = self.path | |
if self.subdir is not None: | |
out = os.path.join(out,self.subdir) | |
out = os.path.join(out,fname+".docx") | |
document.save(out) | |
def make_tone_file(self,fname=None, tones=None,tones_count=10): | |
if fname is None: | |
fname = uuid.uuid4().hex | |
if tones is None: | |
tones = [] | |
for _ in range(tones_count): | |
tones.append(( | |
random.randint(10,800), | |
random.randint(1,2000) | |
)) | |
# generate wav file containing sine waves | |
# FB36 - 20120617 | |
volume = 10 # percent | |
sampleRate = 44100 # of samples per second (standard) | |
numChan = 1 # of channels (1: mono, 2: stereo) | |
dataSize = 2 # 2 bytes because of using signed short integers => bit depth = 16 | |
data = array.array('h') # signed short integer (-32768 to 32767) data | |
for duration,freq in tones: | |
#freq = 440 # of cycles per second (Hz) (frequency of the sine waves) | |
if freq == 0: | |
freq = 1 | |
numSamplesPerCyc = int(sampleRate / freq) | |
numSamples = int(sampleRate * (duration/1000)) | |
for i in range(numSamples): | |
sample = 32767 * float(volume) / 100 | |
sample *= math.sin(math.pi * 2 * (i % numSamplesPerCyc) / numSamplesPerCyc) | |
data.append(int(sample)) | |
out = self.path | |
if self.subdir is not None: | |
out = os.path.join(out,self.subdir) | |
out = os.path.join(out,fname+".wav") | |
f = wave.open(out, 'w') | |
f.setparams((numChan, dataSize, sampleRate, numSamples, "NONE", "Uncompressed")) | |
f.writeframes(data.tostring()) | |
f.close() | |
def make_video(self,fname=None, height=None, width=None,color=None,fps=5, seconds =None): | |
if fname is None: | |
fname = uuid.uuid4().hex | |
if width is None: | |
width = random.randint(920, 1920) | |
if height is None: | |
height = random.randint(700, 2400) | |
if seconds is None: | |
seconds = random.randint(3, 30) | |
if color is None: | |
color = ( | |
random.randint(10, 255), | |
random.randint(10, 255), | |
random.randint(10, 255) | |
) | |
image = Image.new('RGB', (height,width), color) | |
color_percentage = 0.5 # 50% | |
enhanced_image = ImageEnhance.Color(image).enhance(color_percentage) | |
total_frames = seconds * fps | |
color_percentage_for_each_frame = (100 / total_frames) / 100 | |
out = self.path | |
if self.subdir is not None: | |
out = os.path.join(out,self.subdir) | |
out = os.path.join(out,fname+".mp4") | |
writer = imageio.get_writer(out, format='mp4', mode='I', fps=fps) | |
for i in range(total_frames): | |
if i < total_frames: | |
processed = ImageEnhance.Color(image).enhance( | |
color_percentage_for_each_frame * i) | |
writer.append_data(np.asarray(processed)) | |
else: | |
writer.append_data(np.asarray(image)) | |
writer.close() | |
del writer | |
def make_binary(self,fname=None, size=None): | |
if fname is None: | |
fname = uuid.uuid4().hex | |
if size is None: | |
size = random.randint(1,20) | |
teststr = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus | |
pretium justo eget elit eleifend, et dignissim quam eleifend. Nam vehicula nisl | |
posuere velit volutpat, vitae scelerisque nisl imperdiet. Phasellus dignissim, | |
dolor amet."""*size | |
cmpstr = zlib.compress(teststr.encode('utf-8')) | |
out = self.path | |
if self.subdir is not None: | |
out = os.path.join(out,self.subdir) | |
out = os.path.join(out,fname+".bin") | |
with open(out,'wb') as f: | |
f.write(cmpstr) | |
def make_zip(self,fname=None,length=None): | |
if fname is None: | |
fname = uuid.uuid4().hex | |
if length is None: | |
length = random.randint(1,200) | |
out = self.path | |
if self.subdir is not None: | |
out = os.path.join(out,self.subdir) | |
out = os.path.join(out,fname+".zip") | |
zf = zipfile.ZipFile(out, mode="w", compression=zipfile.ZIP_DEFLATED) | |
zf.writestr("sample.txt", "Hello there!"*length) | |
zf.close() | |
# In[3]: | |
a = Trasher('test') | |
# In[4]: | |
for dirs in range(5): | |
for i in range(2): | |
a.make_png() | |
a.make_txt() | |
a.make_docx() | |
a.make_tone_file() | |
a.make_video() | |
a.make_binary() | |
a.make_zip() | |
a.changeSubDir() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment