tobytes()
method is returning actually what I need to render the image as a string
Hi. tobytes()
is returning data from our "raw" encoder.
I would convert the image to "1" mode first (black and white) and then loop over the pixels and print 0 or 1 accordingly.
from PIL import Image
with Image.open("logo.png") as im:
(width, height) = (im.width // 300, im.height // 300)
im_resized = im.resize((width, height))
im_resized = im_resized.convert("1")
px = im_resized.load()
bitmap = [
"".join([
"1" if px[x, y] == 255 else "0"
for x in range(im_resized.width)
])
for y in range(im_resized.height)
]
for i in bitmap:
print(i)
----------------------------------------
I expect you're using Python before 3.6. Pillow 9.0.1 only supports Python 3.7 and greater.
For more information, see https://pillow.readthedocs.io/en/stable/installation.html#python-support
get_emoji("🐶")
that returns images of open-source emojis OpenMoji:from PIL import Image
import numpy as np
import requests
#get openmoji
def get_emoji(emoji):
emoji_code = "-".join(f"{ord(c):x}" for c in emoji)
emoji_code = emoji_code.upper()
url = f"https://raw.githubusercontent.com/hfg-gmuend/openmoji/master/color/72x72/{emoji_code}.png"
image_pil = Image.open(requests.get(url, stream=True).raw)
image = np.array(image_pil.convert("RGBA"))
return image
get_emoji("🐶")
I can imagine people being interested in how to show emojis, but your example doesn't have all that much to do with Pillow. It demonstrates how to open an image from a URL, but apart from that, I think it would seem like a random advertisement for another library in the middle of the Pillow documentation.
If you think the documentation should be updated to include an example of how to open an image from a URL, that would make sense to me. It's in the docs at the moment, but buried in release notes - https://pillow.readthedocs.io/en/stable/releasenotes/2.8.0.html#open-http-response-objects-with-image-open
from PIL import Image, ImageDraw, ImageFont
im = Image.new("RGBA", (130, 120))
d = ImageDraw.Draw(im)
font = ImageFont.truetype("NotoColorEmoji.ttf", 109)
d.text((0, 0), "🐶", "#f00", font)
im.save("out.png")
from urllib.request import urlopen
from PIL import Image
url = "https://raw.githubusercontent.com/python-pillow/pillow-logo/main/pillow-logo-248x250.png"
img = Image.open(urlopen(url))