Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageSequence
- import random,os
- mainImageRaw = input("Image/GIF : ")
- mainImage = Image.open(mainImageRaw.replace("\"",""))
- FileExt = mainImageRaw.split(".")[-1]
- print()
- size = (mainImage.width,mainImage.height)
- colorRaw = input("Smear Color ( the Color you want to Smear, \"R,G,B\". like \"20,100,255\", leave empty for a random color each frame) : ").split(",")
- print()
- colorThres = int(input("Smear Color Threshold (0 is the exact color you just inputed, the higher the number, the more nearby colors will be smeared) : "))
- print()
- colorDirection = input("Smear Color Direction (the direction the color will smear too, \"X,Y\", leave empty for Random) : ").split(",")
- print()
- colorDistance = input("Smear Color Distance (you can enter a single number or a range \"Min,Max\" like so \"10,40\") : ").split(",")
- print()
- colorOffset = input("Smear Color Offset ( Distance * Color Offset + Current Color = Next Smear Pixel ) : ").split(",")
- colorOffset = ( float(colorOffset[0]),float(colorOffset[1]),float(colorOffset[2]) )
- print()
- OutputFileName = mainImageRaw.replace( "."+FileExt, "-Output."+FileExt )
- def clamp(n, minn, maxn):
- return max(min(maxn, n), minn)
- def IsAGif(MediaFile):
- try:
- Index = 0
- for Frame in ImageSequence.Iterator(MediaFile):
- Index += 1
- if Index > 1:
- return True
- else:
- return False
- except:
- return False
- def get_avg_fpms(PIL_Image_object):
- PIL_Image_object.seek(0)
- frames = duration = 0
- while True:
- try:
- frames += 1
- duration += PIL_Image_object.info['duration']
- PIL_Image_object.seek(PIL_Image_object.tell() + 1)
- except EOFError:
- return frames / duration
- return None
- if IsAGif(mainImage):
- timeEffect = input("Fade In (the effects will slowly fade in) (Y/N) : ").lower()
- frameList = []
- for frame_num in range(mainImage.n_frames):
- print("Frames : "+str(frame_num)+" / "+str(mainImage.n_frames))
- finalTimeEffect = 1
- if timeEffect == "y":
- finalTimeEffect = (frame_num/mainImage.n_frames)
- mainImage.seek(frame_num)
- new_frame = Image.new('RGBA', mainImage.size)
- new_frame.paste(mainImage)
- clone_frame = new_frame.copy()
- for x in range(new_frame.width):
- for y in range(new_frame.height):
- pixel = clone_frame.getpixel( (x,y) )
- if len(colorRaw) == 1:
- color = [ random.randint(0,255),random.randint(0,255),random.randint(0,255) ]
- else:
- color = [ int(colorRaw[0]),int(colorRaw[1]),int(colorRaw[2]) ]
- if abs(pixel[0]-color[0]) <= colorThres*finalTimeEffect and abs(pixel[1]-color[1]) <= colorThres*finalTimeEffect and abs(pixel[2]-color[2]) <= colorThres*finalTimeEffect:
- distanceRange = 0
- if len(colorDistance) == 1:
- distanceRange = int(colorDistance[0]*finalTimeEffect)
- else:
- distanceRange = random.randint(int(int(colorDistance[0])*finalTimeEffect),int(int(colorDistance[1])*finalTimeEffect))
- finalcolorDirection = None
- if len(colorDirection) == 1:
- finalcolorDirection = [((random.random()*2) - 1)*finalTimeEffect,((random.random()*2) - 1)*finalTimeEffect]
- else:
- finalcolorDirection = [ float(colorDirection[0])*finalTimeEffect,float(colorDirection[1])*finalTimeEffect ]
- for d in range(distanceRange):
- if 0 > x+int(finalcolorDirection[0]*d) >= new_frame.width or 0 > y+int(finalcolorDirection[1]*d) >= new_frame.height:
- break
- newPos = ( clamp(x+int(finalcolorDirection[0]*d),0,new_frame.width-1), clamp(y+int(finalcolorDirection[1]*d),0,new_frame.height-1) )
- newPixel = ( clamp( pixel[0]+int(colorOffset[0]*d*finalTimeEffect),0,255 ),clamp( pixel[1]+int(colorOffset[1]*d*finalTimeEffect),0,255 ),clamp( pixel[2]+int(colorOffset[2]*d*finalTimeEffect),0,255 ) )
- new_frame.putpixel(newPos , newPixel )
- new_frame = new_frame.convert(mode='P', palette=Image.ADAPTIVE)
- new_frame.info["duration"] = mainImage.info['duration']
- frameList.append(new_frame)
- duration = get_avg_fpms(mainImage)
- frameList[0].save(OutputFileName,
- save_all=True, append_images=frameList[1:], optimize=False, loop=0)
- else:
- OutputImage = mainImage.copy()
- for x in range(mainImage.width):
- print((x/mainImage.width) * 100)
- for y in range(mainImage.height):
- pixel = mainImage.getpixel( (x,y) )
- if len(colorRaw) == 1:
- color = [ random.randint(0,255),random.randint(0,255),random.randint(0,255) ]
- else:
- color = [ int(colorRaw[0]),int(colorRaw[1]),int(colorRaw[2]) ]
- if abs(pixel[0]-color[0]) <= colorThres and abs(pixel[1]-color[1]) <= colorThres and abs(pixel[2]-color[2]) <= colorThres:
- distanceRange = 0
- if len(colorDistance) == 1:
- distanceRange = int(colorDistance[0])
- else:
- distanceRange = random.randint(int(colorDistance[0]),int(colorDistance[1]))
- finalcolorDirection = None
- if len(colorDirection) == 1:
- finalcolorDirection = [(random.random()*2) - 1,(random.random()*2) - 1]
- else:
- finalcolorDirection = [ float(colorDirection[0]),float(colorDirection[1]) ]
- for d in range(distanceRange):
- if 0 > x+int(finalcolorDirection[0]*d) >= mainImage.width or 0 > y+int(finalcolorDirection[1]*d) >= mainImage.height:
- break
- newPos = ( clamp(x+int(finalcolorDirection[0]*d),0,mainImage.width-1), clamp(y+int(finalcolorDirection[1]*d),0,mainImage.height-1) )
- newPixel = ( clamp( pixel[0]+int(colorOffset[0]*d),0,255 ),clamp( pixel[1]+int(colorOffset[1]*d),0,255 ),clamp( pixel[2]+int(colorOffset[2]*d),0,255 ) )
- OutputImage.putpixel(newPos , newPixel )
- OutputImage.save(OutputFileName)
Advertisement
Add Comment
Please, Sign In to add comment