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

@@ -2,3 +2,4 @@ aiohttp==3.0.1
beautifulsoup4==4.6.0
discord==0.0.2
pillow
numpy

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

@@ -27,9 +27,12 @@ SOFTWARE.

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

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


@@ -110,7 +113,7 @@ class PrideFlags:
return cls(rows=rows, colours=colours)


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

@@ -122,6 +125,19 @@ class CustomCommands:
except IndexError:
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
async def flag_filter(name, flag, url):
"""At the moment, can only make horizontal stripe flags"""
@@ -276,10 +292,14 @@ class CustomCommands:
ehn = ImageEnhance.Color(img)
img = ehn.enhance(2)

# Add Salt and Pepper Noise

img = self.add_grain(img)

img.save(jpg_name)

# JPG-fy image
for x in range(10):
for x in range(20):
img = Image.open(jpg_name)
img = img.convert(mode="RGB")
img.save(jpg_name)
@@ -289,4 +309,4 @@ class CustomCommands:


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

Loading…
Cancel
Save