Exemplo n.º 1
0
def main():
    if len(sys.argv) < 2:
        print('Usage: python make-image.py <input> <output>')
        sys.exit(0)

    im = Image.fromPNG(sys.argv[1])

    for y in xrange(0, im.height):
        yiq_line = []
        for x in xrange(0, im.width):
            i = (im.width * y) + x
            yiq_line.append(yiq.from_rgb(im.data[i]))
    
        #yiq_line = yiq.simulate_composite_line(yiq_line)
        yiq_line = ntsc_line(yiq_line, burst_start=([0.3, 0.6, 0.9])[y%3])
        
        """
        for i in range(len(yiq_line)):
            yiq_line[i][0] = 255
            #yiq_line[i][1] = 0
            #yiq_line[i][2] = 0
        """
    
        for x in xrange(0, im.width):
            i = (im.width * y) + x
            tmp = yiq.to_rgb(yiq_line[x])
            if tmp[0] > 255: tmp[0] = 255
            if tmp[1] > 255: tmp[1] = 255
            if tmp[2] > 255: tmp[2] = 255
            
            im.data[i] = [int(tmp[0]), int(tmp[1]), int(tmp[2])]

    im.savePNG('output.png')
Exemplo n.º 2
0
Arquivo: image.py Projeto: cleure/TV4X
 def tv4x_scale(self,
         blur_factor=0.4,
         noise_max=16,
         noise_probability=0.75,
         mult_matrix_even=None,
         mult_matrix_odd=None,
         rgb_matrix_even=None,
         rgb_matrix_odd=None,
         brightness=0.0,
         contrast=0.0):
     
     """
     
     TV4X Scaling Filter. Parameters are:
     
     @param  blur_factor         - 3x3 Blur Factor. Float between 0.0 and 1.0.
     @param  noise_max           - Integer. Maxiumum noise differential (+/- amount).
     @param  noise_probability   - Noise probability. Float between 0.0 (never)
                                   and 1.0 (always).
     @param  mult_matrix_even    - 4x4 Pixel multiplication matrix for even fields.
     @param  mult_matrix_odd     - 4x4 Pixel multiplication matrix for odd fields.
     @param  brightness          - Brightness amount. Float between -100.0 and 100.0.
     @param  contrast            - Constrast amount. Float between -100.0 and 100.0.
     
     """
     
     #
     # Matrix:
     #
     # O = bright
     # o = normal
     # c = medium
     # X = dark
     #
     # Even Fields:
     #
     # O o O o
     # O o O o
     # c X c X
     # X c X c
     #
     # Odd Fields:
     #
     # o O o O
     # o O o O
     # X c X c
     # c X c X
     #
     # 1.25  1.0     1.25    1.0
     # 1.25  1.0     1.25    1.0
     # 0.90  0.80    0.90    0.80
     # 0.80  0.90    0.80    0.90
     #
     
     if mult_matrix_even is None:
         mult_matrix_even = slot_mask_scanline[1]
     
     if mult_matrix_odd is None:
         mult_matrix_odd = slot_mask_scanline[0]
     
     if rgb_matrix_odd is None:
         rgb_matrix_odd = slot_mask_rgb_matrix[0]
     
     if rgb_matrix_even is None:
         rgb_matrix_even = slot_mask_rgb_matrix[1]
     
     width = self.width * 4
     height = self.height * 4
     scaled = [[0, 0, 0]] * (width * height)
     
     blur_factor_ml = blur_factor / 8
     blur_factor = 1.0 - blur_factor
     
     # Make a copy of data
     data = copy.deepcopy(self.data)
     
     # Simulate YIQ Color Space, NTSC Composite Signal, etc
     for y in xrange(0, self.height):
         yiq_line = []
         for x in xrange(0, self.width):
             i = (self.width * y) + x
             yiq_line.append(yiq.from_rgb(data[i]))
 
         yiq_line = yiq.simulate_composite_line(yiq_line)
         for x in xrange(0, self.width):
             i = (self.width * y) + x
             tmp = yiq.to_rgb(yiq_line[x])
             data[i] = [tmp[0], tmp[1], tmp[2]]
     
     # Noise filter
     if not noise_max == 0:
         data = self.filter_noise(
                 noise_max=noise_max,
                 noise_probability=noise_probability,
                 data=data)
     
     # Brightness/Contrast filter
     data = self.filter_contrast2(
                 brightness=brightness,
                 contrast=contrast,
                 data=data)
     
     # Scanline brightness/contrast
     scan_brightness = 0.0
     scan_contrast = 12.0
     
     # Scanline slope/intersect
     scan_slope = math.tan((math.pi * (scan_contrast / 100.0 + 1.0) / 4.0))
     if scan_slope < 0.0: scan_slope = 0.0
     scan_slope = round(scan_slope, 10)
     scan_intersect = scan_brightness / 100.0 + ((100.0 - scan_brightness) / 200.0) * (1.0 - scan_slope)
     
     for y in range(0, self.height):
         #last_pixel = [[0, 0, 0] for a in range(0, 16)]
         
         for x in range(0, self.width):
             # Calculate buffer indexes
             i = (y * self.width) + x
             i2 = (y * width * 4) + (x * 4)
             
             # Blur filter. Similar to gaussian blur, but uses weighted factors
             # to control how much the original color is preserved.
             entropy = self.getEntropyForPixel(x, y,
                 width=self.width,
                 height=self.height,
                 data=data)
             
             """
             value = [0, 0, 0]
             for a in range(0, 3):
                 value[a] = sum([
                     entropy[0][a] * blur_factor_ml,     # -1, -1
                     entropy[1][a] * blur_factor_ml,     # -1,  0
                     entropy[2][a] * blur_factor_ml,     # -1,  1
                     entropy[3][a] * blur_factor_ml,     #  0, -1
                     entropy[4][a] * blur_factor,        #  0,  0
                     entropy[5][a] * blur_factor_ml,     #  0,  1
                     entropy[6][a] * blur_factor_ml,     #  1, -1
                     entropy[7][a] * blur_factor_ml,     #  1,  0
                     entropy[8][a] * blur_factor_ml,     #  1,  1
                 ])
             """
             
             value = data[i]
             
             # Build base matrix
             pixels = [[value[0], value[1], value[2]] for a in range(0, 16)]
             
             # Run pixels through the multiplication matrix/matrices
             for a in range(0, len(pixels)):
                 orig = [pixels[a][0], pixels[a][1], pixels[a][2]]
             
                 if y % 2:
                     # Even Matrix
                     pixels[a][0] = ((float(pixels[a][0]) * mult_matrix_even[a])
                                         + rgb_matrix_odd[a][0])
                     pixels[a][1] = ((float(pixels[a][1]) * mult_matrix_even[a])
                                         + rgb_matrix_odd[a][1])
                     pixels[a][2] = ((float(pixels[a][2]) * mult_matrix_even[a])
                                         + rgb_matrix_odd[a][2])
                 else:
                     # Odd Matrix
                     pixels[a][0] = ((float(pixels[a][0]) * mult_matrix_odd[a])
                                         + rgb_matrix_even[a][0])
                     pixels[a][1] = ((float(pixels[a][1]) * mult_matrix_odd[a])
                                         + rgb_matrix_even[a][1])
                     pixels[a][2] = ((float(pixels[a][2]) * mult_matrix_odd[a])
                                         + rgb_matrix_even[a][2])
                 
                 # Round to ceiling, and make them integers
                 pixels[a][0] = int(math.ceil(pixels[a][0]))
                 pixels[a][1] = int(math.ceil(pixels[a][1]))
                 pixels[a][2] = int(math.ceil(pixels[a][2]))
                 
                 # Underrun checking
                 if pixels[a][0] < 0: pixels[a][0] = 0
                 if pixels[a][1] < 0: pixels[a][1] = 0
                 if pixels[a][2] < 0: pixels[a][2] = 0
                 
                 # Overrun checking
                 if pixels[a][0] > 255: pixels[a][0] = 255
                 if pixels[a][1] > 255: pixels[a][1] = 255
                 if pixels[a][2] > 255: pixels[a][2] = 255
             
             # Scanline brightness/contrast
             for a in range(8, 16):
                 for b in range(0, 3):
                     pixels[a][b] = ((scan_slope * (1.0/255.0)) * pixels[a][b] + scan_intersect) * 255.0
                     if pixels[a][b] > 255: pixels[a][b] = 255
                     if pixels[a][b] < 0: pixels[a][b] = 0
                     pixels[a][b] = int(math.floor(pixels[a][b]))
             
             # Grab surrounding pixels
             # Calculate weighted differences of surrounding pixels
             # Apply values to self, and run through matrix?
             
             """
             
             pixel = [rgb]
             entropy = [[rgb], [rgb], [rgb]...]
             
             X X X
             X T X
             X X X
             
             0 1 2
             3 4 5
             6 7 8
             
             4 =
               a b c d
               e f g h
               i j k l
               m n o p
               
             0 0 0 0 1 1 1 1 2 2 2 2 
             0 0 0 0 1 1 1 1 2 2 2 2
             0 0 0 0 1 1 1 1 2 2 2 2
             0 0 0 0 1 1 1 1 2 2 2 2
             3 3 3 3 A B C D 5 5 5 5
             3 3 3 3 E F G H 5 5 5 5
             3 3 3 3 I J K L 5 5 5 5
             3 3 3 3 M N O P 5 5 5 5
             6 6 6 6 7 7 7 7 8 8 8 8
             6 6 6 6 7 7 7 7 8 8 8 8
             6 6 6 6 7 7 7 7 8 8 8 8
             6 6 6 6 7 7 7 7 8 8 8 8
             
             The sides and corners of the current pixel will be blended with the
             surrounding virtual pixels (from the source image). The center sub
             pixels, of the current pixel will then be blended with the sides,
             which were just blended. This should give a diffuse effect, that's
             more intense depending on the contrast between the two's luminosity.
             
             Blend Table:
                 Pixel A: Blend with 0, 1, 2
                 Pixel B: Blend with 1
                 Pixel C: Blend with 1
                 Pixel D: Blend with 1, 2, 5
                 Pixel E: Blend with 3
                 Pixel I: Blend with 3
                 Pixel M: Blend with 3, 6, 7
                 Pixel N: Blend with 7
                 Pixel M: Blend with 7
                 Pixel P: Blend with 5, 7, 8
                 Pixel F: Blend with A, B, E
                 Pixel G: Blend with C, D, H
                 Pixel J: Blend with I, M, N
                 Pixel K: Blend with O, P, L
             
             """
             
             """
             entropy_tlc = [0, 0, 0]
             entropy_trc = [0, 0, 0]
             entropy_blc = [0, 0, 0]
             entropy_brc = [0, 0, 0]
             
             for a in range(0, 3):
                 entropy_tlc[a] = int(sum([
                     entropy[0][a],
                     entropy[1][a],
                     entropy[3][a]]) / 3.0)
                 
                 entropy_trc[a] = int(sum([
                     entropy[1][a],
                     entropy[2][a],
                     entropy[5][a]]) / 3.0)
                 
                 entropy_blc[a] = int(sum([
                     entropy[3][a],
                     entropy[6][a],
                     entropy[7][a]]) / 3.0)
                 
                 entropy_brc[a] = int(sum([
                     entropy[5][a],
                     entropy[7][a],
                     entropy[8][a]]) / 3.0)
             
             pixels[0] = self.blend_pixels(pixels[0], entropy_tlc)
             pixels[1] = self.blend_pixels(pixels[2], entropy[1])
             pixels[2] = self.blend_pixels(pixels[3], entropy[1])
             pixels[3] = self.blend_pixels(pixels[4], entropy_trc)
             pixels[4] = self.blend_pixels(pixels[4], entropy[3])
             pixels[7] = self.blend_pixels(pixels[7], entropy[5])
             pixels[8] = self.blend_pixels(pixels[8], entropy[3])
             pixels[11] = self.blend_pixels(pixels[11], entropy[5])
             
             pixels[12] = self.blend_pixels(pixels[12], entropy_blc)
             pixels[13] = self.blend_pixels(pixels[13], entropy[7])
             pixels[14] = self.blend_pixels(pixels[14], entropy[7])
             pixels[15] = self.blend_pixels(pixels[15], entropy_brc)
             
             pixels[9] = self.blend_pixels(pixels[9], pixels[8])
             pixels[10] = self.blend_pixels(pixels[10], pixels[11])
             pixels[5] = self.blend_pixels(pixels[5], pixels[4])
             pixels[6] = self.blend_pixels(pixels[6], pixels[7])
             """
             
             pixels[0] = self.blend_pixels(pixels[0], entropy[3])
             pixels[1] = self.blend_pixels(pixels[1], pixels[0])
             pixels[3] = self.blend_pixels(pixels[3], entropy[5])
             pixels[2] = self.blend_pixels(pixels[2], pixels[3])
             
             pixels[4] = self.blend_pixels(pixels[4], entropy[3])
             pixels[5] = self.blend_pixels(pixels[5], pixels[4])
             pixels[7] = self.blend_pixels(pixels[7], entropy[5])
             pixels[6] = self.blend_pixels(pixels[6], pixels[7])
             
             pixels[8] = self.blend_pixels(pixels[8], entropy[3])
             pixels[9] = self.blend_pixels(pixels[9], pixels[8])
             pixels[11] = self.blend_pixels(pixels[11], entropy[5])
             pixels[10] = self.blend_pixels(pixels[10], pixels[11])
             
             pixels[12] = self.blend_pixels(pixels[12], entropy[3])
             pixels[13] = self.blend_pixels(pixels[13], pixels[12])
             pixels[14] = self.blend_pixels(pixels[14], entropy[5])
             pixels[15] = self.blend_pixels(pixels[15], pixels[14])
             
             # data[i][0] = ((data[i][0] - 0.5) * (math.tan((contrast + 1) * math.pi/4))) + 0.5
             
             # Copy values
             # Row 1
             scaled[(i2)+0] = pixels[0]
             scaled[(i2)+1] = pixels[1]
             scaled[(i2)+2] = pixels[2]
             scaled[(i2)+3] = pixels[3]
             
              # Row 2
             scaled[(i2 + width)+0] = pixels[4]
             scaled[(i2 + width)+1] = pixels[5]
             scaled[(i2 + width)+2] = pixels[6]
             scaled[(i2 + width)+3] = pixels[7]
             
              # Row 3
             scaled[(i2 + (width * 2))+0] = pixels[8]
             scaled[(i2 + (width * 2))+1] = pixels[9]
             scaled[(i2 + (width * 2))+2] = pixels[10]
             scaled[(i2 + (width * 2))+3] = pixels[11]
             
             # Row 4
             scaled[(i2 + (width * 3))+0] = pixels[12]
             scaled[(i2 + (width * 3))+1] = pixels[13]
             scaled[(i2 + (width * 3))+2] = pixels[14]
             scaled[(i2 + (width * 3))+3] = pixels[15]
     
     return Image(width, height, scaled)