用户:
总科室III号查看:0 回复:1 评论:0 创建时间:2021-05-23T20:20:44
【作品展示】

【作品介绍】
我的作品是通过PIL、Qt库实现一张彩色字符画的电子明信片,分为三个部分,第一部分是运载火箭,第二部分是空间站,第三部分是人造卫星。
【作品源代码】
import sys
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from ui_Earth import Ui_Earth
from PIL import Image, ImageQt, ImageDraw
class Earth(QMainWindow,Ui_Earth):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pbtn_spacecraft.clicked.connect(self.draw_1)
self.pbtn_space_station.clicked.connect(self.draw_2)
self.pbtn_satellite.clicked.connect(self.draw_3)
self.lb_display_1.setScaledContents(True)
self.lb_display_2.setScaledContents(True)
self.show()
def draw_1(self):
print("此作品致敬所有航天人。")
width, height = (60, 60)
img_original = Image.open("spacecraft.png").resize((width, height))
img = Image.open("spacecraft.png").convert("L").resize((width, height))
ASCII_HIGH = '''$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. '''
img_new = Image.new("RGB", (width*12, height*15), "white")
draw = ImageDraw.Draw(img_new)
for y in range(height):
for x in range(width):
pos = (x, y)
gray = img.getpixel(pos)
index = int(gray/256*70)
txt = ASCII_HIGH[index]+' '
color = img_original.getpixel(pos)
draw.text((x*12, y*15), txt, fill=color)
img_temp = img_new.resize((width*12, width*10)).convert("RGBA")
img_Qt = ImageQt.ImageQt(img_temp)
img_pix = QPixmap.fromImage(img_Qt)
self.lb_display_1.setPixmap(img_pix)
img = Image.open("spacecraft.png").convert("RGBA")
img_Qt = ImageQt.ImageQt(img)
img_pix = QPixmap(img_Qt)
self.lb_display_2.setPixmap(img_pix)
def draw_2(self):
print("此作品致敬所有航天人。")
width, height = (60, 60)
img_original = Image.open("space_station.png").resize((width, height))
img = Image.open("space_station.png").convert("L").resize((width, height))
ASCII_HIGH = '''$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. '''
img_new = Image.new("RGB", (width*12, height*15), "white")
draw = ImageDraw.Draw(img_new)
for y in range(height):
for x in range(width):
pos = (x, y)
gray = img.getpixel(pos)
index = int(gray/256*70)
txt = ASCII_HIGH[index]+' '
color = img_original.getpixel(pos)
draw.text((x*12, y*15), txt, fill=color)
img_temp = img_new.resize((width*12, width*10)).convert("RGBA")
img_Qt = ImageQt.ImageQt(img_temp)
img_pix = QPixmap.fromImage(img_Qt)
self.lb_display_1.setPixmap(img_pix)
img = Image.open("space_station.png").convert("RGBA")
img_Qt = ImageQt.ImageQt(img)
img_pix = QPixmap(img_Qt)
self.lb_display_2.setPixmap(img_pix)
def draw_3(self):
print("此作品致敬所有航天人。")
width, height = (60, 60)
img_original = Image.open("satellite.png").resize((width, height))
img = Image.open("satellite.png").convert("L").resize((width, height))
ASCII_HIGH = '''$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. '''
img_new = Image.new("RGB", (width*12, height*15), "white")
draw = ImageDraw.Draw(img_new)
for y in range(height):
for x in range(width):
pos = (x, y)
gray = img.getpixel(pos)
index = int(gray/256*70)
txt = ASCII_HIGH[index]+' '
color = img_original.getpixel(pos)
draw.text((x*12, y*15), txt, fill=color)
img_temp = img_new.resize((width*12, width*10)).convert("RGBA")
img_Qt = ImageQt.ImageQt(img_temp)
img_pix = QPixmap.fromImage(img_Qt)
self.lb_display_1.setPixmap(img_pix)
img = Image.open("satellite.png").convert("RGBA")
img_Qt = ImageQt.ImageQt(img)
img_pix = QPixmap(img_Qt)
self.lb_display_2.setPixmap(img_pix)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Earth()
sys.exit(app.exec_())
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn