def delete_not_exist(): if not os.path.exists(middle): logger.info('middle not exist!') return print('[delete_not_exist] begin') right_map = {} for root, dirs, files in os.walk(src): for file in files: if not yutils.is_gif(file) and not yutils.is_photo(file): continue source_file = ypath.join(root, file) rename_file = middle_out_file(source_file) if os.path.exists(rename_file): right_map[rename_file] = source_file for root, dirs, files in os.walk(middle): for file in files: if not yutils.is_gif(file) and not yutils.is_photo(file): continue middle_path = ypath.join(root, file) if middle_path in right_map: pass # print('' + source_path + " src:" + right_map[source_path]) else: thum_path = thum + middle_path[len(middle):] if os.path.exists(middle_path): os.remove(middle_path) if os.path.exists(thum_path): os.remove(thum_path) print('删除文件:', middle_path, thum_path) ypath.del_none_dir(middle) ypath.del_none_dir(thum) print('[delete_not_exist] end')
def convert_webp(path_class, src_root, webp_cache_root): if not yutils.is_gif(path_class.ext) and not yutils.is_photo(path_class.ext) and yutils.is_webp(path_class.path): # 如果是webp需要转换一下.然后把webp文件放到另外目录. src_root = ypath.join(path_class.pic_root, str(src_root)) webp_cache_root = ypath.join(path_class.pic_root, str(webp_cache_root)) move_target = ypath.decompose_path(path_class.path, src_root, webp_cache_root) if os.path.exists(move_target): os.remove(move_target) convert_target = None try: im = Image.open(path_class.path) ext = '.png' if im.format == 'PNG' or im.mode == 'RGBA' else '.jpg' convert_target = ypath.del_exten(path_class.path) + ext if os.path.exists(convert_target): os.remove(convert_target) im.save(convert_target) ypath.create_dirs(move_target) shutil.move(path_class.path, move_target) path_class.path = convert_target return True except Exception as e: if convert_target and os.path.exists(convert_target): os.remove(convert_target) pass return False return False
def file_call(file_list): for file in file_list: if not yutils.is_gif(file.ext) and not yutils.is_photo(file.ext): logger.info('这张不是图片:' + file.path) continue if file.path not in exist_in_db_list: new_file_dict.append(file)
def begin_s2middle_by_threads(src_dir, desc_dir, delete_exist): # , source_file, rename_path for root, _, files in os.walk(src_dir): for file in files: source_file = ypath.join(root, file) rename_file = middle_out_file(source_file, desc_dir) if (not delete_exist) and os.path.exists(rename_file): print('文件已存在!' + rename_file) continue if yutils.is_gif(source_file): shutil.copy(source_file, rename_file) continue if not yutils.is_photo(source_file): other_file.append(source_file) continue ypath.create_dirs(rename_file) print('源:' + source_file) img = Image.open(source_file) # 压缩尺寸 w, h = img.size pic_area = w * h if pic_area > middle_area: proportion = (middle_area / pic_area)**0.5 w = int(w * proportion) h = int(h * proportion) img.thumbnail((w, h), Image.ANTIALIAS) # 处理旋转信息. if 'exif' in img.info: old_exif = piexif.load(img.info["exif"]) if '0th' in old_exif and piexif.ImageIFD.Orientation in old_exif[ '0th']: orientation = old_exif['0th'][piexif.ImageIFD.Orientation] if orientation == 6: img = img.rotate(-90, expand=True) if orientation == 3: img = img.rotate(180) if orientation == 8: img = img.rotate(90, expand=True) exif_bytes = piexif.dump({}) img.save(rename_file, 'JPEG', exif=exif_bytes) else: try: img.save(rename_file) except: img = img.convert('RGB') img.save(rename_file)
def middle2thum(delete_exist): if delete_exist and os.path.exists(thum): shutil.rmtree(thum) for root, dirs, files in os.walk(middle): for file in files: source_path = ypath.join(root, file) print(source_path) desc_path = thum + source_path[len(middle):] if (not delete_exist) and os.path.exists(desc_path): print('文件已存在!' + desc_path) continue dir = os.path.dirname(desc_path) ypath.create_dirs(dir, is_dir=True) # gif要走配置 if yutils.is_gif(file): # gif_pic shutil.copy(gif_space, desc_path) pass if not yutils.is_photo(file): continue img = Image.open(source_path) w, h = img.size crop_img = img.crop(yutils.crop_size(w, h)) # 保存裁切后的图片 crop_img.thumbnail((thum_width, thum_width), Image.ANTIALIAS) if 'exif' in img.info: exif_dict = piexif.load(crop_img.info["exif"]) exif_bytes = piexif.dump(exif_dict) # crop_img.save(desc_path, 'JPEG') # 是否需要压缩质量,具体看情况而定. crop_img.save(desc_path, 'JPEG', exif=exif_bytes) # 是否需要压缩质量,具体看情况而定. else: try: crop_img.save(desc_path) except: crop_img = crop_img.convert('RGB') crop_img.save(desc_path) print(desc_path)
def begin_threads(self, create_db_list: list, f_list, err_list): for link_item in f_list: if not yutils.is_gif(link_item.ext) and not yutils.is_photo( link_item.ext): logger.info('这张不是图片:' + link_item.path) continue src_file = link_item.path file_stat = os.stat(src_file) if file_stat.st_size > PngImagePlugin.MAX_TEXT_MEMORY: err_list.append(src_file) continue pi = Photo() pi.src_abs_path = src_file pi.src_name = link_item.relative pi.src_mpath = MediaPath.pdc().search_by_abs_path( link_item.pic_root) try: file_steam = open(src_file, 'rb') pi.src_md5 = yutils.get_md5_steam(file_steam) pi.ctime = int(file_stat.st_ctime) pi.mtime = int(file_stat.st_mtime) desc_rela_name, pi.src_size = PhotoHelper.file_desc_dir( file_stat, pi.src_abs_path, pi.src_md5) src_img = Image.open(file_steam) if src_img.mode == 'RGB': pi.desc_rela_path = desc_rela_name + '.jpg' else: pi.desc_rela_path = desc_rela_name + link_item.ext pi.src_width, pi.src_height = src_img.size except: err_list.append(src_file) logger.info('这张图有错误!!!!!!!!!!!!!!!!!!!!!!!:' + src_file) continue with lock: desc_root = MediaPath.desc() pi.desc_mpath = MediaPath.pdc().search_by_abs_path( desc_root, False) desc_middle_path = ypath.join(desc_root, self.desc_middle_root, pi.desc_rela_path) desc_thum_path = ypath.join(desc_root, self.desc_thum_root, pi.desc_rela_path) desc_webp_path = ypath.join(desc_root, self.desc_webp_root, pi.desc_rela_path) ypath.create_dirs(desc_middle_path) ypath.create_dirs(desc_thum_path) ypath.create_dirs(desc_webp_path) m_img = PhotoHelper.convert_middle(src_img, link_item.path, desc_middle_path, self.middle_area) w, h = m_img.size pi.mid_width = w pi.mid_height = h pi.mid_size = os.path.getsize(desc_middle_path) pi.state = PicHelp.STATE_FINISH pi.is_gif = yutils.is_gif(link_item.ext) # webp_file = ypath.join(desc_webp_root, mulit_file_list[middle_file][0] + '.webp') # convert_webp(m_img, webp_file, middle_file) t_img = PhotoHelper.cut_middle2thum(m_img, desc_thum_path, self.thum_size) if m_img is not t_img: del m_img del t_img else: del m_img with lock: create_db_list.append(pi) if len(create_db_list) >= PhotoHelper.SYNC_PHOTO_DB_COUNT: Photo.objects.bulk_create(create_db_list) create_db_list.clear()