如何将在 Flask 服务器上创建的 PIL 图像下载到我的桌面。 Image.save() 仅将其保存到服务器。无法通过 URL 访问图像
How to download a PIL Image created on a Flask server to my desktop. Image.save() only saves it to the server. Cant access the image by URL
所以我创建了一个 PIL 图像并将其保存到服务器。这很好,但我想尽可能避免将这些图像保存到服务器,而只是保存到用户的桌面。我尝试查找下载文件的方法,但几乎所有方法似乎都涉及 url,这不适用于我的情况。
我的代码:
@app.route('/createAlbumStory', methods = ['GET'])
def create_album_story():
album = scrapeAlbumDB()
albumDict = json.loads(album)
items = albumDict
user = str(session['username']) + "_createdAlbumStory.jpeg"
width, height = (1080, 1920)
img = Image.new('RGB', (width, height), (33, 33, 33))
draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("/home/Spotify365/mysite/GothamBold.ttf", 50)
fontB = ImageFont.truetype("/home/Spotify365/mysite/GothamBold.ttf", 30)
# topText = str(session['username']) + "'s Top 5 Played Albums"
topText = "user's Top 5 Played Albums"
tW, tH = draw.textsize(topText, font)
draw.text(((width - tW)/2, 100), topText ,(255, 255, 255),font)
# NEED TO CHECK IF THIS LOCAL TRACK IF SO SHIFT TO NEXT ARTIST
# img.paste(topSongImage, (340, 250))
for i in range(0, 5):
topSongImage = Image.open(urllib.request.urlopen(items[i][1][1]))
topSongImage = topSongImage.resize((200, 200))
img.paste(topSongImage, (100, 250 + (i * 300) - 50))
draw.rectangle(((325, 200 + (i * 300)), (975, 400 + (i * 300))), fill="white")
songText = str(items[i][0][0]) + "-" + str(items[i][0][1])
position = (350, 300 + (i * 300) - 50)
draw.text(position, songText, (0, 0, 0), fontB)
timeText = str(convert(items[i][1][0]))
position = (350, 375 + (i * 300) - 50)
draw.text(position, timeText, (0, 0, 0), fontB)
adText = "Generated by Spotify365"
tW, tH = draw.textsize(adText, font)
draw.text(((width - tW)/2, 1670), adText ,(255, 255, 255),font)
userText = "@spotify_365"
tW, tH = draw.textsize(userText, font)
# draw.text(((width - tW)/2, 1820), userText ,(255, 255, 255),font)
img.save(user)
return str(user)
我的意思是,我不知道如何将它保存到桌面,但是你绝对可以让他们下载它:
return send_from_directory(directory=folder, filename=filename)
其中 directory
是文件夹,filename
是您要发送的文件的名称
这可能意味着您必须先将其保存到文件中,因此可能有更好的方法来执行此操作。
您可以使用此主题了解更多信息:
Flask Download a File
答案:
send_file(image, as_attachment=True)
as-attachment 具体来说就是将文件下载到您的桌面
所以我创建了一个 PIL 图像并将其保存到服务器。这很好,但我想尽可能避免将这些图像保存到服务器,而只是保存到用户的桌面。我尝试查找下载文件的方法,但几乎所有方法似乎都涉及 url,这不适用于我的情况。
我的代码:
@app.route('/createAlbumStory', methods = ['GET'])
def create_album_story():
album = scrapeAlbumDB()
albumDict = json.loads(album)
items = albumDict
user = str(session['username']) + "_createdAlbumStory.jpeg"
width, height = (1080, 1920)
img = Image.new('RGB', (width, height), (33, 33, 33))
draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("/home/Spotify365/mysite/GothamBold.ttf", 50)
fontB = ImageFont.truetype("/home/Spotify365/mysite/GothamBold.ttf", 30)
# topText = str(session['username']) + "'s Top 5 Played Albums"
topText = "user's Top 5 Played Albums"
tW, tH = draw.textsize(topText, font)
draw.text(((width - tW)/2, 100), topText ,(255, 255, 255),font)
# NEED TO CHECK IF THIS LOCAL TRACK IF SO SHIFT TO NEXT ARTIST
# img.paste(topSongImage, (340, 250))
for i in range(0, 5):
topSongImage = Image.open(urllib.request.urlopen(items[i][1][1]))
topSongImage = topSongImage.resize((200, 200))
img.paste(topSongImage, (100, 250 + (i * 300) - 50))
draw.rectangle(((325, 200 + (i * 300)), (975, 400 + (i * 300))), fill="white")
songText = str(items[i][0][0]) + "-" + str(items[i][0][1])
position = (350, 300 + (i * 300) - 50)
draw.text(position, songText, (0, 0, 0), fontB)
timeText = str(convert(items[i][1][0]))
position = (350, 375 + (i * 300) - 50)
draw.text(position, timeText, (0, 0, 0), fontB)
adText = "Generated by Spotify365"
tW, tH = draw.textsize(adText, font)
draw.text(((width - tW)/2, 1670), adText ,(255, 255, 255),font)
userText = "@spotify_365"
tW, tH = draw.textsize(userText, font)
# draw.text(((width - tW)/2, 1820), userText ,(255, 255, 255),font)
img.save(user)
return str(user)
我的意思是,我不知道如何将它保存到桌面,但是你绝对可以让他们下载它:
return send_from_directory(directory=folder, filename=filename)
其中 directory
是文件夹,filename
是您要发送的文件的名称
这可能意味着您必须先将其保存到文件中,因此可能有更好的方法来执行此操作。
您可以使用此主题了解更多信息: Flask Download a File
答案:
send_file(image, as_attachment=True)
as-attachment 具体来说就是将文件下载到您的桌面