def initialize(self, bars, maximum): assert bars > 0 and maximum > 0 self.index = 0 color = Image.color_for_name("white") self.image = Image.Image(bars * (self.barWidth + self.barGap), maximum * self.stepHeight, background=color)
class ImageBarRenderer: COLORS = [Image.color_for_name(name) for name in ("red", "green", "blue", "yellow", "magenta", "cyan")] def __init__(self, stepHeight=10, barWidth=30, barGap=2): self.stepHeight = stepHeight self.barWidth = barWidth self.barGap = barGap def initialize(self, bars, maximum): assert bars > 0 and maximum > 0 self.index = 0 # the number of bar to be printed color = Image.color_for_name("white") self.image = Image.Image(bars * (self.barWidth + self.barGap), maximum * self.stepHeight, background=color) # usage: Image.Image(width, height, background color) def draw_caption(self, caption): self.filename = os.path.join(tempfile.gettempdir(), re.sub(r"\W+", "_", caption) + ".xpm") # re.sub: replace \ with _ in the string caption # \W: none-char, none-num symbol; +: more than one # filename: wrote C:\Users\zhang.d\AppData\Local\Temp\Forecast_6_8.xpm def draw_bar(self, name, value): color = ImageBarRenderer.COLORS[self.index % len(ImageBarRenderer.COLORS)] width, height = self.image.size x0 = self.index * (self.barWidth + self.barGap) # the x of bar's left side x1 = x0 + self.barWidth # the x of bar's right side y0 = height - (value * self.stepHeight) # refer to the definition of rectangle below y1 = height - 1 self.image.rectangle(x0, y0, x1, y1, fill=color) self.index += 1 def finalize(self): self.image.save(self.filename) print("wrote", self.filename)
class ImageBarRenderer: COLORS = [Image.color_for_name(name) for name in ("red", "green", "blue", "yellow", "magenta", "cyan")] def __init__(self, stepHeight=10, barWidth=30, barGap=2): self.stepHeight = stepHeight self.barWidth = barWidth self.barGap = barGap def initialize(self, bars, maximum): assert bars > 0 and maximum > 0 self.index = 0 color = Image.color_for_name("white") self.image = Image.Image(bars * (self.barWidth + self.barGap), maximum * self.stepHeight, background=color) def draw_caption(self, caption): self.filename = os.path.join(tempfile.gettempdir(), re.sub(r"\W+", "_", caption) + ".xpm") def draw_bar(self, name, value): color = ImageBarRenderer.COLORS[self.index % len(ImageBarRenderer.COLORS)] width, height = self.image.size x0 = self.index * (self.barWidth + self.barGap) x1 = x0 + self.barWidth y0 = height - (value * self.stepHeight) y1 = height - 1 self.image.rectangle(x0, y0, x1, y1, fill=color) self.index += 1 def finalize(self): self.image.save(self.filename) print("wrote", self.filename)
# License, or (at your option) any later version. It is provided for # educational purposes and is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. import os import tempfile try: import cyImage as Image except ImportError: import Image YELLOW, CYAN, BLUE, RED, BLACK = (Image.color_for_name(color) for color in ("yellow", "cyan", "blue", "red", "black")) def main(): filename = os.path.join(tempfile.gettempdir(), "image.xpm") image = Image.Image(300, 60) draw_and_save_image(image, filename) filename = os.path.join(tempfile.gettempdir(), "proxy.xpm") image = ImageProxy(Image.Image, 300, 60) draw_and_save_image(image, filename) def draw_and_save_image(image, filename): image.rectangle(0, 0, 299, 59, fill=YELLOW) image.ellipse(0, 0, 299, 59, fill=CYAN)
# and/or modify it under the terms of the GNU General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. It is provided for # educational purposes and is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. import os import tempfile try: import cyImage as Image except ImportError: import Image YELLOW, CYAN, BLUE, RED, BLACK = (Image.color_for_name(color) for color in ("yellow", "cyan", "blue", "red", "black")) def main(): filename = os.path.join(tempfile.gettempdir(), "image.xpm") image = Image.Image(300, 60) draw_and_save_image(image, filename) filename = os.path.join(tempfile.gettempdir(), "proxy.xpm") image = ImageProxy(Image.Image, 300, 60) draw_and_save_image(image, filename) def draw_and_save_image(image, filename):
# published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. It is provided for # educational purposes and is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. import os import tempfile try: import cyImage as Image except ImportError: import Image YELLOW, CYAN, BLUE, RED, BLACK = (Image.color_for_name(color) for color in ("yellow", "cyan", "blue", "red", "black")) def main(): filename = os.path.join(tempfile.gettempdir(), "image.xpm") image = Image.Image(300, 60) draw_and_save_image(image, filename) filename = os.path.join(tempfile.gettempdir(), "proxy.xpm") image = ImageProxy(Image.Image, 300, 60) draw_and_save_image(image, filename) def draw_and_save_image(image, filename): image.rectangle(0, 0, 299, 59, fill=YELLOW)
""" Example from - https://github.com/azmikamis/pipbook/blob/master/any/imageproxy2.py """ import os import tempfile try: import cyImage as Image except ImportError: import Image YELLOW, CYAN, BLUE, RED, BLACK = ( Image.color_for_name(color) for color in ("yellow", "cyan", "blue", "red", "black") ) class ImageProxy: def __init__(self, ImageClass, width=None, height=None, filename=None): assert (width is not None and height is not None) or filename is not None self.Image = ImageClass self.__image = None self.commands = [] if filename is not None: self.load(filename) else: self.commands = [(self.Image, width, height)] @property def image(self):