Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't save PNG to sys.stdout #5436

Closed
shalomori123 opened this issue Apr 24, 2021 · 3 comments · Fixed by #5437
Closed

Can't save PNG to sys.stdout #5436

shalomori123 opened this issue Apr 24, 2021 · 3 comments · Fixed by #5437
Labels
Bug Any unexpected behavior, until confirmed feature.

Comments

@shalomori123
Copy link

shalomori123 commented Apr 24, 2021

What did you do?

Just tried to run simple code (ImageDraw)

What did you expect to happen?

What actually happened?

Exception looks very basic:

    save_handler(self, fp, filename)
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/PIL/PngImagePlugin.py", line 1225, in _save
    fp.write(_MAGIC)
TypeError: write() argument must be str, not bytes

What are your OS, Python and Pillow versions?

  • OS: Android app, "pydroid3" (I don't have a computer now to check it)
  • Python: 3.8.3
  • Pillow: 8.1.2
import sys
from PIL import Image, ImageDraw

# list of dots, in the following format: [x, y, x, y, x, y,...]
first = (146, 399, 163, 403, 170, 393, 169, 391, 166, 386, 170, 381, 170) #.... long tuple

with Image.open("draw image.jpg") as img:

    draw = ImageDraw.Draw(img)
    for i in range(len(first)//2 - 1):
    	draw.line(first[2*i : 2*i + 4])

    # write to stdout
    img.save(sys.stdout, "PNG")

Sorry if I didn't understand the concept... This is my first issue ever...

@radarhere radarhere changed the title Can't Write to file in my android app Can't save PNG to sys.stdout Apr 25, 2021
@radarhere
Copy link
Member

Hi. You're free to say that Pillow should be able to work with your code, and that you'd like it to be fixed in the next Pillow release - but if you'd like an immediate solution, I'd suggest changing sys.stdout to sys.stdout.buffer.

import sys
from PIL import Image, ImageDraw

# list of dots, in the following format: [x, y, x, y, x, y,...]
first = (146, 399, 163, 403, 170, 393, 169, 391, 166, 386, 170, 381, 170) #.... long tuple

with Image.open("draw image.jpg") as img:

    draw = ImageDraw.Draw(img)
    for i in range(len(first)//2 - 1):
    	draw.line(first[2*i : 2*i + 4])

    # write to stdout
    img.save(sys.stdout.buffer, "PNG")

This makes a difference because while sys.stdout requires strings, sys.stdout.buffer uses bytes.

>>> import sys
>>> sys.stdout.mode
'w'
>>> sys.stdout.buffer.mode
'wb'

@radarhere radarhere added the Bug Any unexpected behavior, until confirmed feature. label Apr 25, 2021
@radarhere
Copy link
Member

I've found that we have an example in our documentation that is similar to this, so yes, this is a bug.

I've created PR #5437 to resolve it. The earliest that could become a part of a Pillow release is July 1.

@shalomori123
Copy link
Author

shalomori123 commented Apr 25, 2021

Yes, I copied the code from the documentation, and that's why I was surprised that it's not working. I assumed this is problem with my system...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants