Pillow pasting doesn't overlay correctly - python-3.x

I'm trying to merge 2 images together using Pillow. A problem comes when some images have the in itself the same image as the other one, but with transparency (I'll post an example later). Here's the code I'm using:
import os, subprocess
from PIL import Image, ImageDraw, ImageFilter
# Creates card art
rootdir = os.listdir('C:/Users/amati/Desktop/Dokkan Assets/japan/character/card')
for entries in rootdir:
newdir = 'C:/Users/amati/Desktop/Dokkan Assets/japan/character/card/' + str(entries)
bg = newdir + '/' + 'card_' + str(entries) + '_bg.png'
effect = newdir + '/' + 'card_' + str(entries) + '_effect.png'
try:
image1 = Image.open(bg)
image3 = Image.open(effect)
rgbimage1 = image1.convert('RGBA')
rgbimage3 = image3.convert('RGBA')
image1_size = rgbimage1.size
final = Image.new('RGBA',(image1_size[0], image1_size[1]), (0,0,0,255))
final.paste(rgbimage1,(0,0))
final.paste(rgbimage3,(0,0),rgbimage3)
if not os.path.exists('C:/Users/amati/Desktop/prova/card_' + str(entries) + '.png'):
final.save('C:/Users/amati/Desktop/prova/card_' + str(entries) + '.png', quality = 95)
except IOError:
continue
Images:
Image 1
Image 2
Result
Expected result using Paint 3D

Related

Scraping Billboard Data with Python; code doesn't crawl to previous chart

First of all, I'm a relative newbie to coding. My goal is to scrape at least the last decade of Billboard Hot 100 charts using the Python code below with billboard.py. My hiccup is I have tried a few variants of while loop statements and none have seemed to work to get me to the previous chart. I have an idea of how it should look from the billboard.py documentation but for whatever reason my code terminates prematurely or outputs an AttributeError: 'ChartEntry' object has no attribute 'previousDate'
Any advice on debugging this and/or corrective code is appreciated. Thank you.
import billboard
import csv
chart = billboard.ChartData('hot-100')
#chart = billboard.ChartData('hot-100', date=None, fetch=True, max_retries=5, timeout=25)
f = open('Hot100.csv', 'w')
headers = 'title, artist, peakPos, lastPos, weeks, rank, date\n'
f.write(headers)
while chart.previousDate:
date = chart.date
for chart in chart:
title = chart.title
artist = chart.artist
peakPos = str(chart.peakPos)
lastPos = str(chart.lastPos)
weeks = str(chart.weeks)
rank = str(chart.rank)
f.write('\"' + title + '\",\"' + artist.replace('Featuring', 'Feat.') + '\",' + peakPos + ',' + lastPos + ',' + weeks + ',' + rank + ',' + date + '\n')
chart = billboard.ChartData('hot-100', chart.previousDate)
f.close()
I figured it out. I had to change how my script was comprehending the for loop.
My revised code below
import billboard
import csv
chart = billboard.ChartData('hot-100')
#chart = billboard.ChartData('hot-100', date=None, fetch=True, max_retries=5, timeout=25)
f = open('hot-100.csv', 'w')
headers = 'title, artist, peakPos, lastPos, weeks, rank, date\n'
f.write(headers)
date = chart.date
while chart.previousDate:
date = chart.date
for song in chart:
title = song.title
artist = song.artist
peakPos = str(song.peakPos)
lastPos = str(song.lastPos)
weeks = str(song.weeks)
rank = str(song.rank)
f.write('\"' + title + '\",\"' + artist.replace('Featuring', 'Feat.') + '\",' + peakPos + ',' + lastPos + ',' + weeks + ',' + rank + ',' + date + '\n')
chart = billboard.ChartData('hot-100', chart.previousDate)
f.close()

Why librosa.load() Runs Out of Memory when generating spectrograms?

I am working on classifying sounds with Deep Learning and my problem is that I run out of memory when I try to convert .wav files to spectrograms using lib.load() from librosa.
split = ['train','val']
categories=['URTI', 'Healthy', 'Asthma', 'COPD', 'LRTI', 'Bronchiectasis','Pneumonia', 'Bronchiolitis']
files_loc = "path"
i=0
for s in split:
for cat in categories:
print('-' * 100)
print('working on ' + cat +" "+str(s)+" "+ '...')
print('-' * 100)
files = [f for f in listdir(files_loc + s + '/' + cat + '/') if isfile(join(files_loc + s + '/' + cat + '/', f)) and is_wav(f)]
for f in files:
convert_to_spec_image(file_loc = files_loc, category=cat, filename=f, is_train=(s == 'train'), verbose=False)
i=i+1
print("We have processed: "+str(i)+" "+ str((i/773*100))+" % "+" so far")
The function convert_to_spec_image is this:
#create images using librosa spectogram
def convert_to_spec_image(file_loc, filename, category, is_train=False, verbose=False):
'''
Converts audio file to spec image
Input file includes path
Saves the file to a png image in the save_directory
'''
train_ = 'train/'
val_ = 'val/'
loc = file_loc + train_ + category + '/' + filename
if is_train == False:
loc = file_loc + val_ + category + '/' + filename
if verbose == True:
print('reading and converting ' + filename + '...')
y, sr = lb.load(loc)
#Plot signal in
plt.figure(figsize=(10,3))
src_ft = lb.stft(y)
src_db = lb.amplitude_to_db(abs(src_ft))
specshow(src_db, sr=sr, x_axis='time', y_axis='hz')
plt.ylim(0, 5000)
save_directory = "C:/Users/raulf/Desktop/espectograms2/"
filename_img = filename.split('.wav')[0]
save_loc = save_directory + train_ + category + '/' + filename_img + '.png'
if is_train == False:
save_loc = save_directory + val_ + category + '/' + filename_img + '.png'
plt.savefig(save_loc)
plt.close('all')
if verbose == True:
print(filename + ' converted!')
plt.close('all')
I am trying to reuse the code from this Kaggle Notebook:
https://www.kaggle.com/danaelisanicolas/cnn-part-3-create-spectrogram-images
Thanks in advance

