本来是想用AI生成一个小说封面,但因为AI无法生成中文所以给我发了这个代码
from PIL import Image, ImageDraw, ImageFont
# Create an empty image with white background
cover = Image.new('RGB', (600, 800), 'white')
draw = ImageDraw.Draw(cover)
# Define colors
background_color = (135, 206, 250) # Sky blue for background
tree_color = (34, 139, 34) # Forest green for the tree
title_color = (255, 215, 0) # Gold for the title
author_color = (255, 255, 255) # White for the author name
border_text_color = (176, 196, 222) # Light steel blue for border text
# Draw the background (simulate a cloud)
draw.rectangle([lbk]0, 0, 600, 800[rbk], fill=background_color)
# Draw the tree (simple representation)
draw.rectangle([lbk]250, 200, 350, 600[rbk], fill=tree_color) # Trunk
draw.ellipse([lbk]150, 100, 450, 400[rbk], fill=tree_color) # Leaves
# Load a font
try:
title_font = ImageFont.truetype("/usr/share/fonts/truetype/arphic/ukai.ttc", 50)
author_font = ImageFont.truetype("/usr/share/fonts/truetype/arphic/ukai.ttc", 30)
border_font = ImageFont.truetype("/usr/share/fonts/truetype/arphic/ukai.ttc", 20)
except IOError:
title_font = ImageFont.load_default()
author_font = ImageFont.load_default()
border_font = ImageFont.load_default()
# Add the title
title_text = "易水遥联"
title_width, title_height = draw.textsize(title_text, font=title_font)
draw.text(((600 - title_width) / 2, 50), title_text, fill=title_color, font=title_font)
# Add the author name
author_text = "常卿年"
author_width, author_height = draw.textsize(author_text, font=author_font)
draw.text(((600 - author_width) / 2, 120), author_text, fill=author_color, font=author_font)
# Add the border text
border_text = "乾 坤 坎 兑 震 离 巽 艮"
border_width, border_height = draw.textsize(border_text, font=border_font)
draw.text(((600 - border_width) / 2, 750), border_text, fill=border_text_color, font=border_font)
# Save the cover
cover_path = "/mnt/data/novel_cover.jpg"
cover.save(cover_path)
cover.show()
cover_path
from PIL import Image, ImageDraw, ImageFont
# Create an empty image with white background
cover = Image.new('RGB', (600, 800), 'white')
draw = ImageDraw.Draw(cover)
# Define colors
background_color = (135, 206, 250) # Sky blue for background
tree_color = (34, 139, 34) # Forest green for the tree
title_color = (255, 215, 0) # Gold for the title
author_color = (255, 255, 255) # White for the author name
border_text_color = (176, 196, 222) # Light steel blue for border text
# Draw the background (simulate a cloud)
draw.rectangle([lbk]0, 0, 600, 800[rbk], fill=background_color)
# Draw the tree (simple representation)
draw.rectangle([lbk]250, 200, 350, 600[rbk], fill=tree_color) # Trunk
draw.ellipse([lbk]150, 100, 450, 400[rbk], fill=tree_color) # Leaves
# Load a font
try:
title_font = ImageFont.truetype("/usr/share/fonts/truetype/arphic/ukai.ttc", 50)
author_font = ImageFont.truetype("/usr/share/fonts/truetype/arphic/ukai.ttc", 30)
border_font = ImageFont.truetype("/usr/share/fonts/truetype/arphic/ukai.ttc", 20)
except IOError:
title_font = ImageFont.load_default()
author_font = ImageFont.load_default()
border_font = ImageFont.load_default()
# Add the title
title_text = "易水遥联"
title_width, title_height = draw.textsize(title_text, font=title_font)
draw.text(((600 - title_width) / 2, 50), title_text, fill=title_color, font=title_font)
# Add the author name
author_text = "常卿年"
author_width, author_height = draw.textsize(author_text, font=author_font)
draw.text(((600 - author_width) / 2, 120), author_text, fill=author_color, font=author_font)
# Add the border text
border_text = "乾 坤 坎 兑 震 离 巽 艮"
border_width, border_height = draw.textsize(border_text, font=border_font)
draw.text(((600 - border_width) / 2, 750), border_text, fill=border_text_color, font=border_font)
# Save the cover
cover_path = "/mnt/data/novel_cover.jpg"
cover.save(cover_path)
cover.show()
cover_path