Guest User

Fake Pixel Sorter

a guest
Sep 13th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.39 KB | None | 0 0
  1. from PIL import Image, ImageSequence
  2. import random,os
  3.  
  4.  
  5. mainImageRaw = input("Image/GIF : ")
  6. mainImage = Image.open(mainImageRaw.replace("\"",""))
  7. FileExt = mainImageRaw.split(".")[-1]
  8.  
  9.  
  10.  
  11. print()
  12. size = (mainImage.width,mainImage.height)
  13. 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(",")
  14. print()
  15. 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) : "))
  16. print()
  17. colorDirection = input("Smear Color Direction (the direction the color will smear too, \"X,Y\", leave empty for Random) : ").split(",")
  18. print()
  19.  
  20.  
  21. colorDistance = input("Smear Color Distance (you can enter a single number or a range \"Min,Max\" like so \"10,40\") : ").split(",")
  22. print()
  23. colorOffset = input("Smear Color Offset ( Distance * Color Offset + Current Color = Next Smear Pixel ) : ").split(",")
  24. colorOffset = ( float(colorOffset[0]),float(colorOffset[1]),float(colorOffset[2]) )
  25. print()
  26. OutputFileName = mainImageRaw.replace( "."+FileExt, "-Output."+FileExt )
  27.  
  28. def clamp(n, minn, maxn):
  29.     return max(min(maxn, n), minn)
  30.  
  31. def IsAGif(MediaFile):
  32.     try:
  33.         Index = 0
  34.         for Frame in ImageSequence.Iterator(MediaFile):
  35.             Index += 1
  36.         if Index > 1:
  37.             return True
  38.         else:
  39.             return False
  40.     except:
  41.         return False
  42.  
  43. def get_avg_fpms(PIL_Image_object):
  44.     PIL_Image_object.seek(0)
  45.     frames = duration = 0
  46.     while True:
  47.         try:
  48.             frames += 1
  49.             duration += PIL_Image_object.info['duration']
  50.             PIL_Image_object.seek(PIL_Image_object.tell() + 1)
  51.         except EOFError:
  52.             return frames / duration
  53.     return None
  54.  
  55.  
  56. if IsAGif(mainImage):
  57.  
  58.     timeEffect = input("Fade In (the effects will slowly fade in) (Y/N) : ").lower()
  59.  
  60.     frameList = []
  61.    
  62.     for frame_num in range(mainImage.n_frames):
  63.         print("Frames : "+str(frame_num)+" / "+str(mainImage.n_frames))
  64.         finalTimeEffect = 1
  65.         if timeEffect == "y":
  66.             finalTimeEffect = (frame_num/mainImage.n_frames)
  67.         mainImage.seek(frame_num)
  68.         new_frame = Image.new('RGBA', mainImage.size)
  69.         new_frame.paste(mainImage)
  70.         clone_frame = new_frame.copy()
  71.         for x in range(new_frame.width):
  72.             for y in range(new_frame.height):
  73.                 pixel = clone_frame.getpixel( (x,y) )
  74.                 if len(colorRaw) == 1:
  75.                     color = [ random.randint(0,255),random.randint(0,255),random.randint(0,255) ]
  76.                 else:
  77.                     color = [ int(colorRaw[0]),int(colorRaw[1]),int(colorRaw[2]) ]
  78.                 if abs(pixel[0]-color[0]) <= colorThres*finalTimeEffect and abs(pixel[1]-color[1]) <= colorThres*finalTimeEffect and abs(pixel[2]-color[2]) <= colorThres*finalTimeEffect:
  79.                     distanceRange = 0
  80.                     if len(colorDistance) == 1:
  81.                         distanceRange = int(colorDistance[0]*finalTimeEffect)
  82.                     else:
  83.                         distanceRange = random.randint(int(int(colorDistance[0])*finalTimeEffect),int(int(colorDistance[1])*finalTimeEffect))
  84.                     finalcolorDirection = None
  85.                     if len(colorDirection) == 1:
  86.                         finalcolorDirection = [((random.random()*2) - 1)*finalTimeEffect,((random.random()*2) - 1)*finalTimeEffect]
  87.                     else:
  88.                         finalcolorDirection = [ float(colorDirection[0])*finalTimeEffect,float(colorDirection[1])*finalTimeEffect ]
  89.                    
  90.                     for d in range(distanceRange):
  91.                        
  92.                        
  93.                         if 0 > x+int(finalcolorDirection[0]*d) >= new_frame.width or 0 > y+int(finalcolorDirection[1]*d) >= new_frame.height:
  94.                             break
  95.                         newPos = ( clamp(x+int(finalcolorDirection[0]*d),0,new_frame.width-1), clamp(y+int(finalcolorDirection[1]*d),0,new_frame.height-1)  )
  96.                         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 ) )
  97.                         new_frame.putpixel(newPos , newPixel )
  98.         new_frame = new_frame.convert(mode='P', palette=Image.ADAPTIVE)
  99.         new_frame.info["duration"] = mainImage.info['duration']
  100.         frameList.append(new_frame)
  101.    
  102.     duration = get_avg_fpms(mainImage)
  103.    
  104.     frameList[0].save(OutputFileName,
  105.                save_all=True, append_images=frameList[1:], optimize=False, loop=0)
  106. else:
  107.     OutputImage = mainImage.copy()
  108.     for x in range(mainImage.width):
  109.         print((x/mainImage.width) * 100)
  110.         for y in range(mainImage.height):
  111.             pixel = mainImage.getpixel( (x,y) )
  112.            
  113.             if len(colorRaw) == 1:
  114.                 color = [ random.randint(0,255),random.randint(0,255),random.randint(0,255) ]
  115.             else:
  116.                 color = [ int(colorRaw[0]),int(colorRaw[1]),int(colorRaw[2]) ]
  117.             if abs(pixel[0]-color[0]) <= colorThres and abs(pixel[1]-color[1]) <= colorThres and abs(pixel[2]-color[2]) <= colorThres:
  118.                
  119.                 distanceRange = 0
  120.                 if len(colorDistance) == 1:
  121.                     distanceRange = int(colorDistance[0])
  122.                 else:
  123.                     distanceRange = random.randint(int(colorDistance[0]),int(colorDistance[1]))
  124.                 finalcolorDirection = None
  125.                 if len(colorDirection) == 1:
  126.                     finalcolorDirection = [(random.random()*2) - 1,(random.random()*2) - 1]
  127.                 else:
  128.                     finalcolorDirection = [ float(colorDirection[0]),float(colorDirection[1]) ]
  129.                
  130.                 for d in range(distanceRange):
  131.                
  132.                    
  133.                     if 0 > x+int(finalcolorDirection[0]*d) >= mainImage.width or 0 > y+int(finalcolorDirection[1]*d) >= mainImage.height:
  134.                         break
  135.                     newPos = ( clamp(x+int(finalcolorDirection[0]*d),0,mainImage.width-1), clamp(y+int(finalcolorDirection[1]*d),0,mainImage.height-1)  )
  136.                     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 ) )
  137.                     OutputImage.putpixel(newPos , newPixel )
  138.     OutputImage.save(OutputFileName)
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment