當我們要在GUI上面放上一些圖片檔,要另外提供.png檔實在是一件麻煩的事。如果可以將圖片放在Python檔裡面就可以只要提供一個檔案即可,這可以透過Base64機制達成。首先將PNG檔轉成Base64字串,很多的網站都有提供這個功能。再將字串插入到Python程式當中。等到在AEDT執行時將字串還原成圖片即可。
import base64
base64_img = '''/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBw8NDQ0OEA8NEQ0PDQ
0ODxASDQ8PEA4WGBEWGBUWFRUYHikgGBonHBcYIjEhJSkrLi4uFx8zODMsNygtLisBCgoKDg0OGx
AQGi0lICUrKy0tLS0tLS0rLSsrKy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0rLS0tLS0tLS0tLTctLf
/AABEIAMgAyAMBEQACEQEDEQH/xAAcAAEAAgMBAQEAAAAAAAAAAAAABwgBBQYEAwL/xABAEAACAQ
ICBgQIDQQDAAAAAAAAAQIDBAURBgcSIVGRExQxgRciQWFxobHRCBUjMlJUZHKCkpOj4SRDssEzYs
L/xAAaAQEAAgMBAAAAAAAAAAAAAAAABQYBAwQC/8QAJhEBAAEDAwQDAQADAAAAAAAAAAECAwQREj
EFE0FRFBUhUkJhgf/aAAwDAQACEQMRAD8Ag0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAGRoPVhljUuq1OhSWdSpLZivJ3vyI8XLkW6Zqq4h6opmurbDrPBdiX2f8AW/gjfuMb26
vg3DwXYl9n/W/gfcY3s+DcPBdiX2f9b+B9zje5JwrrPguxL7N+t/Bn7jG9yfCucuQv7SVvWqUZOL
nTm4ScXnHNPfk/KSVFe6NzkqjSXnPXLDAAAAAAAAAAAAAAAAAAAAZAknU5g23Wq3sl4tJdHT+9Jb
/V7SB63kbaItR5SODa1q3JcKqlgAJn8PDUaWYsrGxuLjPxow2YeeT3L39x24NjvX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G/k+Zd9s+ELFPrH7cPcZ+pxv5Pl3Pb5XenOJVqc6c7huE4uMlsQWaa378j1b6dj0TrF
LzOTcny5k7nOAAAAAAAAfuE3FqSbTi00+D8gHax1sY2kl1x7kl/wAVP3AZ8LWOfXP2qXuAeFrHPr
n7VL3APC1jn1z9ql7gNNpLphf4rGlG7rKqqUpSh4kIuLeWe9LzAc+AAAAAGQBkDAAAMAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAH//2Q=='''
base64_img_bytes = base64_img.encode('utf-8')
with open('d:/demo/ANSYS_Logo.png', 'wb') as file_to_save:
decoded_image_data = base64.b64decode(base64_img_bytes)
file_to_save.write(decoded_image_data)
(圖一)字串還原的圖片 |
沒有留言:
張貼留言