Beispiel #1
0
def image():
    """!i <param> <image>
    
    Available parameters are: smile, smile2, hot, old, young, hollywood, glasses, hitman, mustache, pan, heisenberg, female, female2, male
    """
    global VALID_IMAGE_PARAMS

    msg = Message(flask.request.form)
    _param = msg.textBody.lower()

    paramAliases = {
        'mustache': 'mustache_free',
        'glasses': 'fun_glasses',
        'smile2': 'smile_2',
        'female2': 'female_2'
    }

    if _param not in VALID_IMAGE_PARAMS:
        _param = paramAliases.get(_param)
        if not _param:
            return 'Not a valid parameter. See !help i'

    if msg.fileName == 'noFile':
        return 'Please supply a file.'
    elif msg.fileName == 'fileError':
        return 'There was an error selecting the last file transfer.'

    import faces
    import uuid

    try:
        img = faces.FaceAppImage(file=open(msg.fileName, 'rb'))
        outimg = img.apply_filter(_param, cropped=False)
    except faces.ImageHasNoFaces:
        return 'No faces on this image.'
    except faces.BadInfo as ex:
        return str(ex)

    # Create the directory for us to store these files if it doesn't exist.
    if not os.path.exists('resources/iout/'):
        os.mkdir('resources/iout')

    outPath = os.path.abspath('resources/iout/{}.jpg'.format(uuid.uuid4()))
    with open(outPath, 'wb') as f:
        f.write(outimg)

    return DataResponse(outPath)
Beispiel #2
0
    async def faceapp_image(self, message, client, filter, url):
        url_copy = url.strip().split('?')[0]
        extension = url_copy.split('.')[-1]
        full_path = 'res/temp/image.' + extension

        try:
            r = requests.get(url, stream=True)
            if r.status_code != 200:
                text = "Nie można było pobrać obrazka"
                emb = jb2.embed.error_embed(message.author.mention, text)
                await client.send_message(message.channel, embed=emb)
                return

            image = faces.FaceAppImage(url=url)

            if filter not in image.filters:
                text = "Podany filtr nie istnieje"
                emb = jb2.embed.error_embed(message.author.mention, text)
                await client.send_message(message.channel, embed=emb)
                return

            out_image = image.apply_filter(filter)

            with open(full_path, 'wb') as f:
                f.write(out_image)

            await client.send_file(message.channel, full_path)
        except faces.ImageHasNoFaces:
            text = "Nie znaleziono twarzy"
            emb = jb2.embed.error_embed(message.author.mention, text)
            await client.send_message(message.channel, embed=emb)
        except faces.BadImageType:
            text = "Nieprawidłowy rodzaj obrazu"
            emb = jb2.embed.error_embed(message.author.mention, text)
            await client.send_message(message.channel, embed=emb)
        except faces.BaseFacesException:
            text = "Coś poszło nie tak"
            emb = jb2.embed.error_embed(message.author.mention, text)
            await client.send_message(message.channel, embed=emb)
        except ValueError as e:
            text = "Nieprawidłowy URL"
            emb = jb2.embed.error_embed(message.author.mention, text)
            await client.send_message(message.channel, embed=emb)
Beispiel #3
0
    async def smile(self, ctx, url):
        r = is_valid_image(url)
        if isinstance(r, str):
            await ctx.send(r)
            return
        del r

        await ctx.send('Processing...')
        
        ext = splitext(urlparse(url).path)[1]
        file_name = 'smile{}'.format(ext)
        _url = url

        
        try:
            img = faces.FaceAppImage(url = _url)
        except faces.ImageHasNoFaces:
            await ctx.send('Face is not recognized.')
            return
        except faces.BadImageType:
            await ctx.send('This file is not valid.')
            return
        except faces.BaseFacesException:
            await ctx.send('Unknown error.')
            return
        except:
            await ctx.send('API is dead')
            return

        try:
            result = img.apply_filter('smile')
        except faces.BadFilterID:
            await ctx.send('Filter error.')
            return
        except:
            await ctx.send('Unknown error.')
            return

        with open(file_name, 'wb') as img:
            img.write(BytesIO(result))

        await ctx.send(file = discord.File(file_name))
Beispiel #4
0
def my_form_post():
    if request.method == 'POST' and 'photo' in request.files:
        filename = photos.save(request.files['photo'])
    full_filename = os.path.join(app.config['UPLOADED_PHOTOS_DEST'], filename)
    pic = open(full_filename, 'rb')
    try:
        image = faces.FaceAppImage(file=pic)
    except faces.ImageHasNoFaces:
        return('Your face is not recognized. Are you an alien?')
    except faces.BadImageType:
        return('This image is not valid. Get some good bytes.')
    except faces.BaseFacesException:
        return('Some unknown wrong things happened.')
    try:
        happy = image.apply_filter('female')
    except faces.BadFilterID:
        return('Too cool filter to exist.')
    image = Image.open(io.BytesIO(happy))
    o="op"+filename
    op=os.path.join(app.config['UPLOADED_PHOTOS_DEST'], o)
    image.save(op)
    return render_template('output.html',input="/templates/"+filename,output="/templates/"+o)
import faces

old_rockfeller = 'https://upload.wikimedia.org/wikipedia/commons/6/6f/John_D._Rockefeller_1885.jpg'
image = faces.FaceAppImage(url=old_rockfeller)
young_rockfeller = image.apply_filter('young')


Beispiel #6
0
    def setUp(self):
        with open('tests/Roosevelt.jpg', 'rb') as valid_image:
            self.image_by_file = faces.FaceAppImage(file=valid_image)

        valid_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/President_Roosevelt_-_Pach_Bros.tif/lossy-page1-220px-President_Roosevelt_-_Pach_Bros.tif.jpg'
        self.image_by_url = faces.FaceAppImage(url=valid_url)
Beispiel #7
0
 def setUp(self):
     with open('tests/Roosevelt.jpg', 'rb') as valid_image:
         self.image_by_file = faces.FaceAppImage(file=valid_image)
Beispiel #8
0
 def testNoFaces(self):
     with self.assertRaises(faces.ImageHasNoFaces):
         with open('tests/Enigma.jpg', 'rb') as no_faces_image:
             image = faces.FaceAppImage(file=no_faces_image)
Beispiel #9
0
 def testNotImage(self):
     with self.assertRaises(faces.BadImageType):
         not_image_url = 'https://wikipedia.org'
         image = faces.FaceAppImage(url=not_image_url)
Beispiel #10
0
 def testBadFile(self):
     with self.assertRaises(faces.BadImageType):
         with open('.travis.yml', 'rb') as bad_file:
             image = faces.FaceAppImage(file=bad_file)
Beispiel #11
0
print(faces.KNOWN_FILTERS)
fil = input(
    "Now enter the full name of the filter you want from the above list.\n>")

crop = True
c = input("Crop? [y/n]\n>")
if c == 'y':
    crop = True
elif c == 'n':
    crop = False
else:
    crop = True

try:
    image = faces.FaceAppImage(file=face)
except faces.ImageHasNoFaces:
    print('Your face is not recognized. Are you an alien?')
except faces.BadImageType:
    print('This image is not valid. Get some good bytes.')
except faces.BaseFacesException:
    print('Something Bad Happened.')

try:
    result = image.apply_filter(fil, cropped=crop)
except faces.BadFilterID:
    print('Bad Filter ID')

img = open('output.jpg', 'wb')
img.write(result)
img.close()