def tweet_image(message): font = ImageFont.truetype(font="PlainPensle_BoldItalic.ttf", size=75) W, H = (1280, 720) w, h = font.getsize(message) img = Image.new('RGB', (W, H), color=(240, 226, 179)) img_w, img_h = img.size draw = ImageDraw.Draw(img) draw.text(((W - w) / 2, 585), message, font=font, fill='black') img2 = Image.open( os.path.join("C:/Users/Matthew/PycharmProjects/TwitterBot/botImages", file), 'r') img2 = img2.resize((500, 500)) img3 = img2.convert("RGBA") img3.resize((500, 500)) offset = (390, 65) img.paste(img3, offset, mask=img3) img.save('temp.png') os.remove( os.path.join("C:/Users/Matthew/PycharmProjects/TwitterBot/botImages", file)) api.update_with_media('temp.png', status=message)
def scanline(array): '''creates a scanline effect akin to those on old arcade machines and/or old screens :param The image as an array: :return The image: ''' w = len(array) h = len(array[0]) new_img = Image.new("RGBA", (w, h), "black") #makes a new blank blue image big_pixels = [[1 for _ in range(int(h / 2))] for _ in range(int(w / 2))] for i in range( 0, w, 2): #Iterates through each pixel's colors and modifies accordingly for j in range(0, h, 2): if (i != w - 1 and j != h - 1): pixel0 = array[i][j] pixel1 = array[i + 1][j] pixel2 = array[i][j + 1] pixel3 = array[i + 1][j + 1] big_pixel_array = [pixel0, pixel1, pixel2, pixel3] red = 0 green = 0 blue = 0 for pixel in big_pixel_array: red = red + pixel[0] green = green + pixel[1] blue = blue + pixel[2] red = int(red / 4) green = int(green / 4) blue = int(blue / 4) big_pixel = (red, green, blue) new_img.putpixel((i, j), big_pixel) return new_img
def modImage(array, mod="", threshold=100): '''takes an array of an image and modifies the image based on parameters :param An array of integers coresponding to an image: :param A modifier for what operation will be performed on the pixels: :param A threshold for some operations: :returns An image made from the modified array''' w = len(array) h = len(array[0]) new_img = Image.new("RGBA", (w, h), "blue") #makes a new blank blue image for i in range( w): #Iterates through each pixel's colors and modifies accordingly for j in range(h): red = array[i][j][0] green = array[i][j][1] blue = array[i][j][2] #Chooses formula based on given parameter if mod == "sepia": '''Uses Formula for Sepia''' new_red = int((red * 0.393) + (green * 0.796) + (blue * 0.189)) new_green = int((red * 0.349) + (green * 0.686) + (blue * 0.161)) new_blue = int((red * 0.272) + (green * 0.534) + (blue * 0.131)) elif mod == "greyscale": '''Uses Luminosity Formula for Greyscale''' new_red = int((red * 0.21) + (green * 0.72) + (blue * 0.07)) new_green = int((red * 0.21) + (green * 0.72) + (blue * 0.07)) new_blue = int((red * 0.21) + (green * 0.72) + (blue * 0.07)) elif mod == "dog": new_rgb = dogVision(red, green, blue) new_red = new_rgb[0] new_green = new_rgb[1] new_blue = new_rgb[2] elif mod == "dalton": new_rgb = daltonize(red, green, blue) new_red = new_rgb[0] new_green = new_rgb[1] new_blue = new_rgb[2] elif mod == "lighten": new_red = int(red + threshold) new_green = int(green + threshold) new_blue = int(blue + threshold) elif mod == "darken": '''Decreases each color by given threshold''' new_red = int(red - threshold) new_green = int(green - threshold) new_blue = int(blue - threshold) else: new_red = red new_green = green new_blue = blue new_img.putpixel((i, j), (new_red, new_green, new_blue)) return new_img
def reflect(array, axis="x"): ''' reflects the image over the selected axis :param The image array: :param The axis to reflect the image upon: :return The new Image: ''' w = len(array) h = len(array[0]) new_img = Image.new("RGBA", (w, h), "blue") for i in range(w): for j in range(h): '''swaps the pixels around in desired way''' red = array[i][j][0] green = array[i][j][1] blue = array[i][j][2] if axis == "x": new_img.putpixel((-i, j)(red, green, blue)) elif axis == "y": new_img.putpixel((i, -j)(red, green, blue)) else: new_img.putpixel((j, i), (red, green, blue)) return new_img
def get_merge_image(self, image_mix, location_list): im = image.open(image_mix) im_list_upper = [] im_list_down = [] for location in location_list: if location['y'] == -58: im_list_upper.append( im.crop((abs(location['x']), 58, abs(location['x']) + 10, 166))) if location['y'] == 0: im_list_down.append( im.crop( (abs(location['x']), 0, abs(location['x']) + 10, 58))) new_im = image.new('RGB', (260, 116)) x_offset = 0 for im in im_list_upper: new_im.paste(im, (x_offset, 0)) x_offset += im.size[0] x_offset = 0 for im in im_list_down: new_im.paste(im, (x_offset, 58)) x_offset += im.size[0] return new_im
def get_main_color(img_path): cv2_img = cv2.imread(img_path) #画像読み込み height = cv2_img.shape[0] width = cv2_img.shape[1] cv2_img = cv2.resize(cv2_img, (int(width * 0.3), int(height * 0.3))) cv2_img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB) #BGRとRGBを変換 cv2_img = cv2_img.reshape( (cv2_img.shape[0] * cv2_img.shape[1], 3)) #データ量を3から2へ cluster = KMeans(n_clusters=5) #クラスター数を指定する cluster.fit(X=cv2_img) #クラスタリングを行い、cluster_centers_にRGB値を格納 cluster_centers_arr = cluster.cluster_centers_.astype( int, copy=False) #格納されたRGB値の小数を消す IMG_SIZE = 64 #カラーパレットの一1色分の大きさ MARGIN = 15 width = IMG_SIZE * 5 + MARGIN * 2 height = IMG_SIZE + MARGIN * 2 # 幅と高さ64px × 横並び5画像 + 上下左右余白15pxずつの画像を作成 tiled_color_img = Image.new(mode='RGB', size=(width, height), color='#333333') #作成した画像をグレーにする for i, rgb_arr in enumerate( cluster_centers_arr): #iにインデックスの数字(5)、rgb_arrに上で取得したリストの要素(RGB値) color_hex_str = '#%02x%02x%02x' % tuple(rgb_arr) #RGB値をHEXに変換してPILに渡す color_hex_rev = '#%02x%02x%02x' % tuple( 255 - rgb_arr) #反対色のRGB値をHEXに変換してPILに渡す color_img = Image.new( mode='RGB', size=(IMG_SIZE, IMG_SIZE), #求めた色の正方形の画像を出力 color=color_hex_str) draw = ImageDraw.Draw(color_img) #作成した画像の上にImageDrawインスタンスを作る draw.text((0, 0), color_hex_str) #画像にHEXをいれる draw.text( (21, 54), color_hex_rev, color_hex_rev, ) tiled_color_img.paste(im=color_img, box=(MARGIN + IMG_SIZE * i, MARGIN)) #グレーの画像に各クラスタの色の画像をペーストしていく new_image = np.array(tiled_color_img, dtype=np.uint8) #PillowからCV2に変更 if new_image.ndim == 2: # モノクロ pass elif new_image.shape[2] == 3: # カラー new_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR) elif new_image.shape[2] == 4: # 透過 new_image = cv2.cvtColor(new_image, cv2.COLOR_RGBA2BGRA) path = '/Users/tani/Pictures' #保存先指定 cv2.imshow('image', new_image) #imageというウィンドウで表示 k = cv2.waitKey(0) #何かボタンを押すまで待つ if k == ord('e'): #Eを押した場合 cv2.waitKey(0) cv2.destroyAllWindows() cv2.waitKey(0) #保存せずに取り消し elif k == ord('s'): #Sを押した場合 #名前用に乱数生成 i =str(img_path) i = str(random.randint(1, 100)) #名前用に乱数生成 cv2.imwrite(os.path.join(path, 'colorpalette' + i + '.jpg'), new_image) #保存 cv2.waitKey(0) cv2.destroyAllWindows() #表示取り消し cv2.waitKey(0)