Closed andreymal closed 3 years ago
It seems like if you're importing pillow you can do most of that stuff anyway. I don't think anything in the PNG write routine that is that impressive. I think it's mostly just drawing stuff that can be done with drawline in ImageDraw anyway. Writes it all to the PngBuffer that can write a PNG with that nifty bit of code. This seems like it's trying to salvage some of the png writer code, I'm not sure it's drawing anything extremely useful. And really drawing that in Pillow itself might actually be pretty significantly faster.
Whatever the code is you'd want that with just do:
from PIL import Image, ImageDraw
extends = pattern.bounds()
pattern.translate(-extends[0], -extends[1])
width = int(extends[2] - extends[0])
height = int(extends[3] - extends[1])
draw_buff = Image.new('RGBA', (width, height))
drawer = ImageDraw.Draw(draw_buff)
for stitches, color in pattern.get_as_stitchblock():
if len(stitches) == 0:
continue
b = [(q[0], q[1]) for q in stitches]
drawer.line(b, fill=color.hex_color(), width=3)
Instead of using my silly lists pretending to be an array. This code will be much faster and you can still save it as anything you'd like.
draw_buff.save('mybitmap.bmp')
Ok this probably makes sense
In general, I planned to reuse the existing implementation so as not to reinvent it, but performance does hint this is not the best idea :)
(However a little refactoring would still be good, I think)
I figured you were trying to not reinvent the wheel. But, showing you the new flashy already assembled wheel might dissuade you from asking me to do more work.
I want to generate a bitmap and post-process it using Pillow. PyEmbroidery has
PngWriter
, but writing and reading PNG is not an optimal way. It would be nice to have direct access to the RGBA draw buffer, which I could pass to Pillow without unnecessarily saving to PNG.In the simplest case it could be easily refactored from the existing
PngWriter
module by simply removingwrite_png
(although this may not be the best api):Then I could transfer it to Pillow: