def get_pixbuf(self, attr: str, val: str) -> GdkPixbuf.Pixbuf: if attr == 'category': tbl = self.rd.recipe_table.join(self.rd.categories_table) col = self.rd.categories_table.c.category if hasattr(self, 'category_images'): stment = and_( col == val, self.rd.recipe_table.c.image != None, self.rd.recipe_table.c.image != bytes(), not_(self.rd.recipe_table.c.title.in_( self.category_images))) else: stment = and_(col == val, self.rd.recipe_table.c.image != None, self.rd.recipe_table.c.image != bytes()) result = tbl.select(stment, limit=1).execute().fetchone() if not hasattr(self, 'category_images'): self.category_images = [] if result: self.category_images.append(result.title) elif attr == 'rating': return star_generator.get_pixbuf(val) elif attr in ['preptime', 'cooktime']: return get_time_slice(val) else: tbl = self.rd.recipe_table col = getattr(self.rd.recipe_table.c, attr) stment = and_(col == val, self.rd.recipe_table.c.image != None, self.rd.recipe_table.c.image != bytes()) result = tbl.select(stment, limit=1).execute().fetchone() if result and result.thumb: return scale_pb(bytes_to_pixbuf(result.image)) else: return self.get_base_icon(attr) or self.get_base_icon('category')
def get_recipe_image (rec): if rec.image: pb = scale_pb(bytes_to_pixbuf(rec.image)) else: pb = generic_recipe_image.copy() big_side = ((pb.get_property('height') > pb.get_property('width') and pb.get_property('height')) or pb.get_property('width')) if rec.rating: #sg = get_star_generator() sg = star_generator ratingPB = sg.get_pixbuf(rec.rating) h = pb.get_height() - ratingPB.get_height() - 5 w = pb.get_width() - ratingPB.get_width() - 5 if h < 0: h = 0 if w < 0: w = 0 if ratingPB.get_property('width') > pb.get_property('width'): SCALE = float(pb.get_property('width'))/ratingPB.get_property('width') else: SCALE = 1 ratingPB.composite( pb, w, #dest_x h, # dest_y int(ratingPB.get_width()*SCALE), # dest_width, int(ratingPB.get_height()*SCALE), #dest_height w, #offset_x, h, #offset_y SCALE,SCALE, #scale_x,scale_y GdkPixbuf.InterpType.BILINEAR, 255 # overall_alpha ) if rec.preptime: #prepPB = get_time_slice(rec.preptime) prepPB = make_preptime_icon(rec.preptime) prepPB = prepPB.scale_simple(int(big_side*0.4),int(big_side*0.4),GdkPixbuf.InterpType.BILINEAR) prepPB.composite( pb, pb.get_property('width')/2 + 5,5, prepPB.get_property('width'),prepPB.get_property('height'), pb.get_property('width')/2 + 5,5, 1,1,GdkPixbuf.InterpType.BILINEAR, 127 # alpha ) if rec.cooktime: cookPB = make_cooktime_icon(rec.cooktime) cookPB = cookPB.scale_simple(int(big_side*0.4),int(big_side*0.4),GdkPixbuf.InterpType.BILINEAR) cookPB.composite( pb, pb.get_property('width')/2 + 5,pb.get_property('height')/2, cookPB.get_property('width'),cookPB.get_property('height'), pb.get_property('width')/2 + 5,pb.get_property('height')/2, 1,1,GdkPixbuf.InterpType.BILINEAR, 188 # alpha ) return pb
def test_pixbuf_to_image(): pixbuf = bytes_to_pixbuf(IMAGE) image = pixbuf_to_image(pixbuf) assert isinstance(image, Image.Image)
def test_bytes_to_pixbuf(): pixbuf = bytes_to_pixbuf(IMAGE) assert isinstance(pixbuf, Pixbuf)