Today to do a super simple non-destructive enlargement of the picture of the program, the principle is very simple
JPG principle: read the RGB color value of the pixels of the original image and save it to a file, then double the original image to enlarge it, draw on the enlarged image, draw one pixel of the doubled size, line by line, then save and delete the color file
PNG principle: because some of the PNG is transparent, so the RGBA color value to save and draw, Mr. into the color file and template image, the template image is The template image is double the size of the original picture, is a temporary template, and then generated after the completion of the color file to read, in the template image on the restructuring and generation of the final picture, that is, lossless zoomed-in picture!
Don't forget to leave a like.
This is a lossless enlargement of the original image by a factor of 8, in JPG format.
This is a lossless 8x enlargement of the original image in PNG format.
Lossless enlargement of JPG source code
#!/usr/bin/python # -*- coding:utf-8 -*- import cv2 from PIL import Image import time import shutil import os DATA = input('Please enter the name of the image to be enlarged(JPGThe name of the document must be in numerals or in English):') Multiple = int(input('Please enter the magnification of the image to be enlarged(must be greater than1):')) DATA_file = input('Please enter the name of the picture you want the image to be saved:') print('Start zooming in ....') ('DATA') # New #Read the image The image to be enlarged src = (DATA) # Used to read the pixel RGB color values of the original image Read the image file first IMG = (DATA) # Get the size of the image Get the capital XY of the image is also reversed y, x = [:2] # Temporary decorators List_elements = [] for YY in range(y): # Get the number of pixels in the y-axis of the image, which is also the length. '''Meaning: loop through the RGB values of each pixel point of the image and store them as a list''' if int(len(List_elements)) >= 2: # Restore the value of the list to nothing at the end of each loop. List_elements = [] for XX in range(x): # Get the number of pixels on the x-axis of the image, also known as the width. # ((a, aa)) is used to get the RGB pixel value of a location in the image Hint: the value obtained corresponds to the BGR which is the inverse of the RGB. List_elements = List_elements + [list(((XX, YY)))] # Read the pixel values of a coordinate and store the tuple as a list NAME = open(f"DATA/{YY}", 'w') # Storage (str(List_elements)) # Save lists as strings () (2) # Delay it a bit to prevent the file from loading too slow to read the error # # Image zoom To double the original image and then draw on top of it # result = (src, (x*Multiple,y*Multiple)) for RGB_DATA_Y in range(y): # Loop through all the files # There are as many RGB color files as there are pixels in Y # '''Loop through the RGB color file just stored and loop through the plotting Plot in multiples to ensure lossless zoom''' NAME_ = eval(open(f'DATA/{RGB_DATA_Y}', 'r', encoding='utf-8').read()) # Read the file and turn it into a list for RGB_DATA_X in range(len(NAME_)): # Get how many sublists are in the file ''' Because the color obtained by getpixel is the reverse of RGB, which is BGR, the values in the color file are also reversed. values in the color file are also reversed, so when you read them here, you have to invert them and reverse the conversion. ''' _DATA = NAME_[RGB_DATA_X] _DATA.reverse() ''' Currently this module is the core module is mainly used to read and draw out the multiples of the original map Principle: result[1,1] = [255,255,255] Fill The first pixel of the picture is white [255,255,255] is the RGB value of the white color. result[0:2,0:4] = [255,255,255] Fills the image with white from 0 to 2 pixels on the X-axis and from 0 to 4 pixels on the Y-axis. ''' try: result[RGB_DATA_Y*Multiple:RGB_DATA_Y*Multiple+Multiple,RGB_DATA_X*Multiple:RGB_DATA_X*Multiple+Multiple] = _DATA except:pass # Write to save the image (DATA_file, result) print('Completion of ....') try: ("DATA") # Delete folders and files except:pass
Lossless enlargement of PNG source code
#!/usr/bin/python # -*- coding:utf-8 -*- import cv2 from PIL import Image import time import shutil import os DATA = input('Please enter the name of the image to be enlarged(PNGThe name of the document must be in numerals or in English):') Multiple = int(input('Please enter the magnification of the image to be enlarged(must be greater than1):')) DATA_file = input('Please enter the name of the picture you want the image to be saved:') print('Start zooming in ....') ('DATA') # New #Read the image The image to be enlarged src = (DATA) # Used to read the pixel RGB color values of the original image Read the image file first IMG = (DATA) # Get the size of the image Get the capital XY of the image is also reversed y, x = [:2] # Templates # Image zoom To double the original image and then draw on top of it # result = (src, (x*Multiple,y*Multiple)) # Write to save images - leave template images alone ('', result) # Temporary decorators List_elements = [] # Number of stored files Easy to read later, no misreading Number_documents = 0 for YY in range(y): # Get the number of pixels in the y-axis of the image, which is also the length. '''Meaning: loop through the RGBA values of each pixel point of the image and store them as a list''' if int(len(List_elements)) >= 2: # Restore the value of the list to nothing at the end of each loop. List_elements = [] for XX in range(x): # Get the number of pixels on the x-axis of the image, also known as the width. # ((a, aa)) is used to get the RGBA pixel value of a location in the image List_elements = List_elements + [((XX, YY))]*Multiple # Read the pixel values of a coordinate and store the tuple as a list Multiple is the multiplier for a in range(Multiple): # Multiple is a multiplier, and if it's 2x, it generates two files of the same color, and then it's output in a single line multiple times in post to make sure it's zoomed in at the pixel level. NAME = open(f"DATA/{Number_documents}", 'w') # Storage (str(List_elements)) # Save lists as strings () Number_documents = Number_documents + 1 (1) # Delay it a bit to prevent the file from loading too slow to read the error # DATA_ = list() # Define the list of data to be processed for a in range(Number_documents): NAME = open(f"DATA/{a}", 'r').read() # Read the color file NAME = list(eval(NAME)) # Convert color files to lists for aa in range(len(NAME)): # Loop through the list of color values DATA_.append(NAME[aa]) # Save color values to data list # Open the write template image IMG_2 = ('') # Converted to RGBA RGBA_IMG = IMG_2.convert("RGBA") RGBA_IMG.putdata(DATA_) # Write pictures RGBA_IMG.save(DATA_file, "PNG") # Save the picture print('Completion of ....') try: ("DATA") # Delete folders and files except:pass try: ("") # Delete files except:pass
to this article on Python to achieve lossless zoom picture sample code is introduced to this article, more related Python lossless zoom picture content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!