Implementing a YOLOv3 object detector for the SVHN dataset

I am trying to use object detection for digit detection.
I found the SVHN dataset.
Speed is important in my project so I decided to apply a YOLO approach.
However, all tutorials and explanatiosn on using YOLOv3 either expect me to be using a dataset made from the Google Open Images API or by manually labeling images using a tool such as labellimg.py.
I however have a premade dataset with annotaions in the PASCAL VOC format (which can be found here https://github.com/penny4860/svhn-voc-annotation-format). Because of this I do not create a labels.txt or classes.txt file as I do no labeling myself.
I am rather at a loss on where to get started.
Any help would be appreciated.
You can follow the below code to convert from PASCAL VOC to YOLO supported format.
import glob
import os
import pickle
import xml.etree.ElementTree as ET
from os import listdir, getcwd
from os.path import join
dirs = ['train', 'val']
classes = ['person', 'car']
def getImagesInDir(dir_path):
image_list = []
for filename in glob.glob(dir_path + '/*.jpg'):
image_list.append(filename)
return image_list
def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
def convert_annotation(dir_path, output_path, image_path):
basename = os.path.basename(image_path)
basename_no_ext = os.path.splitext(basename)[0]
in_file = open(dir_path + '/' + basename_no_ext + '.xml')
out_file = open(output_path + basename_no_ext + '.txt', 'w')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
bb = convert((w,h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
cwd = getcwd()
for dir_path in dirs:
full_dir_path = cwd + '/' + dir_path
output_path = full_dir_path +'/yolo/'
if not os.path.exists(output_path):
os.makedirs(output_path)
image_paths = getImagesInDir(full_dir_path)
list_file = open(full_dir_path + '.txt', 'w')
for image_path in image_paths:
list_file.write(image_path + '\n')
convert_annotation(full_dir_path, output_path, image_path)
list_file.close()
print("Finished processing: " + dir_path)

How do I save images to folder after face recognized and cropped?

I am working on face detecting and then cropping face from image, i can crop the face but I can't save them to another folder. My code is below
import cv2
import os
import glob
def facecrop(image):
facedata = "haarcascade_frontalface_alt.xml"
cascade = cv2.CascadeClassifier(facedata)
img = cv2.imread(image)
minisize = (img.shape[1],img.shape[0])
miniframe = cv2.resize(img, minisize)
faces = cascade.detectMultiScale(miniframe)
for f in faces:
x, y, w, h = [ v for v in f ]
#cv2.rectangle(img, (x,y), (x+w,y+h), (0,0,0))
sub_face = img[y:y+h + 500, x:x+w + 500]
fname, ext = os.path.splitext(image)
save = "./salman/crop/"
#print(fname)
#cv2.imwrite(f'{save}"cropped_"{image}{sub_face}')
cv2.imwrite(fname + "_cropped_"+ext, sub_face)
#cv2.imwrite(os.path.join(p),(fname + "_cropped_" + ext, sub_face))
return
for image in glob.glob('./salman/*.jpg'):
facecrop(image)

How to call a function within a string for a title

I have a script that produces 4 images (Below I only include 2 as an example of output). I have another function that determines what % cat or dog the picture is and I would like to call that function in the title of the following code
import cv2
import matplotlib.pyplot as plt
def Mpic():
plt.figure(figsize=(15,30))
path = r"data/dogscats1/pupper"
path1 = r"data/dogscatspeople/test1"
path2 = r"data/dogscatspeople/test1"
path3 = r"data/dogscats1/pupper"
imgpath1 = path + "/cat.jpg"
imgpath2 = path1 + "/1.jpg"
imgpath3 = path2 + "/2.jpg"
imgpath4 = path3 + "/dog.jpg"
img1 = cv2.imread(imgpath1, 1)
img2 = cv2.imread(imgpath2, 1)
img3 = cv2.imread(imgpath3, 1)
img4 = cv2.imread(imgpath4, 1)
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB)
img4 = cv2.cvtColor(img4, cv2.COLOR_BGR2RGB)
titles = ['Kitty', '% Cat = , Dog % = ','% Cat = , Dog % =', 'Pupper']
images = [img1, img2, img3, img4]
for i in range(4):
plt.subplot(4,2,i+1)
plt.imshow(images[i])
plt.xticks([])
plt.title(titles[i])
plt.yticks([])
plt.show()
if __name__ == "__main__":
Mpic()
This is the original function that calls the array:
def pred_datsci(file_path):
prev_precompute = learn.precompute
learn.precompute = False
try:
trn_tfms, val_tfms = tfms_from_model(arch,sz)
test_img = open_image(file_path)
im = val_tfms(test_img)
pred = learn.predict_array(im[None])
class_index = (np.exp(pred))
class_index1 = np.argmax(np.exp(pred))
print(class_index*100)
return data.classes[class_index1]
finally:
learn.precompute = prev_precompute
Which can return something along the lines of:
pred_datsci(f"data/dogscats1/valid/dogs/12501.jpg")
I want it to call it in the form of something like this:
titles = [ cat % = pred_datsci(f"data/dogscats1/valid/cats/cat.1.jpg"),"etc"]
titles = [ "cat % = {}".format(pred_datsci("data/dogscats1/valid/cats/cat.1.jpg")),"etc"]

Resources