def test_errors(self): with self.assertRaisesRegexp(ValueError, "No size found"): lut = load_cube_file([ 'TITLE "LUT name from file"', ] + [ "0 0 0.031", "0.96 0 0.031", ] * 3) with self.assertRaisesRegexp(ValueError, "number of colors on line 3"): lut = load_cube_file([ 'LUT_3D_SIZE 2', ] + [ "0 0 0.031", "0.96 0 0.031 1", ] * 3) with self.assertRaisesRegexp(ValueError, "1D LUT cube files"): lut = load_cube_file([ 'LUT_1D_SIZE 2', ] + [ "0 0 0.031", "0.96 0 0.031 1", ]) with self.assertRaisesRegexp(ValueError, "Not a number on line 2"): lut = load_cube_file([ 'LUT_3D_SIZE 2', ] + [ "0 green 0.031", "0.96 0 0.031", ] * 3)
def convert_lut(src_lut_path, neutral_image_path, out_path): src_lut_file = open(src_lut_path) lines = src_lut_file.readlines() src_lut = pillow_lut.load_cube_file(lines) neutral_image = Image.open(neutral_image_path) LUT_applied_image = neutral_image.filter(src_lut) LUT_applied_image.save(out_path)
def test_parser(self): lut = load_cube_file([ " # Comment", 'TITLE "LUT name from file"', " LUT_3D_SIZE 2 3 4", " SKIP THIS", "", " # Comment", "CHANNELS 4", "", ] + [ " # Comment", "0 0 0.031 1", "0.96 0 0.031 1", "", "0 1 0.031 1", "0.96 1 0.031 1", ] * 6, target_mode='HSV') self.assertTrue(isinstance(lut, ImageFilter.Color3DLUT)) self.assertEqual(tuple(lut.size), (2, 3, 4)) self.assertEqual(lut.channels, 4) self.assertEqual(lut.name, "LUT name from file") self.assertEqual(lut.mode, 'HSV') self.assertEqual(lut.table[:12], [0, 0, 0.031, 1, 0.96, 0, 0.031, 1, 0, 1, 0.031, 1])
def test_application(self): im = Image.new('RGB', (10, 10)) lut = load_cube_file([ "LUT_3D_SIZE 2", "0 0 0.031", "0.96 0 0.031", "0 1 0.031", "0.96 1 0.031", "0 0 0.931", "0.96 0 0.931", "0 1 0.931", "0.96 1 0.931", ]) self.assertEqual(lut.table.__class__.__name__, 'list') im.filter(lut)
def test_minimal(self): lut = load_cube_file([ "LUT_3D_SIZE 2", "0 0 0.031", "0.96 0 0.031", "0 1 0.031", "0.96 1 0.031", "0 0 0.931", "0.96 0 0.931", "0 1 0.931", "0.96 1 0.931", ]) self.assertTrue(isinstance(lut, ImageFilter.Color3DLUT)) self.assertEqual(tuple(lut.size), (2, 2, 2)) self.assertEqual(lut.name, "Color 3D LUT") self.assertEqual( lut.table[:12], [0, 0, 0.031, 0.96, 0, 0.031, 0, 1, 0.031, 0.96, 1, 0.031])
def GButton_preview_command(self): """ Starts the preview of the selected filter side by side with the original """ selection = [] self.frame_no = 0 for i in self.GListBox_select_filter.curselection(): index = int(i) # i ist ein String selection.append(self.filter_list[index]) self.checkPrecondistions(selection,self.filenameOpen,self.filenameClose,True) cube = load_cube_file("cube_files/"+selection[0]) cap = cv2.VideoCapture(self.filenameOpen) ret,frame = cap.read() h,w,_ = frame.shape h = 3*h//4 w=3*w//4 max_duration = self.GEntry_time.get() if max_duration.isdecimal(): max_duration = int(max_duration) else: raise ValueError("Time must be an Integer Value") start = time.time() while(ret): frame = cv2.resize(frame,(w,h)) pil_frame = Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)) trans_frame = pil_frame.filter(cube) trans_frame = cv2.cvtColor(np.asarray(trans_frame), cv2.COLOR_RGB2BGR) result = frame.copy() result[:,w//2:] = trans_frame[:,w//2:] cv2.putText(result,"TeslaCam",(50,30),fontFace = cv2.FONT_HERSHEY_SIMPLEX, fontScale=1,color = (0,255,0),thickness=1) cv2.putText(result, "Enhanced Version", (w//2+50, 30), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(0, 255, 0), thickness=1) cv2.imshow("PREVIEW",result) cv2.waitKey(30) ret,frame = cap.read() end = time.time() if end - start >= max_duration: break cv2.destroyAllWindows()
def test_filename(self): with NamedTemporaryFile('w+t', delete=False) as f: f.write("LUT_3D_SIZE 2\n" "0 0 0.031\n" "0.96 0 0.031\n" "0 1 0.031\n" "0.96 1 0.031\n" "0 0 0.931\n" "0.96 0 0.931\n" "0 1 0.931\n" "0.96 1 0.931\n") try: lut = load_cube_file(f.name) self.assertTrue(isinstance(lut, ImageFilter.Color3DLUT)) self.assertEqual(tuple(lut.size), (2, 2, 2)) self.assertEqual(lut.name, "Color 3D LUT") self.assertEqual( lut.table[:12], [0, 0, 0.031, 0.96, 0, 0.031, 0, 1, 0.031, 0.96, 1, 0.031]) finally: os.unlink(f.name)
def GButton_start_enhancement_command(self): """ Starts the enhancement of the video by applying the selected LUT """ selection = [] for i in self.GListBox_select_filter.curselection(): index = int(i) # i ist ein String selection.append(self.filter_list[index]) self.checkPrecondistions(selection,self.filenameOpen,self.filenameClose) cube = load_cube_file("cube_files/" + selection[0]) cap = cv2.VideoCapture(self.filenameOpen) ret, frame = cap.read() h, w, _ = frame.shape fps = cap.get(cv2.CAP_PROP_FPS) max_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) counter = 0 out = cv2.VideoWriter(self.filenameClose, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h)) while (ret): frame = cv2.resize(frame, (w, h)) pil_frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) trans_frame = pil_frame.filter(cube) trans_frame = cv2.cvtColor(np.asarray(trans_frame), cv2.COLOR_RGB2BGR) self.GProgressbar["value"] = int((counter/max_frames)*100) self.GProgressbar.update() #print(int((counter/max_frames)*100)) counter += 1 out.write(trans_frame) ret, frame = cap.read() self.GProgressbar["value"] = 100 self.GProgressbar.update()
from PIL import Image import pillow_lut img = Image.open("ashlyn.jpg") lut = pillow_lut.load_cube_file("Teal Orange 01 - Classic.cube") img.filter(lut).save("temp.jpg")
def index(request): preset_packs = PresetPack.objects.all().order_by("pack_name") images = UploadedImage.objects.all() if not request.user.is_authenticated: return HttpResponseRedirect(reverse('users:login_register')) latest_image = request.user.uploaded_images.order_by('timestamp').last() if latest_image == None: return render(request, "lg_app/index.html", {}) processed_images = [] if request.method == "POST": get_pack_id = request.POST.get("preset_pack_id") if get_pack_id == None: get_pack_id = 7 # this defaults the preset pack to prevent an error if the button is pressed without selecting a preset pack uploaded_image = request.user.uploaded_images.order_by( 'timestamp').last() code = ''.join([choice(ascii_letters + digits) for i in range(50)]) user = request.user pack = PresetPack.objects.get(id=get_pack_id) presets = pack.presets.all() for preset in presets: # check if a processed image already exists for this uploaded image and preset existing_image = ProcessedImage.objects.filter( user_upload=uploaded_image, preset=preset).first() if existing_image is not None: # appends the list with existing processed images (prevents re-processing) processed_images.append(existing_image) else: lut = pillow_lut.load_cube_file(preset.preset_file.path) image = Image.open(uploaded_image.user_img.path) image = image.filter(lut) watermark(image, text=(preset.preset_name + " (" + pack.pack_name + ") " + " | nicolesy.com"), pos=(30, 40)) image = image.convert('RGB') output = BytesIO() image.save(output, format='JPEG', quality=100) output.seek(0) image = InMemoryUploadedFile(output, 'ImageField', 'image.jpg', 'image/jpeg', sys.getsizeof(output), None) preset_applied_image = ProcessedImage( image=image, preset=preset, user_upload=uploaded_image, code=code, ) preset_applied_image.save() processed_images.append(preset_applied_image) else: pack = None # this needs to be here or 'pack' shows up as not defined context = { "preset_packs": preset_packs, "images": images, "latest_image": latest_image, "processed_images": processed_images, "pack": pack, } if request.user.is_authenticated: return render(request, "lg_app/index.html", context) else: # redirects to login page if not registered return HttpResponseRedirect(reverse('users:login_register'))
def update(self): # keep looping infinitely until the thread is stopped while True: # if the thread indicator variable is set, stop the thread if self.stopped: return if not self.q_move.empty(): movie_info = self.q_move.get() capture_movie = movie_info["capture_movie"] lut = movie_info["lut"] #print("compose_movie::",movie_info["compose_movie"]) bgimg = cv2.imread(movie_info["compose_movie"]) #selected image #print("bgimg::",bgimg) bgimg = cv2.cvtColor(bgimg, cv2.IMREAD_COLOR) #print("bgimg::",bgimg) height, width, channels = bgimg.shape os.makedirs(parser.get('settings', 'image')+"/"+movie_info["id"]+"/video", exist_ok=True) video = cv2.VideoWriter(parser.get('settings', 'image')+'/'+movie_info["id"]+'/video/'+movie_info["filename"]+'_.mp4', -1 , 16.0, (width, height)) for x in range(0, len(capture_movie)): for y in range(0, len(capture_movie[x])): image = capture_movie[x][y] if(lut != None): cv2.imwrite("tmp.jpg", image) hefe = load_cube_file(parser.get('settings', 'cube')+"/"+lut+".cube") im = Image.open("tmp.jpg") ##########LOAD IMAGWE im.filter(hefe).save("tmp.jpg",quality=100) image = cv2.imread("tmp.jpg") im = Image.fromarray(image, mode='RGB') _item = None if(len(movie_info["pos_movie"]) == 1): _item = movie_info["pos_movie"][0] else: _item = movie_info["pos_movie"][x] imWidth, imHeight = im.size ## 618 721 = 603 : 704 ==> x = (618*704) / 721 _width = int((_item[2]*imHeight)/_item[3]) ## x:y = x':y' x'=(x*y')/y _height = imHeight #시작점. _startX = int((imWidth/2) - (_width/2)) _startY = 0 _im = im.crop((_startX, _startY, _startX+_width, _startY+_height)) #1920 1080 ==>1080 , 1080 _im.thumbnail((_item[2],_item[3])) frame = np.array(_im) bgimg[ _item[1]:_item[1] + frame.shape[0], _item[0]:_item[0] + frame.shape[1]] = frame ## Image Addition video.write(bgimg) for y in range(len(capture_movie[x]) -1, 0, -1): image = capture_movie[x][y] if(lut != None): cv2.imwrite("tmp.jpg", image) hefe = load_cube_file(parser.get('settings', 'cube')+"/"+lut+".cube") im = Image.open("tmp.jpg") ##########LOAD IMAGWE im.filter(hefe).save("tmp.jpg",quality=100) image = cv2.imread("tmp.jpg") im = Image.fromarray(image, mode='RGB') _item = None if(len(movie_info["pos_movie"]) == 1): _item = movie_info["pos_movie"][0] else: _item = movie_info["pos_movie"][x] imWidth, imHeight = im.size ## 618 721 = 603 : 704 ==> x = (618*704) / 721 _width = int((_item[2]*imHeight)/_item[3]) ## x:y = x':y' x'=(x*y')/y _height = imHeight #시작점. _startX = int((imWidth/2) - (_width/2)) _startY = 0 _im = im.crop((_startX, _startY, _startX+_width, _startY+_height)) #1920 1080 ==>1080 , 1080 _im.thumbnail((_item[2],_item[3])) frame = np.array(_im) bgimg[ _item[1]:_item[1] + frame.shape[0], _item[0]:_item[0] + frame.shape[1]] = frame ## Image Addition video.write(bgimg) video.release() time.sleep(1)
def save_filter(cls, lut, lut_path): hefe = load_cube_file('file/cube/{lut}.cube'.format(lut=lut)) im = Image.open(lut_path) im.filter(hefe).save(lut_path, quality=100)
def generate_photos_with_applied_filter(luts_to_use, path): for count, lut in enumerate(luts_to_use): lut_filter = load_cube_file(lut) output_image = Image.open(path) output_image.filter(lut_filter).save("name_filter" + str(count) + ".jpg")
from PIL import Image import pillow_lut import pathlib img = Image.open("smoothie-4671.jpg") path = pathlib.Path("./test_luts") for cube in path.iterdir(): if ".cube" in str(cube): lut = pillow_lut.load_cube_file(str(cube)) cube = str(cube) cube_name = cube.replace(".cube", "") cube_name = cube_name.replace("test_luts/", "") img.filter(lut).save(cube_name + ".jpg") # lut = pillow_lut.load_cube_file("Teal Orange 01 - Classic.cube") # img.filter(lut).save("temp.jpg")