Example #1
0
def image_generater(request):
    title = request.GET['title']
    top_text = request.GET['top_text']
    author = request.GET['author']
    animal_code = request.GET['animal_code']
    color_index = request.GET['color_code']
    guide_text = request.GET['guide_text']
    guide_text_placement = request.GET['guide_text_placement']

    im = Image.new("RGB", (256, 256), 'white')

    # im : 위 데이터를 받아서, 이미지는 여기에서 그림

    ttf_path = settings.ROOT('assets', 'fonts', 'NanumGothicCoding.ttf')
    d = ImageDraw.Draw(im)

    fnt = ImageFont.truetype(ttf_path, 40)
    d.text((10, 10), title, font=fnt, fill=(0, 255, 0, 128))

    fnt = ImageFont.truetype(ttf_path, 20)
    d.text((10, 60), top_text, font=fnt, fill=(0, 255, 0, 255))

    fnt = ImageFont.truetype(ttf_path, 10)
    d.text((10, 110), author, font=fnt, fill=(0, 255, 0, 255))

    response = HttpResponse(content_type='image/jpeg')  #file-like
    im.save(response, format='PNG')
    return response
Example #2
0
File: tests.py Project: mdj2/aol
    def test_thumbnail_url(self):
        photo = Photo.objects.get(pk=1)

        # delete the thumbnail file
        thumb_path = SETTINGS.ROOT('media', 'photos', 'thumbnail-test.jpg')
        thumb_url = "/media/photos/thumbnail-test.jpg"
        try:
            os.remove(thumb_path)
        except OSError:
            pass

        # this should generate a thumbnail
        self.assertEqual(photo.thumbnail_url, thumb_url)
        # make sure the thumbnail file actually exists
        self.assertTrue(os.path.exists(thumb_path))

        # now we want to make sure subsequent calls to the thumbnail_url do not
        # recreate the image (since that would be expensive)

        # overwrite the file
        flag_text = "this should not be overwritten!"
        with open(thumb_path, 'w') as f:
            f.write(flag_text)
        # try getting the thumbnail URL again
        self.assertEqual(photo.thumbnail_url, thumb_url)
        # make sure it didn't recreate the thumbnail
        with open(thumb_path) as f:
            self.assertEqual(f.read(), flag_text)

        # delete the thumbnail file
        try:
            os.remove(thumb_path)
        except OSError:
            pass
Example #3
0
 def setup_databases(self, *args, **kwargs):
     to_return = super(AOLRunner, self).setup_databases(*args, **kwargs)
     from django.db import connection
     from django.conf import settings
     cursor = connection.cursor()
     path = settings.ROOT("aol", "test.sql")
     sql = open(path).read()
     cursor.execute(sql)
     return to_return
def image_generator(request):
    title = request.GET['title']
    top_text = request.GET['top_text']
    author = request.GET['author']
    animal_code = request.GET['animal_code']
    color_index = request.GET['color_code']
    guide_text = request.GET['guide_text']
    guide_text_placement = request.GET['guide_text_placement']

    animal_path = settings.ROOT('assets', 'animal',
                                '{}.png'.format(animal_code))
    animal_im = Image.open(animal_path)

    color = COLOR_CODES[int(color_index)]

    canvas_im = Image.new('RGB', (500, 700), (255, 255, 255, 255))

    animal_im = animal_im.resize((400, 400))
    canvas_im.paste(animal_im, (50, 40))  # left/top 지정

    # im : # 위 데이터를 받아서, 이미지는 여기에서 그리겠습니다~ ;)
    ttf_path = settings.ROOT('assets', 'fonts', 'NanumGothicCoding.ttf')
    draw = ImageDraw.Draw(canvas_im)

    draw.rectangle((20, 0, 480, 10), fill=color)

    draw.rectangle((10, 400, 480, 510), fill=color)

    fnt = ImageFont.truetype(ttf_path, 70)
    draw.text((45, 430), title, font=fnt, fill=(255, 255, 255, 255))

    fnt = ImageFont.truetype(ttf_path, 20)
    draw.text((160, 15), top_text, font=fnt, fill=(0, 0, 0, 255))

    fnt = ImageFont.truetype(ttf_path, 25)
    draw.text((360, 655), author, font=fnt, fill=(0, 0, 0, 255))

    fnt = ImageFont.truetype(ttf_path, 30)
    position = (125, 505)  # bottom-right
    draw.text(position, guide_text, font=fnt, fill=(0, 0, 0, 255))

    reponse = HttpResponse(content_type='image/png')  # file-like
    canvas_im.save(reponse, format='PNG')
    return reponse
