Fake Album Cover Game Lab | Coursera Community
Coursera Header
Solved

Fake Album Cover Game Lab

  • 27 January 2019
  • 3 replies
  • 837 views

Badge +1
Hello in Lab (JupyterNotebook) I run program..
*********************************************
PROGRAM
*********************************************
from IPython.display import Image as IPythonImage
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

def display_cover(top,bottom ):
"""This fucntoin
"""
import requests

name='album_art_raw.png'
# Now let's make get an album cover.
# https://picsum.photos/ is a free service that offers random images.
# Let's get a random image:
album_art_raw = requests.get('https://picsum.photos/500/500/?random')
# and save it as 'album_art_raw.png'
with open(name,'wb') as album_art_raw_file:
album_art_raw_file.write(album_art_raw.content)
# Now that we have our raw image, let's open it
# and write our band and album name on it
img = Image.open("album_art_raw.png")
draw = ImageDraw.Draw(img)

# We'll choose a font for our band and album title,
# run "% ls /usr/share/fonts/truetype/dejavu" in a cell to see what else is available,
# or download your own .ttf fonts!
band_name_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 25) #25pt font
album_name_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20) # 20pt font

# the x,y coordinates for where our album name and band name text will start
# counted from the top left of the picture (in pixels)
band_x, band_y = 50, 50
album_x, album_y = 50, 400

# Our text should be visible on any image. A good way
# of accomplishing that is to use white text with a
# black border. We'll use the technique shown here to draw the border:
# https://mail.python.org/pipermail/image-sig/2009-May/005681.html
outline_color ="black"

draw.text((band_x-1, band_y-1), top, font=band_name_font, fill=outline_color)
draw.text((band_x+1, band_y-1), top, font=band_name_font, fill=outline_color)
draw.text((band_x-1, band_y+1), top, font=band_name_font, fill=outline_color)
draw.text((band_x+1, band_y+1), top, font=band_name_font, fill=outline_color)

draw.text((album_x-1, album_y-1), bottom , font=album_name_font, fill=outline_color)
draw.text((album_x+1, album_y-1), bottom , font=album_name_font, fill=outline_color)
draw.text((album_x-1, album_y+1), bottom , font=album_name_font, fill=outline_color)
draw.text((album_x+1, album_y+1), bottom , font=album_name_font, fill=outline_color)

draw.text((band_x,band_y),top,(255,255,255),font=band_name_font)
draw.text((album_x, album_y),bottom,(255,255,255),font=album_name_font)

return img

img=display_cover(top='test',bottom='test')
img.save('sample-out.png')
IPythonImage(filename='sample-out.png')
*****************************************************************************

after program end I have error message...pls help me thx everyone

code:


---------------------------------------------------------------------------
OSError Traceback (most recent call last)
in ()
----> 1 img=display_cover(top='test',bottom='test')
2 img.save('sample-out.png')
3 IPythonImage(filename='sample-out.png')

in display_cover(top, bottom)
23 # run "% ls /usr/share/fonts/truetype/dejavu" in a cell to see what else is available,
24 # or download your own .ttf fonts!
---> 25 band_name_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 25) #25pt font
26 album_name_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20) # 20pt font
27

~/anaconda3/lib/python3.6/site-packages/PIL/ImageFont.py in truetype(font, size, index, encoding, layout_engine)
258
259 try:
--> 260 return FreeTypeFont(font, size, index, encoding, layout_engine)
261 except IOError:
262 ttf_filename = os.path.basename(font)

~/anaconda3/lib/python3.6/site-packages/PIL/ImageFont.py in __init__(self, font, size, index, encoding, layout_engine)
141
142 if isPath(font):
--> 143 self.font = core.getfont(font, size, index, encoding, layout_engine=layout_engine)
144 else:
145 self.font_bytes = font.read()

OSError: cannot open resource








code:
icon

Best answer by hamster 28 January 2019, 23:40

If you read the traceback, it looks like you do not have that font installed.

Either install the font (i presume the course explains how to do that) or change the font to something on your PC.

also, using the code tag makes things easier to read:

code:
def hello_world():
return "hello world!"
View original

3 replies

Userlevel 7
Badge +13
Hi @matej. Is this related to a course assignment? If so, I suggest posting your question in your course discussion forum. I've added a couple tags here – maybe this will help people familiar with Jupyter Notebook to find your post!
Userlevel 4
Badge +3
If you read the traceback, it looks like you do not have that font installed.

Either install the font (i presume the course explains how to do that) or change the font to something on your PC.

also, using the code tag makes things easier to read:

code:
def hello_world():
return "hello world!"
Badge +1
thank you 🙂

Reply