Browse Source

added image noise generation, which means we gotta use numpy now

tags/v1.8.0
Roxie Gibson 6 years ago
parent
commit
b6be37341d
2 changed files with 25 additions and 4 deletions
  1. +1
    -0
      requirements.txt
  2. +24
    -4
      roxbot/cogs/image.py

+ 1
- 0
requirements.txt View File

beautifulsoup4==4.6.0 beautifulsoup4==4.6.0
discord==0.0.2 discord==0.0.2
pillow pillow
numpy

+ 24
- 4
roxbot/cogs/image.py View File



import os import os
import math import math
import random
import numpy as np
from PIL import Image, ImageEnhance

import roxbot import roxbot
import discord import discord
from PIL import Image, ImageEnhance
from discord.ext import commands from discord.ext import commands




return cls(rows=rows, colours=colours) return cls(rows=rows, colours=colours)




class CustomCommands:
class ImageEditor:
def __init__(self, bot_client): def __init__(self, bot_client):
self.bot = bot_client self.bot = bot_client


except IndexError: except IndexError:
return message.author.avatar_url_as(format="png") return message.author.avatar_url_as(format="png")


@staticmethod
def add_grain(img, prob=0.25):
img_matrix = np.zeros(img.size, dtype=np.uint8)
for x in range(img.height):
for y in range(img.width):
if prob < random.random():
img_matrix[x][y] = 255

noisy = Image.fromarray(img_matrix, "L")
noisy = noisy.convert("RGB")
mask = Image.new('RGBA', img.size, (0, 0, 0, 51))
return Image.composite(noisy, img, mask)

@staticmethod @staticmethod
async def flag_filter(name, flag, url): async def flag_filter(name, flag, url):
"""At the moment, can only make horizontal stripe flags""" """At the moment, can only make horizontal stripe flags"""
ehn = ImageEnhance.Color(img) ehn = ImageEnhance.Color(img)
img = ehn.enhance(2) img = ehn.enhance(2)


# Add Salt and Pepper Noise

img = self.add_grain(img)

img.save(jpg_name) img.save(jpg_name)


# JPG-fy image # JPG-fy image
for x in range(10):
for x in range(20):
img = Image.open(jpg_name) img = Image.open(jpg_name)
img = img.convert(mode="RGB") img = img.convert(mode="RGB")
img.save(jpg_name) img.save(jpg_name)




def setup(bot_client): def setup(bot_client):
bot_client.add_cog(CustomCommands(bot_client))
bot_client.add_cog(ImageEditor(bot_client))

Loading…
Cancel
Save