Example #5
0
def image_generator(request):
    title = request.GET['title']
    top_text = request.GET['top_text']
    author = request.GET['author']
    animal_code = request.GET['animal_code']
    color_idx = int(request.GET['color_code'])
    guide_text = request.GET['guide_text']
    guide_text_placement = request.GET['guide_text_placement']

    animal_path = settings.ROOT('assets', 'animal',
                                '{}.png'.format(animal_code))
    animal_im = Image.open(animal_path)

    animal_im = animal_im.resize((400, 400))

    color = COLOR_CODES[color_idx]

    canvas_im = Image.new('RGB', (500, 700), "white")
    canvas_im.paste(animal_im, (50, 40))  # left, top

    ttf_path = settings.ROOT('assets', 'fonts', 'NanumGothicCoding.ttf')
    # 여기서 데이터를 받아서 그리겠습니다.
    draw = ImageDraw.Draw(canvas_im)

    draw.rectangle((20, 0, 480, 8), fill=color)
    draw.rectangle((10, 400, 480, 510), fill=color)

    fnt = ImageFont.truetype(ttf_path, 70)
    draw.text((45, 430), title, font=fnt, fill=(255, 255, 255, 255))

    fnt = ImageFont.truetype(ttf_path, 20)
    draw.text((160, 15), top_text, font=fnt, fill=(0, 0, 0, 255))

    fnt = ImageFont.truetype(ttf_path, 30)
    draw.text((125, 510), guide_text, font=fnt, fill=(0, 0, 0, 255))

    fnt = ImageFont.truetype(ttf_path, 25)
    draw.text((360, 655), author, font=fnt, fill=(0, 0, 0, 255))

    response = HttpResponse(content_type='image/png')  # file-like
    canvas_im.save(response, format='png')
    return response
Example #6
0
def image_generator(request):
    title = request.GET['title']
    top_text = request.GET['top_text']
    author = request.GET['author']
    animal_code = request.GET['animal_code']
    color_index = request.GET['color_code']
    guide_text = request.GET['guide_text']
    guide_text_placement = request.GET['guide_text_placement']
    
    animal_path = settings.ROOT('cover','assets','animal','{}.png'.format(animal_code))
    animal_im = Image.open(animal_path)    

    color=COLOR_CODE[int(color_index)]
    
    canvas_im=Image.new('RGB',(500,700),(255,255,255,255))
    animal_im = animal_im.resize((400,395))
    canvas_im.paste(animal_im,(50,40))
    

    ttf_path=settings.ROOT('assets','fonts','NanumBarunGothicBold.ttf')      
    draw = ImageDraw.Draw(canvas_im)    
    
    draw.rectangle((20,0,480,10),fill=color)
    draw.rectangle((10,400,480,510),fill=color)

    fnt = ImageFont.truetype(ttf_path, 60)
    draw.text((45,430), title , font=fnt, fill=(255,255,255,128))  

    fnt = ImageFont.truetype(ttf_path, 20)
    draw.text((220,15), top_text , font=fnt, fill=(0,0,0,255))

    fnt = ImageFont.truetype(ttf_path, 20)
    draw.text((360,600), author , font=fnt, fill=(0,0,0,255))
    
    fnt = ImageFont.truetype(ttf_path, 24)
    position=(125,510) #bottom_right
    draw.text(position, guide_text , font=fnt, fill=(255,0,0,255))

    response=HttpResponse(content_type='image/png')
    canvas_im.save(response,format='PNG')
    return response