def rotate_img(image, angle, color, filter=Image.NEAREST): if image.mode == "P" or filter == Image.NEAREST: matte = Image.new("1", image.size, 1) # mask else: matte = Image.new("L", image.size, 255) # true matte bg = Image.new(image.mode, image.size, color) bg.paste(image.rotate(angle, filter), matte.rotate(angle, filter)) return bg
def rotate_img(image, angle, color, filter = Image.NEAREST): if image.mode == "P" or filter == Image.NEAREST: matte = Image.new("1", image.size, 1) # mask else: matte = Image.new("L", image.size, 255) # true matte bg = Image.new(image.mode, image.size, color) bg.paste( image.rotate(angle, filter), matte.rotate(angle, filter) ) return bg
def get_concat_v(row1, row2, row3): dst = Image.new('RGB', (row1.width, row1.height + row2.height + row3.height)) dst.paste(row1, (0, 0)) dst.paste(row2, (0, row1.height)) dst.paste(row3, (0, row1.height + row2.height)) return dst
def draw_animal_count(response, target_animal='Cow', frame=None): """ A function that takes PIL image and draws the count as string from rekognition response. Returns a full image as PIL """ count = 0 for label in response['CustomLabels']: if label['Name'] == target_animal: count += 1 image = copy.deepcopy(frame) number_of_animal = target_animal + ': ' + str(count) font = ImageFont.truetype('/usr/share/fonts/default/Type1/n019004l.pfb', 50) x, y = image.size text_width, text_height = font.getsize(number_of_animal) color = '#E4E1E1' button_size = (text_width + 20, text_height + 20) button_img = Image.new('RGBA', button_size, color) button_draw = ImageDraw.Draw(button_img) button_draw.text((10, 10), str(number_of_animal), fill='#000000', font=font) image.paste(button_img, ((x // 2) - text_width, y - text_height - 20)) #bottom middle return image
def get_concat_h(im1, im2, im3, im4, im5): dst = Image.new('RGB', (im1.width + im2.width + im3.width + im4.width + im5.width, im1.height)) dst.paste(im1, (0, 0)) dst.paste(im2, (im1.width, 0)) dst.paste(im3, (im1.width + im2.width, 0)) dst.paste(im4, (im1.width + im2.width + im3.width, 0)) dst.paste(im5, (im1.width + im2.width + im3.width + im4.width, 0)) return dst
def combine_images_vertical(images, y_offset=0): widths, heights = zip(*(i.size for i in images)) max_width = max(widths) total_height = sum(heights) image = Image.new('RGB', (max_width, total_height + y_offset), color=(255, 255, 255, 0)) vert_pos = y_offset for im in images: width, height = im.size image.paste(im, (0, vert_pos)) vert_pos += height + y_offset return image
def highlight(img_pil_deid, coord_df): # draw = ImageDraw.Draw(img_pil_deid) # Make a blank image the same size as the image for the rectangle, initialized # to a fully transparent (0% opaque) version of the tint color, then draw a # semi-transparent version of the square on it. overlay = Image.new('RGBA', img_pil_deid.size, TINT_COLOR+(0,)) draw = ImageDraw.Draw(overlay) # Create a context for drawing things on it. for i,row in coord_df.iterrows(): point = row['coord'] pred_label = iob_to_label(row['ner_label'].lower()) draw.rectangle((row['coord'][:2], row['coord'][2:]), fill=TINT_COLOR+(OPACITY,), outline=TINT_COLOR+(OPACITY,), width=5) # Alpha composite these two images together to obtain the desired result. img = Image.alpha_composite(img_pil_deid, overlay) img = img.convert("RGBA") # Remove alpha for saving in jpg format. # highlighted_image_list.append(img) display(img)
def append_images(images, direction='vertical', bg_color=(255,255,255), aligment='center'): """ Appends images :param images: list: list of images to append :param direction: direction in which images should be appended :param bg_color: background color in RGB :param aligment: aligment of appended images :return: PIL.Image: resulting Image """ widths, heights = zip(*(i.size for i in images)) if direction=='horizontal': new_width = sum(widths) new_height = max(heights) else: new_width = max(widths) new_height = sum(heights) new_im = Image.new('RGB', (new_width, new_height), color=bg_color) offset = 0 for im in images: if direction=='horizontal': y = 0 if aligment == 'center': y = int((new_height - im.size[1])/2) elif aligment == 'bottom': y = new_height - im.size[1] new_im.paste(im, (offset, y)) offset += im.size[0] else: x = 0 if aligment == 'center': x = int((new_width - im.size[0])/2) elif aligment == 'right': x = new_width - im.size[0] new_im.paste(im, (x, offset)) offset += im.size[1] return new_im
Data_Sets_Train = [] Data_Sets_Train.append( [SC_1846_Cortex_Train, Bulk_1846_Cortex, Bulk_1846_Liver]) for dset in Data_Sets_Train: cell = dset[0] print cell os.chdir(os.path.join(basepath, cell)) for file in glob.glob("*sml*.png"): if "mod" not in file or "blk" not in file: path = os.path.splitext(file)[0] basename = os.path.basename(path) outfile1 = basename + "_blk.png" if not os.path.isfile(os.path.join(basepath, cell, outfile1)): #newfile = re.sub("_s\d+__", "-", file) #shutil.move(file, newfile) im = Image.open(file) pixelMap = im.load() img = Image.new(im.mode, im.size) pixelsNew = img.load() for i in range(img.size[0]): for j in range(img.size[1]): if pixelMap[i, j] == (250, 250, 250): pixelsNew[i, j] = (0, 0, 0) elif pixelMap[i, j] == (255, 255, 255): pixelsNew[i, j] = (0, 0, 0) else: pixelsNew[i, j] = pixelMap[i, j] img.crop((160, 130, img.size[0], img.size[1])).resize( (512, 512)).save(outfile1) print "Done!"
%matplotlib inline desired_size = 32 im = Image.open(file_name) old_size = im.size ratio = float(desired_size)/max(old_size) new_size = tuple([int(x*ratio) for x in old_size]) new_size im = im.resize(new_size, Image.ANTIALIAS) np_im = np.array(im) np_im.shape new_im = Image.new("RGB", (desired_size, desired_size)) new_im.paste(im, ((desired_size-new_size[0])//2, (desired_size-new_size[1])//2)) np_new_im = np.array(new_im) np_new_im.shape imshow(np_new_im) # Simple way to resize image example size = 32, 32 im.resize(size, Image.ANTIALIAS) np_im = np.array(im) np_im.shape
def visualize(bucket, photo, response, display_image=False): """ Function that draws bounding boxes based on Rekognition response. """ # Load image from S3 bucket s3_connection = boto3.resource('s3') s3_object = s3_connection.Object(bucket, photo) s3_response = s3_object.get() stream = io.BytesIO(s3_response['Body'].read()) original_image = Image.open(stream) #just in case we need original image image = copy.deepcopy(original_image) imgWidth, imgHeight = image.size draw = ImageDraw.Draw(image) for customLabel in response['CustomLabels']: if 'Geometry' in customLabel: box = customLabel['Geometry']['BoundingBox'] left = imgWidth * box['Left'] top = imgHeight * box['Top'] width = imgWidth * box['Width'] height = imgHeight * box['Height'] fnt = ImageFont.truetype( '/usr/share/fonts/default/Type1/n019004l.pfb', 20) text_width, text_height = fnt.getsize(customLabel['Name']) color = determine_color(customLabel['Name']) button_size = (text_width + 20, text_height + 20) button_img = Image.new('RGBA', button_size, color) button_draw = ImageDraw.Draw(button_img) button_draw.text((10, 10), customLabel['Name'], fill='#000000', font=fnt) image.paste(button_img, (int(left) - 2, int(top - text_height - 20))) points = ((left, top), (left + width, top), (left + width, top + height), (left, top + height), (left, top)) thickness = 5 if customLabel['Name'] == 'cow': thickness = 7 draw.line(points, fill=color, width=thickness) dimensions = [imgWidth, imgHeight] #draw annotations if display_image == True: image.show() img = np.asarray(image).copy() original_image = np.asarray(original_image).copy() return img, dimensions, original_image
# In[15]: #NOMOR SATU #KONFIGURASI WAKTU ts = time.time() #PENYESUAIAN FORMAT WAKTU waktu = datetime.fromtimestamp(ts).strftime("%d/%m/%Y %H:%M:%S") #KONFIGURASI ISI TEKS YANG BERISIKAN NIM, NAMA, TANGGAL DAN WAKTU, DAN INOVASI PENAMBAHAN TULISAN teks = ("18218024 - Yonatan Jori Saputro \n"+ str(waktu) + "\n" + "UAS II3150 18218024" ) #KONFIGURASI GAMBAR TAMBAHAN BERISI TEKS addedimage = Image.new('RGB', (200, 60), color = (73, 109, 137)) #KONFIGURASI ISI DARI GAMBAR YANG BERISI GAMBAR fill = ImageDraw.Draw(addedimage) fill.text((10,10), teks, fill=(255,255,0)) #PENYIMPANAN GAMBAR TAMBAHAN KE DIRECTORY addedimage.save('addedimage.png') #LISTING GAMBAR YANG AKAN DIGUNAKAN DALAM GIF images = ["18218024_Yonatan Jori Saputro.png", "addedimage.png"] #MENAMBAHKAN FRAME frames = [] for i in images: new_frame = Image.open(i)
length = 14 qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size= length, border=0, ) qr.add_data(s1) qr.make(fit=True) img = qr.make_image(fill_color="black", back_color="white") font = ImageFont.truetype("/Library/Fonts/Microsoft/Arial.ttf",30) background = Image.open("SolarPanelLabel.png") txt=Image.new('L', (220,50)) d = ImageDraw.Draw(txt) d.text( (0, 0), "panel_id " + s1, font=font, fill=255) w=txt.rotate(90, expand=1) background.paste(img, (720-22*length, 1141-22*length)) draw = ImageDraw.Draw(background) #draw.text((720-100, 1141-5),"HELOOSLSLLSLS",(0,0,0),font=font) background.paste(ImageOps.colorize(w, (0,0,0), (0,0,0)), (720-25*length, 1141-280), w) #background.show() background = background.save(s1 + ".pdf") pdfs = [f'{panel_id:05d}' + ".pdf" for panel_id in range(int(startID),int(endID)+1)] merger = PdfFileMerger()