简体   繁体   中英

Python 3 - How can I open an image in pillow that I already opened using open('image','r')

How can I open an image in pillow that I already opened using open('image','r')
I have an image that I opened using the open() function, but i want to use the image in pillow.

Actually, I encoded it using base64, then the program decodes it,then gives you a variable that is in the same format as the open() function has. Then I just want to show the image, if there is another way to show the image without saving it, please let me know.
Here is the code that I use to decode it, just so you know:

import base64
image_64_encode = 'this-string-is-big-so'
image_64_decode = base64.decodebytes(image_64_encode)

I just want to show the image.

Like this:

from base64 import b64decode
from PIL import Image
import io

# Load useful-looking base64 string of a PNG
b64 = 'iVBORw0KGgoAAAANSUhEUgAAAEAAAABAEAIAAAB1mzrKAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0T///////8JWPfcAAAAB3RJTUUH5QsDDgY2fEahewAAANhJREFUeNrt3LENg0AQRcFFOue4/yIhN9K5jAl4UwHS02flAB977z1h1sxzr1M/xnsVAFszz7W++jHeqwVgBcAKgHUDsBaAFQArANYNwFoAVgCsAFg3AFszv/tz6sd4r15BWAGwbgDWArACYAXACoB1hLEWgBUAKwDWDcBaAFYArADYmvldn24A0wKwAmAFwPodgLUArABYAbBuANYCsAJgBcAKgHWEsRaAFQDr+wCsG4D1CsIKgBUA6wZgLQArAFYArBuAtQCsAFgBsG4A1gKwY++Z/sDe+QP/bITzNFwkpgAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyMS0xMS0wM1QxNDowNjo1NCswMDowMGz+3A4AAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjEtMTEtMDNUMTQ6MDY6NTQrMDA6MDAdo2SyAAAAAElFTkSuQmCC'

# Open it with PIL - no disk access required
im = Image.open(io.BytesIO(b64decode(b64)))
print(im)

# prints: <PIL.PngImagePlugin.PngImageFile image mode=RGB size=64x64 at 0x7FF3B90251C0>

在此处输入图片说明

The clue is here in the Pillow documentation where it says:

fp – A filename (string), pathlib.Path object or a file object .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM