i using wand python library, , trying solve python challenge, , current problem asks me even/odd pixels.
this is, obviously, simple task. however, found wand library quite slow in loading pixels/copying pixels (maybe it's because change fill_color color of pixel each?), , wondering if load them in @ once.
my current solution loading pixels this:
from wand.image import image img = image(filename="5808.jpg") pixels = [] x in range(img.width+1): y in range(img.height+1): pixels.append(img[x, y]) print(pixels)
i'd prefer this:
from wand.image import image img = image(filename="5808.jpg") print(img.pixels)
is there akin that? in advance.
without attempting read python challenge, , focusing on question..
i'd prefer this:
img.pixels
iterating on image size collect wand.color.color
objects slow invokes repeated uses of magickwand's internal pixel iteration & pixel structure. said challenge python, , not c buffers, i'd recommend getting buffer of pixel data once. after freely able iterate, compare, evaluate, & etc. without need of imagemagick resource.
for example, i'm assuming images 2x2 png below (scaled visibility.)
from wand.image import image # prototype local variables pixels = [] width, height = 0, 0 blob = none # load image image(filename='2x2.png') image: # enforce pixels quantum between 0 & 255 image.depth = 8 # save width & height later use width, height = image.width, image.height # copy raw image data blob string blob = image.make_blob(format='rgb') # iterate on blob , collect pixels cursor in range(0, width * height * 3, 3): # save tuple of color values pixels.append((blob[cursor], # red blob[cursor + 1], # green blob[cursor + 2])) # blue print(pixels) #=> [(255, 0, 0), (0, 0, 255), (0, 128, 0), (255, 255, 255)]
this example pretty quick, remember python objects well. let's extend image
object, , create method satisfy img.pixels
.
# lets create generic pixel class easy color management class mypixel(object): red = 0 green = 0 blue = 0 def __init__(self, red=0, green=0, blue=0): self.red = red self.green = green self.blue = blue def __repr__(self): return u'#{0.red:02x}{0.green:02x}{0.blue:02x}'.format(self) # extend wand.image.image , add new `img.pixels` pseudo-attribute class myimage(image): # repeat above example @property def pixels(self): pixels = [] self.depth = 8 blob = self.make_blob(format='rgb') cursor in range(0, self.width * self.height * 3, 3): pixel = mypixel(red=blob[cursor], green=blob[cursor + 1], blue=blob[cursor + 2]) pixels.append(pixel) return pixels # call custom class; which, collects custom pixels myimage(filename=filename) img: print(img.pixels) #=> [#ff0000, #0000ff, #008000, #ffffff]
Comments
Post a Comment